|
Answer» Firstly, hi to everyone, this is my first post. I stumbled here after a rather frustrating Google tiki tour. Wondering if anyone can help with a batch file issue that I’ve encountered a number of times and has just left me STUMPED. I’m trying to Echo relative paths into another file. I don’t want what the relative path equals just the relative path label.
So here’s an example of what I would put in for the line: Echo RoboCopy /switches "D:\My Files" "%^userprofile%\My Files" > “D:\My Files\RestoreBackup.cmd”
It works perfectly if I type it into a cmd window: RoboCopy /switches "D:\My Files" "%userprofile%\My Files" This line is EXACTLY what I want to appear in the target file!
But as soon as I put it into a .bat or .cmd file I get this: RoboCopy /switches "D:\My Files" "\My Files"
If I remove the ^ then I get this: RoboCopy /switches "D:\My Files" "C:\Users\Admin\My Files"
This certainly isn’t the only situation where this DISCREPANCY between cmd line and batch file result has affected me. If anyone can shed some light it’ll really help me out now and in the future. ALSO if you could travel back in time and tell me years ago you could save me countless hours
I appreciate your help
Thankyou You appear to appreciate that percent signs are special characters in batches and have to be "escaped". The escape character for a percent sign is another percent sign (not a caret). In other words in a batch when this line is executed:
echo %username%
... the result is that %username% is replaced by the variable's value. This is by design.
If you want to echo a percent sign then the variable name then another percent sign use this
echo %%username%%
Thank you so so much! I really can't properly express my gratitude for your help.
I spent so much time looking through walls of text but that worked like a charm.
Thank you so much!Most other special characters use a caret
e.g. command: dir > temp.txt dir *.jpg | find "Mother" dir *.jpg & echo. & echo Done
batch: dir ^> temp.txt > script.bat dir *.jpg ^| find "Mother" > script.bat dir *.jpg ^& echo. ^& echo Done > script.bat
Note: unlike percent signs, the exclamation marks used with delayed EXPANSION variables cannot be escaped simply by doubling. Use two carets thus ^^!
Useful reference page
http://www.robvanderwoude.com/escapechars.php
Cool. I've never ACTUALLY used the & and | symbols in batch files before. I knew about the carrot tho.
Thanks for the pointers, I'll have to play around with those. I'm constantly writing batch files, mostly for my backups and also for mounting ramdisks and creating temporary junction points to folder locations so my games load faster
|