VBS Script - Run series of .batch jobs - vbscript

Help me run a series of .bat script
they are located like so:
p:\Co-Brand\export.bat
p:\Generic\export.bat
p:\Tri-Brand\export.bat
Thanks in advance,
Best regards,
Joe

Would a simple shell command do? You can call this from a command prompt:
for /R %F in (*.bat) do "%F"
or the following from a .bat file:
for /R %%F in (*.bat) do call "%%F"

found a way that works, should have tried this first of all.
I am a bit embarrassed that it was this easy actually:
cd P:\Co-Brand\
CALL Export.bat
cd P:\Generic\
CALL Export.bat
cd P:\TriBrand\
CALL Export.bat
cd P:\UBA\
CALL Export.bat

As originally asked, here is a VBScript solution...
The problem described is probably related to the "Script-Working-Directory".
Try this ...
Dim objShell
Dim blnWaitOnReturn
Dim strOriginalCD
Dim strCmd
Dim intWindowStyle
Dim intExitCode
Set objShell = WScript.CreateObject("Wscript.Shell")
'' if necessary, save the original "Script-Working-Directory"
strOriginalCD = objShell.CurrentDirectory
intWindowStyle = 1
blnWaitOnReturn = True
objShell.CurrentDirectory = "p:\Co-Brand\"
strCmd = "%comspec% /K export.bat"
intExitCode = objShell.Run(strCmd, intWindowStyle, blnWaitOnReturn)
objShell.CurrentDirectory = "p:\Generic\"
strCmd = "%comspec% /K export.bat"
intExitCode = objShell.Run(strCmd, intWindowStyle, blnWaitOnReturn)
objShell.CurrentDirectory = "p:\Tri-Brand\"
strCmd = "%comspec% /K export.bat"
intExitCode = objShell.Run(strCmd, intWindowStyle, blnWaitOnReturn)
'' if necessary, restore the original "Script-Working-Directory"
objShell.CurrentDirectory = strOriginalCD
Notes:
'' If filename contains spaces make sure to add double-quotes around filename
strCmd = "%comspec% /K " & Chr(34) & "File name with spaces.bat" & Chr(34)
'' To run the commands in a "Hidden" window, use:
intWindowStyle = 0
'' To run the commands "Minimized", use:
intWindowStyle = 7
More info on "objShell.Run" can be found here: http://ss64.com/vb/run.html
The above examples will cause VBScript to wait for each called ".bat" to complete and return an "ExitCode" before proceeding.
If you don't want VBScript to wait for one ".bat" to complete before proceeding to the next then set blnWaitOnReturn = False, and remove intExitCode like:
...
blnWaitOnReturn = False
objShell.CurrentDirectory = "p:\Co-Brand\"
strCmd = "%comspec% /K export.bat"
objShell.Run strCmd, intWindowStyle, blnWaitOnReturn
objShell.CurrentDirectory = "p:\Generic\"
strCmd = "%comspec% /K export.bat"
objShell.Run strCmd, intWindowStyle, blnWaitOnReturn
objShell.CurrentDirectory = "p:\Tri-Brand\"
strCmd = "%comspec% /K export.bat"
objShell.Run strCmd, intWindowStyle, blnWaitOnReturn
...
If you want the ability to get the "Status" and "ProcessID", and access the standard streams of the executable to read/write to the process's stdout/stderr in real-time while the process executes, then use "objShell.Exec".
More info on "objShell.Exec" can be found here: http://ss64.com/vb/exec.html

Related

Inserting a variable in wsh.run command string

I want to have my VBScript input variable used in my
makefile = objShell.run (""""""cmd /K CD "strFolderName" & dir /-n > "strFolderName"\wk3result.txt"""""",1,True) command.
I've tried cmd /K cd '"strFolderName"'…
I get directory not found so I do not believe my variables strFolder name is being put into the cmd line code correctly.
Here is my script:
Const cTitle = "WK3 Assignment"
Dim objFSO, objFolder
Dim strFolderName, objShell
strFolderName = InputBox("Which Folder", cTitle)
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFolder = objFSO.GetFolder(strFolderName)
If objFSO.FolderExists(strFolderName) Then
WScript.Echo "Folder is there"
Else
WScript.Echo "Folder is not there"
End If
Set objShell = WScript.CreateObject("WScript.shell")
makefile = objShell.run (""""""cmd /K CD "strFolderName" & dir /-n > "strFolderName"\wk3result.txt"""""",1,True)
expecting to get a txt file with the dir results of the inputed folder
You are missing some & around your strFolderName variable in your makefile command string. It should look like this:
"cmd /K CD """ & strFolderName & """ dir /-n > """ & strFolderName & "\wk3result.txt"""
Entire command:
makefile = objShell.run ("cmd /K CD """ & strFolderName & """ dir /-n > """ & strFolderName & "\wk3result.txt""", 1, True)
Better yet, make a variable of your command in case you need to view it for troubleshooting:
Dim strCommand
strCommand = "cmd /K CD """ & strFolderName & """ dir /-n > """ & strFolderName & "\wk3result.txt"""
makefile = objShell.run(strCommand, 1, True)
This way you can check what the command is with MsgBox strCommand.

