0

I know this question might have already been discussed a thousands of times but I dont know what am I doing wrong!

I have the following bat file script that tries to perform a memory dump based on PID of all the current processes matched by the %PROCNAME% variable. It seem to perform what I need upto the point where I echo the %DUMFILENAME%. Here it prints the PID of the previous iteration's PID variable ...

E.g. If I have 3 notepad instances (with PIDs 1, 2, 3) running and I run the following bat. I get following output on command prompt:

"Current PID is 1"

C:\Documents and Settings\userme\Local Settings\Application Data\notepad\notepad.exe_Thu 03222012204843.02.dmp

"Current PID is 2"

C:\Documents and Settings\userme\Local Settings\Application Data\notepad\notepad.exe_1_Thu 03222012204843.02.dmp

"Current PID is 3"

C:\Documents and Settings\userme\Local Settings\Application Data\notepad\notepad.exe_2_Thu 03222012204843.02.dmp

My bat script:

@echo off
SETLOCAL EnableDelayedExpansion
SET PROCNAME=notepad.exe
FOR /F "tokens=1 delims=," %%A in ('tasklist /FI "IMAGENAME eq %PROCNAME%" /FO LIST') DO (
      set "maintoken=%%A"
      Call:InnerToken !maintoken!)
pause
goto :end
:InnerToken
set innertoken=%1
IF "%innertoken%"=="PID:" (
      FOR /F "tokens=1,2 delims=:" %%B in ("%innertoken%") DO (
         set "res=%~n2"
         echo "Current PID is !res!"
         SET "DUMPFILENAME=%USERPROFILE%\Local Settings\Application Data\Notepad\%PROCNAME%_!res!_%date:/=%%time::=%.dmp"
         echo %DUMPFILENAME%
         )
      )
:end

Any help would be greatly appreciated...

1 Answer 1

2

Use

echo !DUMPFILENAME!

instead.

Which surprises me though, as you seem to already know that you need delayed expansion when setting and using variables in blocks.

Sign up to request clarification or add additional context in comments.

2 Comments

So does that mean when I use a a delayed expanded variable like !res! to create another local variable such as DUMPFILENAME I am supposed treat DUMPFILENAME also as a delayed expansion variable? I really didnt know this.
cmd isn't a spreadsheet and won't track where your variables got its values. Delayed expansion is necessary because variables are expanded when parsing a statement (which, if it is or includes a block, counts as a single statement).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.