views:

22

answers:

2

Inside a batch file on Windows I would like some variable to have the output of dir /b command.

How this can be achieved ?

+1  A: 

Batch files didn't handle this use case very well. I did find one thread that describes a technique using temporary files.

Jason R. Coombs
Thanks a lot !!
Misha Moroshko
A: 

On Windows, there's a better facility that comes pre-installed. Its called vbscript (and later there is Powershell ). Why don't you use vbscript instead.

strFolder="c:\test"
Set objFS = CreateObject( "Scripting.FileSystemObject" )
Set objFolder = objFS.GetFolder(strFolder)
s=""
For Each strFile In objFolder.Files
    s=s & strFile & vbCrLf
Next
WScript.Echo s

The variable s now contains a list of files (equivalent to dir ). And if you want to store each filename into arrays, its also possible. (cmd.exe does not have arrays etc)

ghostdog74