Computer Hope

Microsoft => Microsoft DOS => Topic started by: kamak on April 21, 2008, 11:12:42 PM

Title: Writing batch files in batch files help
Post by: kamak on April 21, 2008, 11:12:42 PM
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!
Title: Re: Writing batch files in batch files help
Post by: gpl on April 22, 2008, 02:42:04 AM
Try 'escaping' any special DOS characters by preceding them with a ^ symbol
for example
Echo ^>
Echo ^%

etc

Graham
Title: Re: Writing batch files in batch files help
Post by: kamak on April 22, 2008, 05:05:55 AM
Thanks 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
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 >> pconfig.bat


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

any more suggestions?  :P




Title: Re: Writing batch files in batch files help
Post by: RandomGuy on April 22, 2008, 05:09:52 AM
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.  :-\
Title: Re: Writing batch files in batch files help
Post by: gpl on April 22, 2008, 06:12:21 AM
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

Graham
Title: Re: Writing batch files in batch files help
Post by: blastman on April 22, 2008, 06:20:46 AM
this simple batch makes a variable and then creates another batch and uses that variable.

Code: [Select]
@echo off

set answer=hello world

echo.@echo 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


Title: Re: Writing batch files in batch files help
Post by: kamak on April 22, 2008, 06:21:02 AM
Thanks for the tip about the setting out, will make it much easier to read :P

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

echo.@echo 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 :)


Title: Re: Writing batch files in batch files help
Post by: blastman on April 22, 2008, 06:51:08 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....

Title: Re: Writing batch files in batch files help
Post by: kamak on April 22, 2008, 06:52:39 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 :)
Title: Re: Writing batch files in batch files help
Post by: 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
Title: Re: Writing batch files in batch files help
Post by: blastman on April 22, 2008, 07:13:11 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.
Title: Re: Writing batch files in batch files help
Post by: Dias de verano on April 25, 2008, 01:24:19 PM
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



Title: Re: Writing batch files in batch files help
Post by: RandomGuy on April 26, 2008, 11:21:38 PM
THANKYOU Dias de verano! it helped us heaps! :)