Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Run an Exe with Specific commands through all files in folder

Status
Not open for further replies.

udaynag

Programmer
Aug 21, 2007
7
US
Hi,

I am new to VB script..I want to write a script to run a exe through all files in a folder..I have searched the forum and found some help on working with all files in a folder, but not anything that relates ro running an application through all files in folder..

This is what I need to do..I have a software which needs two input files(one fixed reference and other varies through all files in a folder) and a few commandline options.I want to run this software or exe through all files in a folder automatically. I am able to open the software using the script , but not able to run anything..would be great if Somebody can help me.. Fortunately, I have a batch file(.bat) that comes with the software to run the software with just one file..So, as an alternate option I can use this batch file and all I have to do is just write a script that has a FOR loop to run this batch file through all files in a folder..

If anybody has clue on how to do this..Please help me..If you think you have something and dont understand completely, please post your suggestion and I will reply..

Thanks
Uday
 
post your batch so people can see what you are trying to accomplish.

You can easily enumerate files in a folder by binding to the folder and using Folder.Files property.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Thank you Mark for the reply.

here is the content of the batch file that I am using

opera -Exec -Algorithm Name=PESQ -Input Inp=0 File=%RefFileName% Inp=1 File=%TestFileName% -Mux InpRefLeft=0 ChannelRefLeft=0 InpTestLeft=1 ChannelTestLeft=0 DCFilterOn -Out "output.xls" -Append

These are some of the commands I want to run the batch file , Filenames vary and the rest of the commands remain the same for all runs.. I am not sure how to specify the run command in vbscript so that it can take the commands and filenames automatically..

Thanks in advance for the help
Uday
 
uh OK great. how about identifying the aspects of that command now. What do you need to automate to be changed?

A run command is simple:

Set WSHShell = CreateObject("Wscript.Shell")
WSHShell.Run CommandToExecute

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Hi Mark,

Thanks again for the reply..I found some posts in the forum which helped me gain some understanding of vbscripts and now I have a script which does what the batch file does..atleast run the script for one file.Now, I am using the following code to run the bath file from the VBScript

Dim RefFileName
Set WshShell = Wscript.CreateObject("WScript.Shell")
Set FSO = CreateObject("Scripting.FileSystemObject")
strDir = "C:\Programme\Opera\Batch"
RefFileName="DefaultRefFileMixed_16kLin_400msSilenceatEnd.wav"
Set objDir = FSO.GetFolder(strDir)
getInfo(objDir)

Sub getInfo(pCurrentDir)

For Each aItem In pCurrentDir.Files
strFile = (aItem.Name)
if right(strFile, 3) = "wav" then
WshShell.Run "C:\Programme\Opera\Batch\RunPesq.bat RefFileName strFile output.xls"
'WshShell.Run "C:\Programme\Opera\opera.exe"
end if
Next

End Sub

The problem with this code is that the variables RefFileName and strFile are passed to the application as themselves and not with the specific filenames of the files present in the folder(like AudioRef.wav etc) ..I tried using quotes,% etc but none of them worked..

Thanks in advance
Uday
 
replace RefFileName with aItem.Name

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Thanks Mark..but I still have the same problem

Thanks in advance
Udaynag
 
I am afraid what you need is not being communicated. What do you need to replace and with data from where?

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
I apologize for the miscommunications..This is what I want to achieve..I have the following code currnetly..

Dim RefFileName
Set WshShell = Wscript.CreateObject("WScript.Shell")
Set FSO = CreateObject("Scripting.FileSystemObject")
strDir = "C:\Programme\Opera\Batch"
RefFileName="DefaultRefFileMixed_16kLin_400msSilenceatEnd.wav"
Set objDir = FSO.GetFolder(strDir)
getInfo(objDir)

Sub getInfo(pCurrentDir)

For Each aItem In pCurrentDir.Files
strFile = (aItem.Name)
if right(strFile, 3) = "wav" then
WshShell.Run "C:\Programme\Opera\Batch\RunPesq.bat RefFileName strFile output.xls"
'WshShell.Run "C:\Programme\Opera\opera.exe"
end if
Next

End Sub

When I run this code, this is the error message I get..
'can't open RefFileName' , which means all that is passed to the software ( OPERA in my case) is not the actual file name but the string 'RefFileName', which it tries to find and is not able to do so..I am not sure if this is the way my script has to be..but any other suggestion on a different method or any other scripting tool which can do my work easily would be great.. But I need to get this done so that I can automate the tedious work I am doing right now with lots of files...

Thanks in advance
Udaynag
 
You're not concatenating the string with the variables properly...should be something like this.