vbs run program with objShell.run

I have the following vbscript that fail with an error on last line
Option Explicit
Dim objShell
Dim strComputer, strCmd , strVar
strComputer = "."
Set objShell = CreateObject("WScript.Shell")
' strVar = objShell.ExpandEnvironmentStrings("%ProgramFiles(x86)%")
' strCmd = strVar & "\CaptureBites\Express\Programs\AutoBites\Autobites.exe"
' wscript.echo strCmd
objShell.Run "taskkill /im Autobites.exe",,True
WScript.Sleep(5000)
objShell.run """%ProgramFiles(x86)%""" &"\CaptureBites\Express\Programs\AutoBites\Autobites.exe"
The problem is that script open the folder %ProgramFiles(x86)% but don't run the exe autobites.exe
Can you help me to debug it
The whole file spec has to be quoted, not only the first part:
objShell.run """%ProgramFiles(x86)%" &"\CaptureBites\Express\Programs\AutoBites\Autobites.exe"""

Run a command and capture the output in vbscript

I am trying to run the following command and return the output of it using VBscript:
dir /A-d "C:\Windows\Minidump" | find /c "/"
And I have the following script but it does not work (probably because of " charachters:
Wscript.Echo runCMD("dir /A-d "C:\Windows\Minidump" | find /c "/"")
Function runCMD(strRunCmd)
Set objShell = WScript.CreateObject("WScript.Shell")
Set objExec = objShell.Exec(strRunCmd)
strOut = ""
Do While Not objExec.StdOut.AtEndOfStream
strOut = strOut & objExec.StdOut.ReadLine()
Loop
Set objShell = Nothing
Set objExec = Nothing
runCMD = strOut
End Function
Any suggestions on how to achieve this?
dir is intrinsic; you need %comspec%.
Double quotes need to be escaped by double double quotes in VBScript:
Wscript.Echo runCMD("%comspec% /c dir /A-d ""C:\Windows\Minidump"" | find /c ""/""")

Run a vbs script from another vbs script and redirect right away output to a file

Running a vbscript from another vbscript, Is it possible to get right away an output to a file like this one:
dim shell
set shell=createobject("wscript.shell")
strCMD =replace("'myvbs.vbs' '"&a_parameter&"' ","'","""")
shell.Run strCMD >output.txt
Thank in advance
Yes, you can. Try this example.
'script1.vbs -----------------
Dim oShell, strCMD
Set oShell = CreateObject("WScript.Shell")
strCMD = Replace("CMD /C CScript.exe 'script2.vbs' " & 3, "'", """")
oShell.Run strCMD & " //NoLogo >output.txt", 0, True
'script2.vbs -----------------
result = WScript.Arguments(0) ^ 2
WScript.StdOut.Write result
Take a look at this answer too.

set permissions with a vbs script

I have a VBS script that downloads a file on login and places it in a given folder, It works brilliantly in some places but in others it falls over because the file was created by user1 and user2 can't overwrite it.
How would i give the group "Everyone" full control of a given file using a VBS script?
Something like this:
Set WshShell = CreateObject("WScript.Shell")
strFile = "c:\test_folder\test_file.txt"
setPerms = "%COMSPEC% /c echo Y| C:\windows\system32\cacls.exe " & Chr(34) & strFile & Chr(34) & " /G domain\everyone:F"
wscript.echo setPerms
WshShell.run setPerms
Partially gleaned from here:
http://social.technet.microsoft.com/forums/en-us/ITCG/thread/6CDA091A-6B3D-4F58-8374-9A46F59F389A
One way of doing it would be to use the CACLS command line tool. Just run it from your script using Shell.Run.
Here's another link to information about how to use CACLS that has some samples.
Function giveFullPermissionToFolder(strFolder)
Dim objShell, strCmd, intRunError
Set objShell = CreateObject("Wscript.Shell")
strCmd = "%comspec% /c echo Y| cacls " & strFolder & " /T /E /C /G Users:F"
intRunError = objShell.Run(strCmd, 2, True)
If intRunError<>0 Then
Reporter.ReportEvent micFail, "giveFullPermissionToFolder" , "Unable to give full permission to " & strFolder
End If
Set objShell=Nothing
End Function

Resources