Solved

VBS to run all exe in specified directory

Posted on 2006-06-28
5
558 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: 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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
This video teaches viewers about errors in exception handling.
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 …

726 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