How to handle spaces in path names in a for loop?

Trying to use the path to the current script, and the path contains spaces in it. I can't seem to get it to work:

C:\Test Directory>dir Volume in drive C has no label. Volume Serial Number is 7486-CEE6 Directory of C:\Test Directory 08/31/2010 07:28 PM <DIR> . 08/31/2010 07:28 PM <DIR> .. 08/31/2010 07:28 PM 20 echoit.cmd 08/31/2010 07:28 PM 94 test.cmd 2 File(s) 114 bytes 2 Dir(s) 344,141,197,312 bytes free C:\Test Directory>type echoit.cmd @echo off echo %* C:\Test Directory>type test.cmd @echo off for /f "tokens=*" %%a in ('%~dp0\echoit.cmd Hello World') do ( echo %%a ) C:\Test Directory>test 'C:\Test' is not recognized as an internal or external command, operable program or batch file. C:\Test Directory> 
+1
source share
3 answers

Change test.cmd to the following:

  @echo off for /f "tokens=*" %%a in ('"%~dp0\echoit.cmd" Hello World') do ( echo %%a ) 

You need to set the entire command, minus arguments, in quotation marks. The Windows command line treats a collection of words as a single command when the entire collection is quoted, so you must exclude the Hello World arguments. If you included this in quotation marks, Windows would treat this as part of the command, not as arguments.

+2
source

Have you tried adding quotes?

 for /f "tokens=*" %%a in ('"%~dp0\echoit.cmd" Hello World') do ( echo %%a ) 
0
source

how about using ~ fs0, i.e.

 C:\Test Directory>type test.cmd @echo off for /f "tokens=*" %%a in ('%~fs0\echoit.cmd Hello World') do ( echo %%a ) 

where% ~ fsI - extends% i to the full path name with only short names

0
source

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


All Articles