Solved

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

Posted on 2009-04-15
4
4,946 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
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 …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

829 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