Passing quotes in quotes to cmd.exe
I need to run appcmd.exe
but running cmd.exe to query an IIS website and I also need to redirect the output.
The command should look like this:
cmd /c "c:\windows\system32\inetsrv\appcmd.exe list site MySite" > c:\output.txt
This works fine, however I开发者_JAVA技巧 encounter problems when I have spaces inside my paths, in which case I need to use quotes. Ideally, I would do:
cmd /c ""c:\windows\system32\inetsrv\appcmd.exe" list site "MySite"" > "c:\output.txt"
but this doesn't work - any ideas?
The help screen displayed by "cmd /c" shows this message:
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
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.
Use the "old" behavior mentioned in point 2. Type everything after /c as you would normally, but surround everything after /c in quotes. For example:
"c:\windows\system32\inetsrv\appcmd.exe" list site "MySite" > "c:\output.txt"
becomes
cmd /c ""c:\windows\system32\inetsrv\appcmd.exe" list site "MySite" > "c:\output.txt""
cmd /c c:\windows\system32\inetsrv\appcmd.exe list site MySite > c:\output.txt
This might work:
cmd /c "'c:\windows\system32\inetsrv\appcmd.exe' list site 'MySite'" > "c:\output.txt"
精彩评论