Solved

VBS to run all exe in specified directory

Posted on 2006-06-28
5
530 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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Making a javascript parser in javascript language 3 73
while loop over for loop 7 104
How To Loop - Python 19 87
Not seen Link button 5 39
Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
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 …

809 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