在 CMD 中引用和转义规则已经够复杂了,当你添加 WMIC 复杂性时,它们会变得更加疯狂。
WMIC 通常可以使用' 或" 作为引号字符。双引号内的双引号可以使用\" 进行转义。单引号中的单引号也可以使用\'进行转义,但是反斜杠似乎没有被消耗,所以看起来没什么用。
CMD 只使用",没有办法在带引号的字符串中转义"。 &、|、< 等不在引号内的毒字符必须像^& 等一样转义。
尝试合并 WMIC 和 CMD 的引用和转义规则很棘手。请记住,CMD 引用和转义发生在 WMIC 看到命令行之前。
另外,您可以使用PROCESS CALL CREATE 代替PROCESS CREATE。我想PROCESS CREATE 是可能的,但我从未见过它是如何完成的。
我不确定我是否知道不使用 WMIC 的实际命令行。根据您的代码,我假设以下方法可行:
C:\PROGRA~1\VideoLAN\VLC_117\vlc.exe rtsp://abcd --sout="#duplicate{dst=display, dst={std{access=file, mux=ps, dst='vlc_117/video.mpg'}}} --rtsp-caching=120 --no-video-title-show"
如果是这样,那么我相信以下 WMIC 命令会起作用:
wmic process call create 'C:\PROGRA~1\VideoLAN\VLC_117\vlc.exe rtsp://abcd --sout="#duplicate{dst=display, dst={std{access=file, mux=ps, dst='vlc_117/video.mpg'}}} --rtsp-caching=120 --no-video-title-show"'
WMIC 必须在创建后将整个命令行视为一个连续的字符串。 'vlc_117/video.mpg' 中的内部单引号不会引起问题,因为内容中没有空格。添加空格会破坏这个解决方案,需要另一种策略。
如果您在 exe 路径周围使用双引号,您应该能够使用长路径而不是短路径:
wmic process call create '"C:\Program Files\VideoLAN\VLC_117\vlc.exe" rtsp://abcd --sout="#duplicate{dst=display, dst={std{access=file, mux=ps, dst='vlc_117/video.mpg'}}} --rtsp-caching=120 --no-video-title-show"'
您可能希望在批处理文件中的变量中捕获 PID,在这种情况下将使用 FOR /F 命令。这增加了更多的复杂性。像=、, 等不带引号的 CMD 标记分隔符必须被转义,因为 FOR /F 引入了额外的解析层。由于额外的解析层,未加引号的毒字符也必须被转义。
for /f "tokens=2 delims=;= " %%N in (
'wmic process call create '"C:\Program Files\VideoLAN\VLC_117\vlc.exe" rtsp://abcd --sout^="#duplicate{dst=display, dst={std{access=file, mux=ps, dst='vlc_117/video.mpg'}}} --rtsp-caching=120 --no-video-title-show"' ^| find "ProcessId"'
) do set "pid=%%N"
FOR /F IN() 子句中的封闭单引号不会导致问题,因为它们在 WMIC 看到命令之前就被剥离了。但--sout=... 中未引用的= 必须转义。
我无法测试上述任何内容,因此可能没有一个可以按照书面说明进行。但是,我讨论的概念应该仍然有效。
由于引用和转义的层数和复杂性,对命令行的看似微小的更改可能会对解决方案产生重大影响。如果命令不是我解释的那样,请通过编辑您的问题让我知道正确的命令,我可以尝试调整答案。
2014 年 5 月 27 日更新
我的坏消息。 PROCESS CALL CREATE 选项期望完整的命令行作为第一个字符串参数,可选地后跟工作目录作为第二个参数。坏消息是参数由逗号分隔。您的命令行也有逗号,并且 PROCESS CALL CREATE 解析器(无论是什么)似乎都不支持命令行中的逗号:(
我尝试在单引号或双引号中引用逗号,但这没有帮助。我也试过用\ 转义逗号。再次,没有运气。我也用谷歌搜索了一个解决方案,结果一无所获。
恐怕没有解决方案,除非有可用的 VLC 语法可以消除逗号,或者如果有办法将复杂的 VLC 命令行参数放在某种类型的外部脚本文件中。或者也许其他一些聪明人发现了一种逃避逗号的方法?
这里有一些例子展示了我的尝试。我没有 VLC,所以我简单地将 CMD /C 替换为 ECHO 语句。每次尝试 ECHO 逗号都会失败。
@echo off
:: All of these commands without commas work just fine
wmic process call create 'cmd /c echo hello world^&pause'
wmic process call create 'cmd /c "echo hello world&pause"'
wmic process call create "cmd /c echo hello world&pause"
:: But none of these commands with commas work
wmic process call create 'cmd /c echo hello,goodbye^&pause'
wmic process call create 'cmd /c "echo hello,goodbye&pause"'
wmic process call create "cmd /c echo hello,goodbye&pause"
wmic process call create 'cmd /c echo hello\,goodbye^&pause'
wmic process call create 'cmd /c "echo hello\,goodbye&pause"'
wmic process call create "cmd /c echo hello\,goodbye&pause"
2014-12-23 更新:已找到解决方案!
在 DosTips,我们一群人开发了various methods for a batch script to determine its own PID。一旦知道这一点,就可以使用 WMIC 列出父会话的所有子进程。通过仔细的簿记,可以可靠地确定每个新创建的子进程的 PID。但它确实需要大量的代码。仅当您的命令行无法通过 WMIC PROCESS CALL CREATE(逗号问题)传递时才需要这样做。
@echo off
setlocal enableDelayedExpansion
:: Get the PID and UID for this batch process
call :getMyPID
:: Initialize an existing PIDs list
set "PIDs= "
:: Get a list of all existing child processes, except for the
:: child CMD.EXE process that was created by this FOR /F loop.
:: This is necessary because the console session that this script
:: is running in might have previously created a child process.
for /f %%A in (
'2^>nul wmic process where "ParentProcessID=%myPID% and not CommandLine like '%%<%UID%>%%'" get ProcessID'
) do for %%B in (%%A) do set "PIDs=!PIDs!%%B "
:: Create your new process as you normally would.
:: For this demonstration, I will simply create a new CMD.EXE session
start
:: Get the PID of the newly created child process by getting all child PIDs,
:: except for the child CMD.EXE process that was created by this FOR /F loop.
:: Ignore any PID that already exists in the %PIDs% list. The only PID left
:: is your new process.
for /f %%A in (
'2^>nul wmic process where "ParentProcessID=%myPID% and not CommandLine like '%%<%UID%>%%'" get ProcessID'
) do for %%B in (%%A) do if "!PIDs: %%B =!" equ "!PIDs!" set "PID=%%B"
:: At this point you could append the new PID to the PIDs list using
:: set "PIDs=!PIDs!%PID% "
:: Then you can repeat the steps of creating a new proces and getting the new PID
::
:: But instead, I will simply show the result and exit
echo new PID=%PID%
exit /b
:getMyPID
setlocal disableDelayedExpansion
:getLock
:: Establish a nearly unique temp file name using %time%
set "lock=%temp%\%~nx0.%time::=.%.lock"
:: Transform the full path into a UID that can be used in a WMIC query
set "uid=%lock:\=:b%"
set "uid=%uid:,=:c%"
set "uid=%uid:'=:q%"
set "uid=%uid:_=:u%"
setlocal enableDelayedExpansion
set "uid=!uid:%%=:p!"
endlocal & set "uid=%uid%"
:: Establish an exclusive lock on the temp file
:: If this fails, then loop back and try again until success
:: This guaranees no two process will ever have the same UID
2>nul ( 9>"%lock%" (
%= The FOR /F loops creates a child CMD.EXE process which in turn invokes WMIC. =%
%= The child CMD.EXE process contains the WMIC query with the UID in its command line. =%
%= The WMIC query identifies the CMD.EXE process with the UID in the command line, =%
%= and returns the parent process ID, which happens to be the PID for this batch script =%
for /f "skip=1" %%A in (
'wmic process where "name='cmd.exe' and CommandLine like '%%<%uid%>%%'" get ParentProcessID'
) do for %%B in (%%A) do set "PID=%%B"
(call ) %= Guarantee success so we don't accidently loop again =%
))||goto :getLock
:: Delete the temp file
del "%lock%" 2>nul
:: Return the result
( endlocal
set "myPID=%PID%"
set "UID=%uid%"
)
exit /b