Solved

How do I execute a DIR command from within a VBScript file?

Posted on 2009-04-15
4
4,869 Views
Last Modified: 2012-06-27
I have a .VBS file that I am running.  I need to be able to execute a DIR command which lists a number of directories to a text file before moving on with the script and using said text files for other functions.  I can confirm that the DIR command works from a command prompt.  Here is the code I have currently which doesn't create the file:
SourceRoot = "c:\program files"
strFileType = ".txt"
FileListName = "c:\myfile.txt"
 
FindFiles SourceRoot, strFileType, FileListName
Set oFSO = CreateObject("Scripting.FileSystemObject")
if oFSO.FileExists(FileListName) then
  WScript.Echo "File is there"
else
  WScript.Echo "File is not there"
end if
 
Sub FindFiles(BaseRoot, FileType, DestFile)
Dim sCommand
 
 sCommand = "cmd /c  dir/s/b '"&BaseRoot&"*"&FileType&"' > '"&DestFile&"'"
 oShell.Run(sCommand),0,True
 
End Sub

Open in new window

0
Comment
Question by:electricd7
  • 2
4 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24147665
Off the bat, I see a missing space in line 16:
sCommand = "cmd /c  dir /s /b '"&BaseRoot&"*"&FileType&"' > '"&DestFile&"'"

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24147687
Due to the way you declared SourceRoot, this may be more appropriate:
sCommand = "cmd /c  dir /s /b '"&BaseRoot&"\*"&FileType&"' > '"&DestFile&"'"

Open in new window

0
 
LVL 10

Expert Comment

by:TakedaT
ID: 24147696
You can use the shell run method for that.  After you create the file, you can then use your choice of vbscript method to read the file into your script.  

Set objShell = CreateObject("WScript.Shell")

makefile=objShell.run("cmd /c your dir command>c:\yourtextfile.txt",1,true)

0
 

Accepted Solution

by:
electricd7 earned 0 total points
ID: 24147704
Sorry I retyped it rather than copy/paste.  The error was using the ' instead of using """ to put in quotes.  Sorry for wasting your time.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This is an explanation of a simple data model to help parse a JSON feed
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question