1.

Solve : Writing batch files in batch files help?

Answer»

Hi, i've come across a small problem in my coding and was wondering if anyone could help me FIND a way around it.

My problem is when i try and write a new batch file in a batch file using something like ECHO exampletext > example.bat i can't write the % for variables.

eg.

if i was trying to write the following through the command i mentioned earlier,

set /p ANSWER =
if /i %answer% == hi'
echo correct!


it would come out as:


set /p answer=
if /i == hi
echo correct!

Please help!Try 'escaping' any special DOS characters by preceding them with a ^ symbol
for example
Echo ^>
Echo ^%

etc

GrahamThanks for the reply, but it didn't work

i'll post my code, and the what i did after you posted.

@echo off
echo please choose a password!
set /p input=

echo @echo off > pconfig.bat
echo set /p pw = >> pconfig.bat
echo :A >> pconfig.bat
echo if /i %pw% == %input% ( >> pconfig.bat
echo CALL 94lock.bat >> pconfig.bat
echo pause >> pconfig.bat
echocls >> pconfig.bat
echo ) else goto next >> pconfig.bat

echo :next >> pconfig.bat
echo echo Wrong password >> pconfig.bat
echo pause >> pconfig.bat
echo goto :A >> pconfig.bat


i assume you meant trying:
echo if /i ^%pw% == %input% ( >> pconfig.bat

any more suggestions?




hey kamak, i tried echo if /i ^%pw% == %input% ( >> pconfig.bat, i also tried echo if /i ^%pw^% == %input% ( >> pconfig.bat, and neither of them work. It really helps to make things clear if you place the redirection at the START of the line, you can then concentrate on what you are writing out.

The % symbols around the input also need escaping -

Code: [Select]@echo off
echo please choose a password!
set /p input=

> pconfig.bat echo @echo off
>> pconfig.bat echo set /p pw =
>> pconfig.bat echo :A
>> pconfig.bat echo if /i ^%pw^% == ^%input^% (

>> pconfig.bat echo CALL 94lock.bat
>> pconfig.bat echo pause
>> pconfig.bat echo cls
>> pconfig.bat echo ) else goto next

>> pconfig.bat echo :next
>> pconfig.bat echo echo Wrong password
>> pconfig.bat echo pause
>> pconfig.bat echo goto :A
Grahamthis simple batch makes a VARIABLE and then creates another batch and uses that variable.

Code: [Select]@echo off

set answer=hello world

[emailprotected] off >>C:\test2.bat
echo.echo %answer% >>C:\test2.bat
echo.pause >>C:\test2.bat
echo.exit >>C:\test2.bat

start C:\test2.bat

exit


hope it helps


Thanks for the tip about the setting out, will make it much easier to read

Only problem is my code still isn't WORKING

it's still showing as:

@echo off
set /p pw =
:A
if /i == hey (
CALL 94lock.bat
pause
cls
) else goto next
:next
echo Wrong password
pause
goto :A

EDIT:

Quote

this simple batch makes a variable and then creates another batch and uses that variable.


Code:
@echo off

set answer=hello world

[emailprotected] off >>C:\test2.bat
echo.echo %answer% >>C:\test2.bat
echo.pause >>C:\test2.bat
echo.exit >>C:\test2.bat

start C:\test2.bat

exit



hope it helps

That code creates a file with hello world in it, right?

If it does, it wasn't quite what i wanted, but thanks for trying


Quote from: kamak on April 22, 2008, 06:21:02 AM
That code creates a file with hello world in it, right?

If it does, it wasn't quite what i wanted, but thanks for trying


not quite, it creates a variable and then echo's a few lines of code to a new batch file and runs it, displaying the varaible.

I thought the issue was that your variable wasn't echo-ing into your new batch file correctly. I'm sorry if i got the wrong end of the stick....

Quote from: blastman on April 22, 2008, 06:51:08 AM
Quote from: kamak on April 22, 2008, 06:21:02 AM
That code creates a file with hello world in it, right?

If it does, it wasn't quite what i wanted, but thanks for trying


not quite, it creates a variable and then echo's a few lines of code to a new batch file and runs it, displaying the varaible.

I thought the issue was that your variable wasn't echo-ing into your new batch file correctly. I'm sorry if i got the wrong end of the stick....




My problem was that it wouldn't print %pw% in the new file (kamak and i are actually working together btw) we want to print the actual % variable signs in the pconfig.bat fileQuote from: RandomGuy on April 22, 2008, 07:10:53 AM
(kamak and i are actually working together btw) we want to print the actual % variable signs in the pconfig.bat file

arh, I didn't get that from the other posts.

sorry for the confusion.In a batch, to echo one percent sign, use two percent signs

Code: [Select]@echo off
echo @echo off>test.bat
echo set variable=cats and dogs>> test.bat
echo echo variable=%%variable%%>>test.bat
echo this is test.bat
type test.bat
call test.bat

result

Code: [Select]this is test.bat
@echo off
set variable=cats and dogs
echo variable=%variable%
variable=cats and dogs



THANKYOU Dias de verano! it helped us heaps!


Discussion

No Comment Found