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

时间:2017-08-27 14:31:12

标签: windows batch-file cmd

有没有办法将运行进程的目录保存到变量?说我要保存mysqld.exe的完整路径,这是 c:\ program files \ mysql \ mysql server 5.6 \ bin ,这个批处理脚本应该搜索进程目录并保存而不用 \ processName.exe ,可行吗?

2 个答案:

答案 0 :(得分:1)

您在寻找

吗?
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\

答案 1 :(得分:0)

@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 KernelModeTime 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和解析 - 可以获得流程特征的许多部分