Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Getting a variable in FOR loop  (Read 4015 times)

0 Members and 1 Guest are viewing this topic.

abcmno

  • Guest
Getting a variable in FOR loop
« on: October 25, 2006, 03:39:25 AM »
Hi,
I have a FOR loop something like this :
FOR /f %%a in (temp.txt) do set b=%%a

Here, b takes the contents in the file temp.txt. The contents in this file are as follows :

S0000000.LOG
S0000001.LOG
S0000002.LOG
S0000003.LOG
S0000004.LOG
S0000005.LOG
S0000006.LOG
S0000007.LOG
S0000008.LOG

So, b takes all these values. But I want b to take only the number in the filename. So, I modify my FOR loop as follows :

FOR /f %%a in (temp.txt) do set b=%%a set c=%b:~1,7%
but c is taking the value as 0000008 through out the FOR loop, what should I do so as the values 0000001 to 0000008 come in the variable c?

gpl



    Apprentice
  • Thanked: 27
    Re: Getting a variable in FOR loop
    « Reply #1 on: October 25, 2006, 05:16:59 AM »
    The trouble is, its a loop
    That is to say, c get set to each of the values in turn -- the last one to be set is 0000008, so this is the value to be held after the loop exits.

    You really want to call a subroutine for each iteration of the loop and do your processing there for each value :

    FOR /f %%a in (temp.txt) do Call :Subroutine %%a %b:~1,7%  

    GoTo :EOF
    :Subroutine
    Set b=%1
    Set c=%2
    ... rest of your process


    Graham

    abcmno

    • Guest
    Re: Getting a variable in FOR loop
    « Reply #2 on: October 25, 2006, 06:02:47 AM »
    Thanks a lot. It worked.

    GuruGary



      Adviser
      Re: Getting a variable in FOR loop
      « Reply #3 on: October 25, 2006, 10:07:59 PM »
      Or a slightly shorter version:
      Code: [Select]
      setlocal enabledelayedexpansion
      FOR /f %%a in (temp.txt) do (
         set b=%%~na
         set c=!b:~1!
         echo Rest of your process for !c!
         )
      When using setlocal enabledelayedexpansion, you can use ! instead of % to use your variables without having to call a separate function.  For more information you can do "setlocal /?" or "set /?"

      The set b=%%~na tells the batch file to use the filename portion of the %%a without the extension.  So this will work for files that are longer and files that are shorter than the 8.3 examples you had posted since it doesn't rely on just using the first 8 characters.
      « Last Edit: October 25, 2006, 10:13:29 PM by GuruGary »

      DosItHelp



        Intermediate
        Re: Getting a variable in FOR loop
        « Reply #4 on: October 26, 2006, 07:00:59 PM »
        Or filter the number while parsing the file by defining 'S' and '.' as delimitters and capturing the first token:

        for /f "delims=S." %%a in (temp.txt) do (
            echo.Do the rest of your process using '%%a'
        )
        ::)                                                                                                                                      ::)                                                                                                                                      ::)                                                                                                                                      ::)                                                                                                                                      ::)                                                                                                                                      ;D                                                                                                                                      ::)                                                                                                                                      ::)                                                                                                                                      ;D                                                                                                                                      ::)                                                                                                                                      ::)                                                                                                                                      ::)                                                                                                                                      :D