When using the ^ symbol to enter a multiline command with arguments when using double quotes to use strings with spaces the ^ symbol is also passed on, can anyone explain way this is?
working.cmd
@echo off
call openfiles.cmd ^
C:\dir\filename.txt ^
C:\another_dir\another_file.txt
notworking.cmd
@echo off
call openfiles.cmd ^
"C:\dir with spaces\file with spaces.txt" ^
"C:\another dir with spaces\another file with spaces.txt"
openfiles.cmd looks like
@echo off
for %%x in (%*) do (
IF EXIST %%x (
call "c:\Program Files\Notepad++\notepad++.exe" %%x
) ELSE (
call echo Not found %%x
)
)
pause
the error I get look like
C:\>call openfiles.cmd "C:\dir with spaces\file with spaces.txt" ^
ELSE was unexpected at this time.
%%a are special variables created by the for command to represent the current loop item or a token of a current line. for is probably about the most complicated and powerful part of batch files. If you need loop, then in most cases for has you covered.
Double quotes enable escaping through the use of the backslash (\). For example, if the special character semicolon is a value that is double-quoted, it should be represented as backslash semicolon (\;) so that the Integration Engine knows that the actual value (;) should be used.
A batch file can be commented using either two colons :: or a REM command. The main difference is that the lines commented out using the REM command will be displayed during execution of the batch file (can be avoided by setting @echo off ) while the lines commented out using :: , won't be printed.
The Windows command prompt (cmd.exe) allows the ^ (Shift + 6) character to be used to indicate line continuation. It can be used both from the normal command prompt (which will actually prompt the user for more input if used) and within a batch file.
The problem is, that the quote at the beginning of the second line will be escaped by the multi line caret.
Therefore the last quote in the line starts the quoting instead of stopping it and so the caret in the second line is handled as normal character.
call openfiles.cmd ^"C:\dir with spaces\file with spaces.txt" ^
**This is a seperate line** "C:\another dir with spaces\another file with spaces.txt"
A caret escapes the next character, so that the character loses all special effects.
If the next character is a linefeed drop this and take the next character (even when this is also a linefeed).
With this simple rule you can explain things like
echo #1 Cat^&Dog
echo #2 Cat^
&Dog
echo #3 Redirect to > Cat^
Dog
setlocal EnableDelayedExpansion
set linefeed=^
echo #4 line1!linefeed!line2
#3
creates a file named "Cat Dog" as the space was escaped and doesn't work as delimiter anymore.
But it's still possible to break this rule!
You only need to put any redirection just in front of the caret, it still drops the linefeed (multiline still works), but the next character isn't escaped anymore.
echo #5 Line1< nul ^
& echo Line2
So you could also use this to build your multiline command
call openfiles.cmd < nul ^
"C:\dir with spaces\file with spaces.txt" < nul ^
"C:\another dir with spaces\another file with spaces.txt"
Or using a macro
set "\n=< nul ^"
call openfiles.cmd %\n%
"C:\dir with spaces\file with spaces.txt" %\n%
"C:\another dir with spaces\another file with spaces.txt"
After trying some diffrent things I managed to get it working with just a extra space infront of the double quotes. Changes notworking.cmd to the following worked
@echo off
call openfiles.cmd ^
"C:\dir with spaces\file with spaces.txt" ^
"C:\another dir with spaces\another file with spaces.txt"
note the spaces in front of the double quotes
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With