Affect the result of a command in a variable

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Gabriel31
Posts: 1
Joined: 07 Apr 2008 08:20

Affect the result of a command in a variable

#1 Post by Gabriel31 » 07 Apr 2008 08:26

Hi All,

I am a really big noob in batch : I was wondering how to put the result of a command in a variable

for example :
DIR /A-D /B %PACKERINPUTPATH%

returns me a file list that I would like to put in a variable name MYFILES

I know how to do it in X shell :

MYFILES=`ls $PACKERINPUTPATH`

But just can't find the equivalent in Batch script :(

Help me please and sorry for the noob question

Gabriel

jaffamuffin
Posts: 40
Joined: 25 Jan 2008 14:05

#2 Post by jaffamuffin » 07 Apr 2008 17:51

Is the out put of that command one-line or multiline?

Code: Select all


for /f "delims=" %%a in ('DIR /A-D /B %PACKERINPUTPATH%') do set MYFILES=%%a


will work for a one liner not sure about multi.

If it's a dir listing with mutli lines, usually best to write to a temp file like

Code: Select all

DIR /A-D /B %PACKERINPUTPATH% > %temp%\temp1.tmp


and read it back with a for loop:

Code: Select all

for /f "delims=" %%a in (%temp%\temp1.tmp) do (
echo Value=%%a
)

Post Reply