|
Answer» Ello
so. im doing dir /b on a folder, shaping the output a little with sed and setting each item in the folder as a separately numbered variable like this:
setlocal ENABLEDELAYEDEXPANSION set LOOP=0
for /f "delims=" %%a in ('dir /b c:\windows\tasks\*.job^|sed -e "s/\(.*\)....$/\1/g"') do ( set TASK_%LOOP%=%%a set /a LOOP +=1 )
only it doesnt work, and niether does this:
setlocal ENABLEDELAYEDEXPANSION set LOOP=0
for /f "delims=" %%a in ('dir /b c:\windows\tasks\*.job^|sed -e "s/\(.*\)....$/\1/g"') do ( set TASK_!LOOP!=%%a set /a LOOP +=1 )
Anyone help me with this?
Cheers AlastairYou enabled DELAYED expansion, but in the first script you didn't use it (%LOOP%). So %LOOP% is always going to be 0
In the second you got it right (!LOOP!) but, in any case, you need to set the variable in a child PROCESS (because of the way cmd.exe expands variables)
Code: [Select]call set TASK_!LOOP!=%%a Then your simulated array, the variables TASK_0 through TASK_whatever, will be set and visible to the batch file while it is running.
A small point: your SED one liner appears to strip off the final 3 chars of the extension but leaves the dot. (the dot is considered part of the extension)
If you did not wish this, you could dump SED altogether and just use the FOR variable modifier ~n
(type FOR /? at the PROMPT for full details of all the MODIFIERS and how to use them)
e.g.
Code: [Select]for /f "delims=" %%a in ('dir /b c:\windows\tasks\*.job') do ( call set TASK_!LOOP!=%%~na set /a LOOP+=1 )
Or if you did want to keep the dot,
Code: [Select]call set TASK_!LOOP!=%%a. BTW, I am guessing that maybe you are coming to Windows scripting from a *nix background? You may like to know that Windows command stuff is case insensitive, (%LOOP% and %loop% and %LooP% are all considered the same variable)
hey
Am I missing something?
C:\Documents and Settings\Administrator>setlocal ENABLEDELAYEDEXPANSION
C:\Documents and Settings\Administrator>set LOOP=0
C:\Documents and Settings\Administrator>for /F %a in ('dir /b c:\test') do ( call set TASK_!LOOP!=%a set /a LOOP+=1 )
C:\Documents and Settings\Administrator>( call set TASK_!LOOP!=test1.txt set /a LOOP+=1 )
C:\Documents and Settings\Administrator>( call set TASK_!LOOP!=test2.txt set /a LOOP+=1 )
C:\Documents and Settings\Administrator>( call set TASK_!LOOP!=test3.txt set /a LOOP+=1 )
C:\Documents and Settings\Administrator>echo %TASK_0% %TASK_0%
C:\Documents and Settings\Administrator>echo %TASK_1% %TASK_1%
C:\Documents and Settings\Administrator>Quote from: alkjones on March 05, 2009, 02:32:45 PM hey
Am I missing something? Looks like you're trying to expand the variables at the prompt after the batch has finished. No can do.
From my post
Quote from: MeThen your simulated array, the variables TASK_0 through TASK_whatever, will be set and visible to the batch file while it is running. Code: [Select]@echo off setlocal ENABLEDELAYEDEXPANSION set LOOP=0 for /f "delims=" %%a in ('dir /b') do ( call set TASK_!LOOP!=%%a set /a LOOP+=1 )
echo use SET without args to list variables set | find "TASK_" echo. echo Expand some of them echo var0=%TASK_0% echo var1=%TASK_1% echo var2=%TASK_2% echo var3=%TASK_3% echo var4=%TASK_4%
Code: [Select]S:\Test\Batch\After 06-02-09\looptest>looptest3 use SET without args to list variables TASK_0=looptest1.bat TASK_1=looptest2.bat TASK_10=pr.exe TASK_2=looptest3.bat TASK_3=mvdir.exe TASK_4=nl.exe TASK_5=od.exe TASK_6=paste.exe TASK_7=patch.exe TASK_8=pathchk.exe TASK_9=pclip.exe
Expand some of them var0=looptest1.bat var1=looptest2.bat var2=looptest3.bat var3=mvdir.exe var4=nl.exe But after it has finished...
Code: [Select]S:\Test\Batch\After 06-02-09\looptest>echo %TASK_0% %TASK_0%
that - frankly - is extremely sexy.
mate: Beers owed
Alastair.
(re *nix - no, but i like to keep things nice and tidy)You might want to set a variable (e.g. TASK_MAX) equal to the highest suffix number created...
|