WMIC:如何对当前工作目录使用 "process call create"

WMIC: how to use "process call create" with the current working directory

我想使用 wmic Process call create 在当前工作目录打开一个 CMD。但是当我添加 "%~dp0" 指定目录时,以下命令什么也不输出。

for /F "tokens=2 delims==; " %%I in ('wmic Process call create "cmd.exe"^,"%~dp0" ^| find "ProcessId"') do echo PID = %%I

据我所知,%~dp0 将以 \ 结尾,因此 %~dp0 将类似于 C:\Users\

如果我删除 \ 它将起作用。例如:以下命令将起作用。

for /F "tokens=2 delims==; " %%I in ('wmic Process call create "cmd.exe"^,"C:\Users" ^| find "ProcessId"') do echo PID = %%I

如何将 wmic Process call create 与动态工作目录一起使用?

我找到了解决办法。我必须添加一个尾随点 (.)。

%~dp0%~dp0.

for /F "tokens=2 delims==; " %%I in ('wmic Process call create "cmd.exe"^,"%~dp0." ^| find "ProcessId"') do echo PID = %%I

阅读更多:How to get the path of a batch script without the trailing backslash in a single command?