Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This is about my first experience with programming Arduino.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
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 …

867 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now