Solved

Pass a VB Script variable to a batch file

Posted on 2011-09-29
15
617 Views
Last Modified: 2012-05-12
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.filesystemobject")

' 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.Shell")
   objShell.Run "C:\FileCount\filecount.bat" & IntCount,1
   Set objshell = Nothing  
Else    
End If

End Function
0
Comment
Question by:cwc70
  • 7
  • 6
  • 2
15 Comments
 
LVL 53

Expert Comment

by:Bill Prew
ID: 36815339
You need a space after the BAT file name, before the parm, as in :

   objShell.Run "C:\FileCount\filecount.bat " & IntCount,1

~bp
0
 
LVL 8

Expert Comment

by:jawa29
ID: 36815354
Hi

Do you need a space at the end of the file name?

I suspect that you are getting this output
C:\FileCount\filecount.bat123

Instead of
C:\FileCount\filecount.bat 123

Jawa29
0
 

Author Comment

by:cwc70
ID: 36815364
I copied your text into the program and I received the same error.
0
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 53

Expert Comment

by:Bill Prew
ID: 36815406
Try this:

   objShell.Run "%COMSPEC% /C C:\FileCount\filecount.bat " & IntCount,1

~bo
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 36815477
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
0
 
LVL 8

Expert Comment

by:jawa29
ID: 36815536
Copied your entire script and set a batch file up with a simple echo statement and it all worked.
0
 

Author Comment

by:cwc70
ID: 36815579
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:\InowFileCount\blat

cd %program_blat%
blat.exe -f admin@ummc.org -To chad@ummc.org -subject "Sentry FTP" -server mail1.ummc.org:25 -body "test"
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 36815595
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
0
 

Author Comment

by:cwc70
ID: 36815755
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.  
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 36815774
Is this the line you now are using?

   objShell.Run "C:\FileCount\filecount.bat " & IntCount,1

~bp
0
 

Author Comment

by:cwc70
ID: 36815788
THis is it:
objShell.Run "%COMSPEC% /C C:\FileCount\hello.bat " & IntCount,1
0
 
LVL 53

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 36815820
You shouldn't need the COMSPEC stuff, that isn't needed so let's take that out and go with:

   objShell.Run "C:\FileCount\filecount.bat " & IntCount,1

~bp
0
 

Author Comment

by:cwc70
ID: 36816967
Things are good - It worked perfectly.  Thanks for your help.
0
 

Author Closing Comment

by:cwc70
ID: 36816970
THanks
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 36817166
Welcome, glad we sorted that out.

~bp
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

770 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