Solved

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

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

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 74

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
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.
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 …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

708 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

15 Experts available now in Live!

Get 1:1 Help Now