2017-06-02 7 views
3

こんにちは私は自分のPIDをバットスクリプトから取得する方法を見つけようとしています。 私はこれを見つけた:実行中のバットスクリプトからPIDを取得する

"cmd.exe","15084","RDP-Tcp#5","2","2,768 K","Running","MEDIASERVER\Administrator 
","0:00:00","Administrator: =mycmd" 

は、どのように私は私のバットスクリプト内の変数にPID番号を取得します:出力

title=mycmd 
tasklist /v /fo csv | findstr /i "mycmd" 

助けていただければ幸いです。

答えて

3

ので、タイトルを変更する必要はありません、getcmdpidで試してみてください。

call getCmdPID.bat 
echo %errorlevel% 

あなたは出力を処理するループのために必要がありますタスクリストでそれを行うには:

title mycmd 
for /f "tokens=2 delims=," %%a in (
    'tasklist /v /fo csv ^| findstr /i "mycmd"' 
) do (
    set "mypid=%%~a" 
) 
echo %mypid% 

もチェックこのスレッド: http://www.dostips.com/forum/viewtopic.php?t=6133

+0

声援、完全に:) – StrayanDropbear

4
@echo off 
    setlocal enableextensions disabledelayedexpansion 

    rem Prepare a temporary file reference where to send the wmic output 
    for %%t in ("%temp%\%~nx0.%random%%random%%random%%random%%random%.tmp") do > "%%~ft" (

     rem Call wmic to retrieve its own parent process ID, that is, our cmd instance 
     wmic process where "name='wmic.exe' and commandline like '%%_%random%%random%%random%_%%'" get parentprocessid 

     rem Read the PID from the generated temporary file 
     for /f "skip=1" %%a in ('type "%%~ft"') do set "processID=%%a" 

     rem And remove the temporary file 
    ) & 2>nul del /q "%%~ft" 

    echo %processID% 
関連する問題