Computer Hope
Microsoft => Microsoft DOS => Topic started 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!
-
Try 'escaping' any special DOS characters by preceding them with a ^ symbol
for example
Echo ^>
Echo ^%
etc
Graham
-
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
-
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 -
@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
-
this simple batch makes a variable and then creates another batch and uses that variable.
@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
-
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:
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 :)
-
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....
-
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 file
-
(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
@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
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! :)