How to handle the closing bracket in path names in a for loop?

I have a long path name for the program that I have to run in the for / f loop, which includes the closing bracket ")", and from which I need to parse the output:

for /f "tokens=1" %%G in ('"C:\Documents and Settings\myaccount\Desktop\Test_release (x86)\program.exe" list') do (echo Will do something with %%G)

... where "list" is the parameter passed to my program. I get the error "C: \ Documents" is not recognized as an internal or external command, operating program or batch file. "

I know that the problem is that the closing bracket actually closes the โ€œforโ€ block, so trailing double quotes are not โ€œvisibleโ€, so the long path name is no longer enclosed in double quotes. I donโ€™t understand why this is happening, since my path is enclosed in double quotes? I also tried the usebackq option:

for /f "usebackq tokens=1" %%G in (`"C:\Documents and Settings\myaccount\Desktop\Test_release (x86)\program.exe" list`) do (echo Will do something with %%G)

... without the best results. I tried to run away like this "^)" or something like this "^^)", nothing to do. Tried to double double quotes:

for /f "tokens=1" %%G in ('""C:\Documents and Settings\myaccount\Desktop\Test_release (x86)\program.exe"" list') do (echo Will do something with %%G)

Still not working.

In addition, I use a variable that contains a path that is not known in advance (built from% CD%), and EnableDelayedExpansion is activated . I tried a slow extension (which fixed similar problems in other situations) to prevent the variable from expanding while reading and delaying it at runtime:

setlocal EnableDelayedExpansion
set _var=%CD%\program.exe
@REM _var now contains C:\Documents and Settings\myaccount\Desktop\Test_release (x86)\program.exe
for /f "tokens=1" %%G in ('"!_var!" list') do (echo %%G)
endlocal

Still not working, I donโ€™t understand why.

But , doubling double quotes with slow expansion in the above code:

for /f "tokens=1" %%G in ('""!_var!"" list') do (echo %%G)

!... ... ??? ? . , ...

?

+5
2

, XP , Windows.

XP FOR/F: http://www.dostips.com/forum/viewtopic.php?p=9062#p9062. .

, FOR/F IN(). CMD \C command (. Windows (CMD.EXE)?)

, Aacini PROG.BAT.

echo cmdcmdline=%cmdcmdline%

, CMD , /C, XP , Windows.

XP, Vista :

for /f "delims=" %a in ('"test (this)\prog" args') do @echo %a

, FOR (% cmdcmdline%), ( % COMSPEC%):

C:\Windows\system32\cmd.exe /c "test (this)\prog" args

XP CMD , . ( ). Vista , .

HELP CMD

If /C or /K is specified, then the remainder of the command line after
the switch is processed as a command line, where the following logic is
used to process quote (") characters:

    1.  If all of the following conditions are met, then quote characters
        on the command line are preserved:

        - no /S switch
        - exactly two quote characters
        - no special characters between the two quote characters,
          where special is one of: &<>()@^|
        - there are one or more whitespace characters between the
          two quote characters
        - the string between the two quote characters is the name
          of an executable file.

    2.  Otherwise, old behavior is to see if the first character is
        a quote character and if so, strip the leading character and
        remove the last quote character on the command line, preserving
        any text after the last quote character.

, CMD 1, , ( ) XP, 2 CMD

test (this)\prog args

, !

- , 1. , MS.

-, Vista , HELP. Vista ( ) 1, , .

2015-05-17: , Vista @, ^ & , . , <, > | , . , Vista 1 where special is one of: &<>@^|.

, , .

XP Vista .

for /f "delims=" %a in ('^""test (this)\prog" args^"') do @echo %a

, ) FOR. , IN(),

C:\Windows\system32\cmd.exe /c ""test (this)\prog" args"

XP, Vista 2, , CMD

"test (this)\prog" args

!

, , .


; ( ) , . , , . "", .

:

  • : for /f "tokens=1" %%G in ('"!_var!" list') do (echo %%G)

  • : for /f "tokens=1" %%G in ('""!_var!"" list') do (echo %%G)

var , , .

var "C:\Documents and Settings\myaccount\Desktop\Test_release (x86)\program.exe", .

, "test (this)\prog.exe"

"!var!" , ""test (this)\prog.exe"", . , , - :

" " " "

""!var!"" , """test (this)\prog.exe""", . :

" " " " " "

, :

var , !var! - XP: "! var!"

var , "!var!" Edit-, XP: "! var!" "

+4

, , , , . test (this) prog.bat:

>dir /b
test (this)
>dir /b "test (this)"
prog.bat
>type "test (this)\prog.bat"
@echo off
echo/One - Line one of prog
echo/Two - Line two of prog
echo/Three - Line three of prog

for /f. , useback , :

>for /f "usebackq tokens=1" %G in ("test (this)\prog.bat") do @echo %G
@echo
echo/One
echo/Two
echo/Three

. , , , ?

>for /f "usebackq tokens=1" %G in (`"test (this)\prog.bat"`) do @echo %G
'test' is not recognized as an internal or external command,
operable program or batch file.

, , test - , , test.bat:

>type test.bat
@echo off
echo Test.batFileLine1
echo Test.batFileLine2
echo Test.batFileLine2

>for /f "usebackq tokens=1" %G in (`"test (this)\prog.bat"`) do @echo %G
Test.batFileLine1
Test.batFileLine2
Test.batFileLine2

! , test (this)\prog.bat, ?

>for /f "usebackq tokens=1" %G in (`test (this)\prog.bat`) do @echo %G
\prog.bat`) was unexpected at this time.

? , :

>for /f "usebackq tokens=1" %G in (`test ^(this^)\prog.bat`) do @echo %G
Test.batFileLine1
Test.batFileLine2
Test.batFileLine2

, for /f , "usebackq", , , .

+1

All Articles