获取另一个进程的当前工作目录


3

我有一个PID,我正在尝试确定该进程的当前工作目录是什么。我玩wmic process了一段时间,但似乎没有可用的cwd。有人知道我能做到这一点的聪明方法吗?



1
两个给定的答案似乎都需要下载一个单独的实用程序(tlist或句柄)。我真的希望内置一个,所以解决方案是可移植的。
kbjr

Answers:


1

您可以尝试使用此批处理脚本执行管理员权限:

@echo off
Mode 75,8 & color 0A
Title Get ExecutablePath of any PID by Hackoo 2018
:::::::::::::::::::::::::::::::::::::::::
:: Automatically check & get admin rights
:::::::::::::::::::::::::::::::::::::::::
REM  --> Check for permissions
Reg query "HKU\S-1-5-19\Environment" >nul 2>&1
REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
Echo.
ECHO                 **************************************
ECHO                  Running Admin shell... Please wait...
ECHO                 **************************************

    goto UACPrompt
) else ( goto gotAdmin )

:UACPrompt
    echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
    set params = %*:"=""
    echo UAC.ShellExecute "cmd.exe", "/c ""%~s0"" %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"

    "%temp%\getadmin.vbs"
    del "%temp%\getadmin.vbs"
    exit /B

:gotAdmin
::::::::::::::::::::::::::::
::START
::::::::::::::::::::::::::::
:Loop
Cls
echo( 
set /P "PID=Please provide a PID number to get its ExecutablePath : "
set "ExePath="
SetLocal EnableDelayedExpansion
for /f "skip=1 delims=" %%a in ('"wmic process where "ProcessID^=!PID!" get ExecutablePath 2^>nul"') do (
    If Not "!errorlevel!" equ "1" (
        for /f "delims=" %%b in ("%%a") do if not defined ExePath set "ExePath=%%b"
        Rem To trim a variable ( Removing Spaces into a variable )
        set "ExePath=!ExePath: =!"
    )
)

echo ExecutablePath ==^> "!ExePath!"
for %%a in ("!ExePath!") do set "CWD=%%~dpa"
echo Current Working Directory ==^> "!cwd!"
echo(
echo Hit any key to choose another PID & pause>nul & goto Loop

这是令人印象深刻的批处理脚本,但OP正在寻找一个进程的工作目录,而不是它的可执行文件路径。
mklement0
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.