reading file in windows batch


  1. Posts : 19
    win7-64 bit
       #1

    reading file in windows batch


    i have a file b.txt with entries like :

    explorer.exe pid: 3136 type: File A24: D:\ftproot\logs\tagswap
    notepad.exe pid: 7732 type: File 18: D:\ftproot\logs\tagswap


    I want to read each record and extract pid and kill it.
    The following is only giving me only one record .

    for /f "tokens=4 delims= " %%G in (c:\temp\b.txt) do (
    SET KILLPIDSTR=%%G
    SET KILLPIDSUBSTR=%KILLPIDSTR:~24,5%
    echo %KILLPIDSUBSTR%
    )
      My Computer


  2. Posts : 2,177
    Windows 8.1 Pro x64
       #2

    Hi,

    Does this help

    Code:
    SETLOCAL ENABLEDELAYEDEXPANSION
    
    for /f %%G in (c:\temp\b.txt) do (
    SET KILLPIDSTR=%%G
    SET KILLPIDSUBSTR=%KILLPIDSTR:~24,5%
    echo %KILLPIDSUBSTR%
    )
    
    Pause
    This is what i get when running the above code, as you can see it returns the name of the programs i expect (i did it against calc.exe and notepad.exe):

    reading file in windows batch-2014-08-18-21_52_23-task-manager.png

    Part of the problem you have is you are setting a variable and then setting another variable based on the 1st one within the same command, when you do this all variables are set at the same time so technically when you try to do the 2nd SET command its blank as the first one isn't set if that makes sense?

    SETLOCAL ENABLEDELAYEDEXPANSION fixes that issue

    Regards,
    Jamie
      My Computer


  3. Posts : 19
    win7-64 bit
    Thread Starter
       #3

    i put your command in but i still dont see the pids assigned to the variable:


    C:\TEMP>for /F %G in (c:\temp\b.txt) do (
    SET KILLPIDSTR=%G
    SET KILLPIDSUBSTR=~24,5
    echo
    )
    C:\TEMP>(
    SET KILLPIDSTR=explorer.exe
    SET KILLPIDSUBSTR=~24,5
    echo
    )
    ECHO is on.
    C:\TEMP>(
    SET KILLPIDSTR=notepad.exe
    SET KILLPIDSUBSTR=~24,5
    echo
    )
    ECHO is on.
    C:\TEMP>
      My Computer


  4. Posts : 2,177
    Windows 8.1 Pro x64
       #4

    Sorry, i see what you mean... i can see you are stripping the output in the 2nd SET command, what do you expect to be echo'ed there?

    Regards,
    Jamie
      My Computer


  5. Posts : 19
    win7-64 bit
    Thread Starter
       #5

    i want the pids to be assigned to variable so
    KILLPIDSUBSTR=3136
    KILLPIDSUBSTR=7732

    thanks
      My Computer


  6. Posts : 2,177
    Windows 8.1 Pro x64
       #6

    Does this do the trick?

    Code:
    SETLOCAL ENABLEDELAYEDEXPANSION
    
    for /f "tokens=3 delims= " %%G in (b.txt) do (
    	SET KILLPIDSTR=%%G
    	echo !KILLPIDSTR!
    )
    
    pause
    reading file in windows batch-2014-08-18-22_58_44-c__windows_system32_cmd.exe.png

    Regards,
    Jamie
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 00:58.
Find Us