Computing.Net > Forums > Programming > set output of dir /b as variables

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

set output of dir /b as variables

Reply to Message Icon

Name: alkjones
Date: March 5, 2009 at 03:59:41 Pacific
OS: Windows XP
Subcategory: Batch
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: March 5, 2009 at 07:22:16 Pacific
Reply:

set TASK_!LOOP!=%%a


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

M2


0

Response Number 2
Name: alkjones
Date: March 5, 2009 at 07:31:54 Pacific
Reply:

um.... nope.....still no joy

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


0

Response Number 3
Name: Mechanix2Go
Date: March 5, 2009 at 07:41:48 Pacific
Reply:

I dunno sed. Try it with just dir/b.


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

M2


0

Response Number 4
Name: alkjones
Date: March 5, 2009 at 07:46:42 Pacific
Reply:

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
)


0

Response Number 5
Name: alkjones
Date: March 5, 2009 at 07:47:26 Pacific
Reply:

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%


0

Related Posts

See More



Response Number 6
Name: klint
Date: March 5, 2009 at 10:26:31 Pacific
Reply:

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
)


0

Response Number 7
Name: alkjones
Date: March 5, 2009 at 13:40:45 Pacific
Reply:

Thanks for your efforts but......

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

Cheers
Alastair


0

Response Number 8
Name: klint
Date: March 5, 2009 at 14:30:01 Pacific
Reply:

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.


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: set output of dir /b as variables

Set date/time of log file as variable www.computing.net/answers/programming/set-datetime-of-log-file-as-variable/20059.html

make param = dir /b /s www.computing.net/answers/programming/make-param-dir-b-s/12558.html

Monitor Log file useing DOC BATCH www.computing.net/answers/programming/monitor-log-file-useing-doc-batch-/16455.html