WshShell.Run "C:\Programme\Opera\Batch\RunPesq.bat " & RefFileName & " " & strFile & " output.xls"

Though I don't know for sure by looking at your example.

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Thanks Dm4ever,

My code works now..however, I dont understand how the quotes work in vbscript..the more I look into examples on web, the more I get confused..Now, I wanted to use the script to get the command line arguments RefFileName and strFile from a folder with spaces rather than the current working folder(which doesnot have spaces and hence worked)..after some work, I am able to write a code which I thought should work..but, unfortunately, the script just runs and doesnot even give an error..Normally the software OPERA takes around 2 mins to process the input files, so I am sure that my code is wrong somewhere ,but I dont know where it goes wrong because it doesnot give any error.. Here is the code I wrote

Dim RefFileName
Set WshShell = Wscript.CreateObject("WScript.Shell")
Set FSO = CreateObject("Scripting.FileSystemObject")
strDir = "C:\TEMP\Uday\Mono HATS Testing\Audio Files\LX260V0a\BH200\New\"
RefFileName="DefaultRefFileMixed_16kLin_400msSilenceatEnd.wav"
RefFileName = strDir & RefFileName
Set objDir = FSO.GetFolder(strDir)
getInfo(objDir)

Sub getInfo(pCurrentDir)

For Each aItem In pCurrentDir.Files
strFile = strDir & (aItem.Name)

if right(strFile, 3) = "wav" then
WshShell.Run "C:\Programme\Opera\Batch\RunPesq.bat "" & RefFileName & "" "" & strFile & "" output.xls"
end if
Next

End Sub

Also, is there any compiler for vbscript that is more interactive ..currently, I am running my script from command line using cscript, but the code is going to get messy soon when I go for final implementation..any suggestions would be great.

Thanks in advance
Udaynag
 
Quotes are used to indicate a string value. If you are using a variable, then you don't wrap that within quotes.

Here is an example:
Code:
digits = "5"
Wscript.Echo "You have " & digits & " fingers on your hand"

Output: You have 5 fingers on your hand


Using the ampersand (&) to glue the text and variable values together is called concatenation.

Compare this to the same example without the quotes to break up the string and allow the variable to be replaced by its value.

Code:
digits = "5"
Wscript.Echo "You have digits fingers on your hand"

Output: You have digits fingers on your hand

Make sense?


I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Thanks Marc. I was able to work on the code further and got some good knowledge of vb scripts..Now I have the following code which saves all the filenames in a folder into a text file and retrieves them back to run the batch file through them.

Dim FSO
Dim ObjStream
Dim contents
Dim RefFileName
Const TristateFalse = 0
Const FILE_LOCATION = "C:\"
Const FILE_NAME = "Temp.txt"
Const ForReading = 1
RefFileName="DefaultRefFileMixed_16kLin_400msSilenceatEnd.wav"
strDir = "C:\Programme\Opera\BT500\"
Set WshShell = Wscript.CreateObject("WScript.Shell")
set Fso = CreateObject("Scripting.FilesystemObject")
objStartFolder = "C:\Programme\Opera\BT500"
Set objFolder = FSO.GetFolder(objStartFolder)
set objStream = FSO.CreateTextFile(FILE_LOCATION & FILE_NAME, True, TristateFalse)
Set colFiles = objFolder.Files

For Each objFile in colFiles

objStream.WriteLine objFile.Name
'objStream.Close
Next

set readfile = FSO.OpenTextFile("c:\Temp.txt", 1, false)
MsgBox "File " & FILE_NAME & " was Created Successfully."

Set objTextFile = FSO.OpenTextFile _
("c:\Temp.txt", ForReading)

Do Until objTextFile.AtEndOfStream
strNextLine = objTextFile.Readline
arrFileList = Split(strNextLine , ",")
'Wscript.Echo "File name: " & arrFileList(0)

strFile = arrFileList(0)
wscript.Echo strFile
WshShell.Run "C:\Programme\Opera\Batch\RunPesq.bat " & RefFileName & " " & strFile & " output.xls"

Loop

I thought I got everything correct until I found out that the application is not able to open the file with the parameter strFile. i.e, I have a folder with file LX260V0a_BH200_Call1_Tr1__BTLevelMax_DefaultRefFileMixed_DL , but the code is not able to pass this filename inside the variable strFile. But, When I do wscript.Echo strFile , it echoes the correct filename on the command line. I tried to change the filename assuming there is a problem with underscores, but it still didnot work..

Any help would be appreciated..

Thanks in advance..

 
Why run the bat file? Convert it to vbscript.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top