Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

VBS to run all exe in specified directory

Posted on 2006-06-28
5
Medium Priority
?
606 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 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
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.
Suggested Courses

636 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