|
Answer» 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?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
GrahamThanks a lot. It worked.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.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' )
|