Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5787
  • Last Modified:

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

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
electricd7
Asked:
electricd7
  • 2
1 Solution
 
käµfm³d 👽Commented:
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
 
käµfm³d 👽Commented:
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
 
TakedaTCommented:
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
 
electricd7Author Commented:
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now