title: Nested for loop with if statement in batch file
body: <p>I know this question might have already been discussed a thousands of times but I dont know what am I doing wrong!</p>
  
  <p>I have the following bat file script that tries to perform a memory dump based on <code>PID</code> of all the current processes matched by the <code>%PROCNAME%</code> variable. It seem to perform what I need upto the point where I echo the <code>%DUMFILENAME%</code>. Here it prints the <code>PID</code> of the previous iteration's <code>PID</code> variable ...</p>
  
  <p>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:</p>
  
  <pre><code>"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
  </code></pre>
  
  <p><strong>My bat script:</strong></p>
  
  <pre><code>@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
  </code></pre>
  
  <p>Any help would be greatly appreciated...</p>