Double-quoted multi-line batch file command

When using the ^ character to enter a multi-line command with arguments, when using double quotes to use strings with spaces, the ^ character is also transmitted, can anyone explain how 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 looks like

 C:\>call openfiles.cmd "C:\dir with spaces\file with spaces.txt" ^ ELSE was unexpected at this time. 
+5
source share
2 answers

Carriage Rule:

The carriage slips away from the next character, so that the character loses all special effects.
If the next character is a line drop, it will take the next character (even if it is also a translation string).

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 called "Cat Dog" because the space has been shielded and no longer works as a delimiter.

But it’s still possible to break this rule!
You just need to redirect just before the carriage, it still leaves a line (multi-line work continues), but the next character is no longer escaped.

 echo #5 Line1< nul ^ & echo Line2 

So you can also use this to create your multi-line 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" 
+5
source

Having tried some different things, I managed to get it to work with extra space in front of double quotes. Notworking.cmd changes for the following processed

 @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 before double quotes

+1
source

Source: https://habr.com/ru/post/1236083/


All Articles