Dir to a variable?

July 31, 2010 at 01:35:43
Specs: Windows XP
Hello, I have this issue with the set command i tried
set test=dir /b
echo %test%

But it says "dir /b" instead of what I wanted.

dir /b >blah.txt
set /p test=<blah.txt

above works but involves creating a text file! I'd rather not have to create a texr file, or download any fancy program.

if any body knows a way that'd be great.
on another note, do directories have a extension?


See More: Dir to a variable?

Report •


#1
July 31, 2010 at 04:05:01
What are you trying to achieve? Do you want the entire output of dir /b in a variable? AFAIK environment variables are restricted to one line.

Try this:

@echo off
cls
setlocal enabledelayedexpansion

for /f "tokens=*" %%1 in ('dir /b') do (
    set var=!var! %%1
)

echo %var%

Directories generally do not have extensions but there is nothing to prevent a directory being created with an extension.


Report •

#2
August 1, 2010 at 02:31:36
well that was pretty much what i was looking for. I keep forgetting about for loops! I'm rooting out any doc files that are in the main folder, or a sub folder and copying them to a new directory, without their subfolders. then I delete the originals and keep the other resources in the old folders.

I can do all that now. Thank you!

Edit:

Now I cant get the variables to work
I did another test and it turns out all but one variable turns up as an empty string!

@echo off
cls
setlocal enabledelayedexpansion

set path=%~dp0
echo %path%
set #=%path%
set length=0
:loop
if defined # (set #=%#:~1%&set /A length += 1&goto loop)

set a=1
for /f "tokens=*" %%1 in ('dir /b /s') do (
    set var%a%=%%1
    if [!var%a%:~-4!]==[.mp3] (echo !var%a%:~%length%! & set /a a+=1) 
    if [!var%a%:~-4!]==[.wav] (echo !var%a%:~%length%! & set /a a+=1)
)
pause
cls
set b=1
:loop2
echo !var%b%!
set /a b +=1
if [%b%]==[%a%] goto endloop2
goto loop2
:endloop2
pause


Report •

#3
August 1, 2010 at 05:46:59
Try changing
set var%a%=%%1

to
set var!a!=%%1

When your only tool is a hammer, every problem looks like a nail.


Report •

Related Solutions

#4
August 1, 2010 at 17:48:49
Odd, apparently that saves the variables but causes the for loop to echo the same variable over and over. I just did this

    set var2%a%=%%1
    set var!a!=%%1

and it out putted var2 correctly. but var one had all the variables saved in it! weird.

Thanks guys.


Report •

#5
August 2, 2010 at 04:21:55
Unfortunately, the other references to var%a% also need to include the delayed expanded variables, including those in the IF statements. I say "unfortunately" because to construct the variable, you do need a mix of both non-expanded and expanded variables to do it. In other words, the easy way of doing it would be to use !var!a!!, which absolutely does not work. I get around the problem by using CALL:

[...]
set a=1
for /f "tokens=*" %%1 in ('dir /b /s') do (
    set var!a!=%%1
    CALL :Var_Test !a!
)

[...]

EXIT /B

:Var_Test
SET var_num=%1
if [!var%var_num%:~-4!]==[.mp3] (echo !var%var_num%:~%length%! & set /a a+=1) 
if [!var%var_num%:~-4!]==[.wav] (echo !var%var_num%:~%length%! & set /a a+=1)
GOTO :EOF

In the :Var_Test subroutine, the delayed expanded value of !a! is passed as the variable %1. The inclusion of "SET var_num=%1" is probably not needed, but I tend to use descriptive variable names when I can to help describe the intended logic I'm trying to use. I also avoid using numbers as (user) variables whenever possible. I didn't notice that the FOR loop was using the number 1 as it's variable name until pasting the above snippet, initially thinking it was lowercase L. From FOR's help:

 7:17:45>help for
Runs a specified command for each file in a set of files.

FOR %variable IN (set) DO command [command-parameters]

  %variable  Specifies a single letter replaceable parameter.

When your only tool is a hammer, every problem looks like a nail.


Report •

Ask Question