0

Problem 1: My code abruptly stops with no output in console.

Problem 2: Line 5 of my code doesn't capture the process ID when in the code block, but it does as a standalone command. Could this be because of the set command not working in else blocks?

Here's the goal of the code:

  • Check if explorer.exe is running.
  • If more than one instance is running, set a variable to multiple.
  • Else, if no instances are running, set a variables to not found.
  • Else, if one instance is running, dump the memory of explorer.exe.

I expect to grab the process ID of explorer.exe, but the PID is not defined and the program stops at, I believe, line 2. I've inserted pauses after every line of code, and that's where it happens.

for /f "tokens=1,*" %%a in ('tasklist ^| find /I /C "explorer.exe"') do set amount=%%a
if %amount% GTR 1 (
    set multiple-explorer=  - [ALERT] Multiple instances of explorer.exe found.
) else (
    for /F "tokens=1,2" %%i in ('tasklist /FI "IMAGENAME eq explorer.exe" /fo table /nh') do set PID=%%j
    if "%PID%" EQU "No" (
        set explorer=  - [ALERT] explorer.exe not found.
    ) else (
        echo  [#] Dumping memory of explorer.exe...
        strings2.exe -pid %PID% -nh > explorer.txt
    )
)

My goal for this code is also to make it as compact as possible without any goto lines. Any help is appreciated, thanks.

Quizzed
  • 163
  • 1
  • 8
  • 3
    You are setting *and* using `PID` inside an `else` (code block) what requires [delayed expansion](https://ss64.com/nt/delayedexpansion.html). –  Jul 24 '19 at 23:35
  • Thanks, that fixed the problem. – Quizzed Jul 24 '19 at 23:50
  • 1
    Just be aware that `if "%PID%" EQU "No"` will very likely be language dependent! – Compo Jul 25 '19 at 00:17

0 Answers0