set output of dir /b as variables

March 5, 2009 at 03:59:41
Specs: Windows XP
hi

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
)

except it doesnt work..

can anyone help?

Cheers
Alastair


See More: set output of dir /b as variables

Report •


#1
March 5, 2009 at 07:22:16
set TASK_!LOOP!=%%a


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#2
March 5, 2009 at 07:31:54
um.... nope.....still no joy

at least %TASK_0%, %TASK_1% etc dont exist as variables..


Report •

#3
March 5, 2009 at 07:41:48
I dunno sed. Try it with just dir/b.


=====================================
If at first you don't succeed, you're about average.

M2


Report •

Related Solutions

#4
March 5, 2009 at 07:46:42
IGNORE THIS POST _ LOOK AT THE NEXT ONE


C:\Documents and Settings\USER>"C:\Documents and Settings\USER\Desktop\3.cmd"

C:\Documents and Settings\USER>setlocal ENABLEDELAYEDEXPANSION

C:\Documents and Settings\USER>set LOOP=0

C:\Documents and Settings\USER>for /F "delims=" %a in ('dir /b c:\test') do (
set TASK_!LOOP!=%a
set /a LOOP +=1
)

C:\Documents and Settings\USER>(
set TASK_!LOOP!=test1.txt
set /a LOOP +=1
)

C:\Documents and Settings\USER>echo %TASK_0%
%TASK_0%

C:\Documents and Settings\USER>echo %LOOP%
%LOOP%

C:\Documents and Settings\USER>(
set TASK_!LOOP!=test2.txt
set /a LOOP +=1
)

C:\Documents and Settings\USER>(
set TASK_!LOOP!=test3.txt
set /a LOOP +=1
)


Report •

#5
March 5, 2009 at 07:47:26
C:\Documents and Settings\USER>"C:\Documents and Settings\USER\Desktop\3.cmd"

C:\Documents and Settings\USER>setlocal ENABLEDELAYEDEXPANSION

C:\Documents and Settings\USER>set LOOP=0

C:\Documents and Settings\USER>for /F "delims=" %a in ('dir /b c:\test') do (
set TASK_!LOOP!=%a
set /a LOOP +=1
)

C:\Documents and Settings\USER>(
set TASK_!LOOP!=test1.txt
set /a LOOP +=1
)

C:\Documents and Settings\USER>(
set TASK_!LOOP!=test2.txt
set /a LOOP +=1
)

C:\Documents and Settings\USER>(
set TASK_!LOOP!=test3.txt
set /a LOOP +=1
)

C:\Documents and Settings\USER>echo %TASK_0%
%TASK_0%

C:\Documents and Settings\USER>echo %LOOP%
%LOOP%


Report •

#6
March 5, 2009 at 10:26:31
The problem is that you want to use a variable inside a loop. This requires delayed expansion. To enable delayed expansion, you have to use the SETLOCAL command. Unfortunately, that has an unwanted side-effect in your case. It makes all variables local to the batch file, and does not export them to the outside.

You want your batch file to create and export new variables. So you can't use the SETLOCAL command. You need to use a different method. Try this (untested).

rem NO setlocal ENABLEDELAYEDEXPANSION
set LOOP=0

for /f "delims=" %%a in ('dir /b c:\windows\tasks\*.job^|sed -e "s/\(.*\)....$/\1/g"') do (
   call set TASK_%%LOOP%%=%%a
   set /a LOOP +=1
)


Report •

#7
March 5, 2009 at 13:40:45
Thanks for your efforts but......

I beleive that the CALL is the way forward, but it still doesnt work..

Cheers
Alastair


Report •

#8
March 5, 2009 at 14:30:01
I've just copied & pasted the code exactly as it is shown above, and it does indeed work.

One possibility is that you tried my first version, which had

call set TASK_%LOOP%=%%a

which I spotted straight after posting and immediately changed to

call set TASK_%%LOOP%%=%%a

so the old incorrect version existed on this page for about 30 seconds and you may have got there before I had corrected it.

The other thing is, make absolutely sure that you do NOT have setlocal enabledelayed expansion at all, anywhere in your batch file.


Report •


Ask Question