Solved

VBS to run all exe in specified directory

Posted on 2006-06-28
5
542 Views
Last Modified: 2008-01-09
I am trying to make a script that will run all the exe files in a specified directory. The script I have so far seems to run the bat files randomly. For testing I set up a folder with a.bat, b.bat and c.bat where a.bat runs notepad, b.bat runs calc, and c.bat runs appwiz.cpl. What happens seems totally randomly to me, sometimes calc runs, sometimes appwiz, sometimes notepad. I don't think I have seen all of them start like they are supposed to. What am I doing wrong? Here is my script:

Dim fso, startFolder
Dim WshShell, oExec
Dim folder, file, fileCollection

Set WshShell = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")

startFolder = "C:\test"

Set folder = fso.GetFolder(startFolder)
Set fileCollection = folder.Files
For Each file In fileCollection
   If UCase(Right(file.Name, 4)) = ".BAT" Then
      Set oExec = WshShell.Exec(file.name)
   End If
Next


I will probably make this into an exe in vb6, so if anybody already knows how to do this in vb6 it would save me time.
0
Comment
Question by:rilliam
5 Comments
 
LVL 35

Expert Comment

by:YZlat
ID: 17004369
your code should work in VB6
0
 
LVL 29

Expert Comment

by:leonstryker
ID: 17013383
>What happens seems totally randomly to me,

What order do you want to run them in?

Leon
0
 

Author Comment

by:rilliam
ID: 17019709
doesn't matter to me, alphabetical I guess. Whatever is easiest.

Yes, it does kind of work in vb6...usually it just runs and does nothing.
0
 
LVL 29

Expert Comment

by:leonstryker
ID: 17019743
>doesn't matter to me, alphabetical I guess. Whatever is easiest.

Easiest would be to let it run as it does now, letting it choose the order. To sort them you would have to read the names into an array, sort them and then pass in each one at  a time to run.
0
 
LVL 4

Accepted Solution

by:
SmithJW earned 500 total points
ID: 17081402
I have edited your script, this should run all the .bat files it finds in your specified folder, but not subfolders.

Dim fso, startFolder, WshShell
Dim folder, file, fileCollection
Dim filename

Set WshShell = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")

startFolder = "C:\test"

Set folder = fso.GetFolder(startFolder)
Set fileCollection = folder.Files
For Each file In fileCollection
   If UCase(Right(file.Name, 4)) = ".BAT" Then
      filename = StartFolder + "\" + file.name
      WshShell.run(filename)
   End If
Next

The limitation here is that the .bat files must not contain any spaces in their filenames.
I dont know how to fix this though... maybe someone more familiar with the syntax can help with that.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How To Loop - Python 19 97
WPF issue with Trigger 2 129
Zip Folders Using Chilkat Routines 1 71
Work with App store 7 89
Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
This article will show, step by step, how to integrate R code into a R Sweave document
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 …
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

735 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