Solved

VBS to run all exe in specified directory

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

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
The viewer will learn how to implement Singleton Design Pattern in Java.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

758 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

23 Experts available now in Live!

Get 1:1 Help Now