批处理脚本在Windows中保存正在运行的进程的目录

有没有办法将正在运行的进程的目录保存到一个variables? 说我想保存mysqld.exe的完整path,这是c:\ program files \ mysql \ mysql服务器5.6 \ bin ,这个批处理脚本应该search一个进程目录,并保存它没有\ processName.exe ,它是可行的吗?

你在找什么

set "MYSQLDIR=c:\program files\mysql\mysql server 5.6\bin" 

更新:这有点挑剔,你需要使用PowerShell的路径,但这似乎对我来说:

 @echo off set "tmpname=%TMP%\fproc%RANDOM%" powershell get-process ^| format-list path | findstr %1 > %TMPNAME% set /p T1=<%TMPNAME% call :get_path result "%T1:~7%" echo %result% goto:cleanup :get_path set "%~1=%~dp2" exit /b :cleanup del %TMPNAME% 

将其保存到名为procpath.bat的文件并传递进程名称:

 c:\srv\tmp> procpath pycharm64 C:\Program Files (x86)\JetBrains\PyCharm 2016.3.2\bin\ 
 @ECHO OFF SETLOCAL :: set name of executable to locate. :: This one onmy system - adjust to suit SET "targetname=boinc.exe" CALL :zapvars FOR /f "tokens=1*delims==" %%a IN ('wmic process list full') DO IF "%%b" neq "" ( IF "%%a"=="CommandLine" CALL :report SET "$%%a=%%b" ) CALL :report GOTO :EOF :report IF NOT DEFINED $processid GOTO zapvars IF NOT DEFINED $executablepath GOTO zapvars :: select your target executablename here SET $executablepath|FINDSTR /i /L /e /c:"\\%targetname%" >NUL IF ERRORLEVEL 1 GOTO zapvars FOR %%z IN ("%$executablepath%") DO SET "pathwithslosh=%%~dpz ECHO PATH without \executablename = "%pathwithslosh:~0,-1%" :zapvars FOR %%z IN ( CommandLine CSName Description ExecutablePath ExecutionState Handle HandleCount InstallDate coreelModeTime MaximumWorkingSetSize MinimumWorkingSetSize Name OSName OtherOperationCount OtherTransferCount PageFaults PageFileUsage ParentProcessId PeakPageFileUsage PeakVirtualSize PeakWorkingSetSize Priority PrivatePageCount ProcessId QuotaNonPagedPoolUsage QuotaPagedPoolUsage QuotaPeakNonPagedPoolUsage QuotaPeakPagedPoolUsage ReadOperationCount ReadTransferCount SessionId Status TerminationDate ThreadCount UserModeTime VirtualSize WindowsVersion WorkingSetSize WriteOperationCount WriteTransferCount ) DO SET "$%%z=" GOTO :eof 

使用wmic和解析 – 可以获得许多过程特征