cwc70
asked on
Pass a VB Script variable to a batch file
To start off I really know very little about VB. I compiled this code from Google searches, but I am now stuck. I want to run a VB program that looks at a specific folder and counts the files within that folder. If the file count is <50 I want the program to call a batch file and send the “File Count” as a variable. I got everything to work, but the variable send. The batch file just runs blat to email me the File Count. I want to pass the vriable IntCount to the batch file. When I run the script I get an error "The system can not find the specified file". If I remove
" & IntCount,1" the program run fine (but I dont get the variable passed).
The code is:
' VBScript
Option Explicit
Dim fs ' variable declared outside a procedure (this is a global variable)
' this is hold a reference to the file system object
' create an instance
Set fs = CreateObject("scripting.fi lesystemob ject")
' count files in windows directory
CountFiles ("\\TestServer\TOTIFF")
' takes a string argument containing the name of the directory
' returns an integer contiang the nubmer of files in that direcrectory
' and all sub directories
Function CountFiles (ByVal StrFolder)
Dim ParentFld
Dim SubFld
Dim IntCount
DIM Var1
Var1 = "Hello"
' note the use of the fs global variable
Set ParentFld = fs.GetFolder (StrFolder)
' count the number of files in the current directory
IntCount = ParentFld.Files.Count
For Each SubFld In ParentFld.SubFolders
' count all files in each subfolder - recursion point
IntCount = IntCount + CountFiles(SubFld.Path)
Next
' return counted files
CountFiles = IntCount
If IntCount < 50 Then
MsgBox IntCount
Dim objShell
Set objShell = CreateObject("WScript.Shel l")
objShell.Run "C:\FileCount\filecount.ba t" & IntCount,1
Set objshell = Nothing
Else
End If
End Function
" & IntCount,1" the program run fine (but I dont get the variable passed).
The code is:
' VBScript
Option Explicit
Dim fs ' variable declared outside a procedure (this is a global variable)
' this is hold a reference to the file system object
' create an instance
Set fs = CreateObject("scripting.fi
' count files in windows directory
CountFiles ("\\TestServer\TOTIFF")
' takes a string argument containing the name of the directory
' returns an integer contiang the nubmer of files in that direcrectory
' and all sub directories
Function CountFiles (ByVal StrFolder)
Dim ParentFld
Dim SubFld
Dim IntCount
DIM Var1
Var1 = "Hello"
' note the use of the fs global variable
Set ParentFld = fs.GetFolder (StrFolder)
' count the number of files in the current directory
IntCount = ParentFld.Files.Count
For Each SubFld In ParentFld.SubFolders
' count all files in each subfolder - recursion point
IntCount = IntCount + CountFiles(SubFld.Path)
Next
' return counted files
CountFiles = IntCount
If IntCount < 50 Then
MsgBox IntCount
Dim objShell
Set objShell = CreateObject("WScript.Shel
objShell.Run "C:\FileCount\filecount.ba
Set objshell = Nothing
Else
End If
End Function
Hi
Do you need a space at the end of the file name?
I suspect that you are getting this output
C:\FileCount\filecount.bat 123
Instead of
C:\FileCount\filecount.bat 123
Jawa29
Do you need a space at the end of the file name?
I suspect that you are getting this output
C:\FileCount\filecount.bat
Instead of
C:\FileCount\filecount.bat
Jawa29
ASKER
I copied your text into the program and I received the same error.
Try this:
objShell.Run "%COMSPEC% /C C:\FileCount\filecount.bat " & IntCount,1
~bo
objShell.Run "%COMSPEC% /C C:\FileCount\filecount.bat
~bo
I did a simple test here without the comspec, but with the space, and it worked fine. Are you sure you put the space in?
What does your BAT file look like, I suspect there is logic in it that is doing something different when a parm is passed and trying to execute a program or command that is then generating the error.
~bp
What does your BAT file look like, I suspect there is logic in it that is doing something different when a parm is passed and trying to execute a program or command that is then generating the error.
~bp
Copied your entire script and set a batch file up with a simple echo statement and it all worked.
ASKER
The VB program now runs with out errors. The batch file screen opens and closes quickly and it does not email. If I run the Batch by itself it works fine.
Here is the batch:
@echo off
set program_blat=C:\InowFileCo unt\blat
cd %program_blat%
blat.exe -f admin@ummc.org -To chad@ummc.org -subject "Sentry FTP" -server mail1.ummc.org:25 -body "test"
Here is the batch:
@echo off
set program_blat=C:\InowFileCo
cd %program_blat%
blat.exe -f admin@ummc.org -To chad@ummc.org -subject "Sentry FTP" -server mail1.ummc.org:25 -body "test"
Add a line like this to the end of the BAT file and test it from the VBS script again. This way it will keep the window open until you press a key and you can look for any errors.
PAUSE
~bp
PAUSE
~bp
ASKER
It's is not even calling the batch file. I made a new batch file and tested it.
@Echo hello
pause
I pointed the script to call this new BAT file and the script runs I still see that cmd window open and close really quick and that is it. The window I see must not be the BAT window.
@Echo hello
pause
I pointed the script to call this new BAT file and the script runs I still see that cmd window open and close really quick and that is it. The window I see must not be the BAT window.
Is this the line you now are using?
objShell.Run "C:\FileCount\filecount.ba t " & IntCount,1
~bp
objShell.Run "C:\FileCount\filecount.ba
~bp
ASKER
THis is it:
objShell.Run "%COMSPEC% /C C:\FileCount\hello.bat " & IntCount,1
objShell.Run "%COMSPEC% /C C:\FileCount\hello.bat " & IntCount,1
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Things are good - It worked perfectly. Thanks for your help.
ASKER
THanks
Welcome, glad we sorted that out.
~bp
~bp
objShell.Run "C:\FileCount\filecount.ba
~bp