FINDREPL is "working" for me at work, but not at home. I thought I did the same process at home, but apparantly. I'll check again later.
But even when it "works" - it doesn't
I modified Aacini's script to wait until it receives a signal (existence of a file).
Code: Select all
@echo off
setlocal
set "source=wmiCollection('Win32_Process','CommandLine','ParentProcessID')"
set "search=%source:(=\(%"
set "search=%search:)=\)%"
:loop
if not exist go.txt goto :loop
for /F "usebackq tokens=4 delims=," %%a in (`FindRepl "/S:%source%" =search /J`) do set "PID=%%~a" & goto continue
:continue
echo %PID%
I then ran the modified script in 3 different cmd.exe console sessions. They both just sit there, waiting for the GO signal. Finally, in a fourth cmd.exe session, I create the signal using COPY NUL GO.TXT
Obviously I delete GO.TXT and start over when I want to repeat the test.
Aacini's "solution" simply doesn't work. All three sessions reported the same PID, except on some runs, one of the sessions failed entirely with one of two error messages:
D:\test\FindRepl.bat(318, 1) Microsoft JScript runtime error: Permission denied
or
D:\test\FindRepl.bat(322, 1) Microsoft JScript runtime error: Input past end of file
I used the same technique with my code, and it works flawlessly - each session reported its true unique PID:
Code: Select all
@echo off
:getPID [rtnVar]
setlocal disableDelayedExpansion
set "time="
:loop
if not exist go.txt goto :loop
:getLock
set "lock=%temp%\%~nx0.%time::=.%.lock"
set "uid=%lock:\=:b%"
set "uid=%uid:,=:c%"
set "uid=%uid:_=:u%"
set "uid=%uid:'=:q%"
2>nul ( 9>"%lock%" (
for /f "skip=1 delims=" %%A in (
'wmic process where "name='cmd.exe' and CommandLine like '%%<%uid%>%%'" get ParentProcessID'
) do for /f "delims=" %%B in ("%%A") do set "PID=%%B"
(call )
))||goto :getLock
del "%lock%" 2>nul
endlocal & if "%~1" equ "" (echo(%PID%) else set "%~1=%PID%"
exit /b
Siberia-man's PowerShell solution also works, as does npocmaka's/foxidrive's jscript.net solution.
Aacini's JScript solution fails (all 3 sessions report the same PID). This is not surprising given Aacini's statement:
Aacini wrote:I think this JScript method (WSH+WMI) should work well in all cases (excepting when two instances of the same program run in the same few milliseconds that the program takes to end).
One problem is that WMI is relatively slow. The script takes ~0.17 seconds, not a few miliseconds. But even if it were miliseconds, the technique should still be more robust.
Dave Benham