Search code examples
windowswmic

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


I want to use wmic Process call create to open a CMD at current working directory. But when I add "%~dp0" to specify directory the following command output nothing.

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

As I know %~dp0 will end with \, so %~dp0 will be like C:\Users\.

If I remove \ It will works. E.g: The following command will work.

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

How can I use wmic Process call create with dynamic working directory?


Solution

  • I found the solution. I have to add a trailing dot (.).

    %~dp0 to %~dp0.

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

    Read more: How to get the path of a batch script without the trailing backslash in a single command?