Issue error checking a batch file run via VBScript - windows

I am unsure why InstallResult always returns a 1 in my VBScript. I have put an echo in my batch file to confirm if I delete the source file before a copy it returns a 4 and that it returns nothing if it is successful. Any help would be appreciated. My files should do the following:
Copy a script from a network share to the local machine.
Run a batch file to install office (currently some test code for error checking). The batch file should run and post an error code on exit or a 0 if successful.
Go back to the VBScript to error check and run another cleanup VBScript.
Here is my code:
Run install bat (VBScript)
Dim objshell, InstallResult
Dim FSO
Set FSO = CreateObject("Scripting.FileSystemObject")
FSO.CopyFile "\\altirisdata\AssetMgmt\Tools\WSM\DeleteOffice13Package.vbs", "C:\source\DeleteOffice13Package.vbs"
'DeleteMS2013FilePath = objShell.run ("c:\source\DeleteOffice13Package.vbs", 0, True)
WScript.Sleep 3000
Set objShell = WScript.CreateObject("WScript.Shell")
InstallResult = objShell.run ("cscript.exe C:\source\Microsoft_Office_2013_01\install.bat", 0, True)
WScript.Echo InstallResult
If InstallResult <> 0 Then WScript.Echo "Unable to install Microsoft Office 2013. Please manually check the install results"
If InstallResult = 0 Then
DeleteDelScript = objShell.Run("cscript.exe c:\source\DeleteOffice13Package.vbs", 0, True)
End If
If DeleteDelScript = 0 Then
FSO.DeleteFile("C:\source\DeleteOffice13Package.vbs")
End If
Set FSO = nothing
WScript.Quit
install.bat
#echo off
xcopy "C:\source\test again\test.txt" "C:\Temp\Temp1\TempTest" /y
if %errorlevel% neq 0 (
exit /b %errorlevel%
)
exit

You get a return value of 1, because you're trying to run a batch script with a VBScript interpreter:
InstallResult = objShell.run ("cscript.exe C:\sourc...l.bat", 0, True)
Remove cscript.exe from the commandline, or replace it with %COMSPEC% /c:
InstallResult = objShell.run ("%COMSPEC% /c C:\sourc...l.bat", 0, True)
As a side note, you shouldn't need a condition in your batch script. Simply returning the errorlevel should suffice:
#echo off
xcopy "C:\source\test again\test.txt" "C:\Temp\Temp1\TempTest" /y
exit /b %errorlevel%

Related

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 ""/""")

corresponding commands from batch file to vbsript

I trying to go from cmd script to vbscript in MS window xp
cmd code yes works
set home_=%~dp0
set part001=part001
set part002=part002
set part003=part003
set part004=part004
::get the dir in part001
for /f "delims=" %%A in ('dir /s/b/o:n/a:d ^"%home%%part001%\^"') do (
echo show have dir path
echo %%A
pause
)
echo to the end
pause
goto :eof
to vbscript
the part I do not know to convert are those that are foramtted as cmd{cmd codeing}
dim strHome as strimg =cmd{[%~dp0]}
dim strPart001 as sting = part001
dim strPart002 as sting = part002
dim strPart003 as sting = part003
dim strPart004 as sting = part004
'get the dir in part001
Dim objFSo, objFile
Set objFSo = CreateObject("Scripting.FileSystemObject")
set objDirPart001list = objFS.getfolder(strHome&strPart001\)
set subDirPart001list = objDirPart001list.SubFolders
for each subDirPart001Name in subDirPart001list
WScript.Echo show dir path
WScript.Echo part
cmd{pause}
)
cmd{pause}
what are the corresponding vbscript commands for:
%~dp0
pause
The %~dp0 is a so-called magic command but more technically this method is known as variable substition. The %n variables are used to reference the command line parameters of the script. The most common, %0 will return the full path to the script that is executing. The d and p are special modifiers that will return the drive and path portion of that path, respectively. There is also n which returns the filename portion as well as others. These modifiers can be combined as necessary. So the %~dp0 command will return the full drive and path to the directory where the executing script resides. To do this in VBScript, you can use any of the following that rely on the WScript Object's ScriptFullName method:
Set objShell = CreateObject("WScript.Shell")
Set objFso = CreateObject("Scripting.FileSystemObject")
strPath = objFso.GetParentFolderName(WScript.ScriptFullName)
Or
strPath = Left(WScript.ScriptFullName, Len(WScript.ScriptFullName) - Len(WScript.ScriptName))
Or my favorite:
Replace(WScript.ScriptFullName, WScript.ScriptName, "")
The pause command is used to stop the command interpreter and prompt the user to press any key to continue. This is typically done so that the user has time to read the information in the command window before it closes. Here's a subroutine to do that in CScript. (For WScript, you would just use a simple MessageBox.)
Sub Pause
WScript.StdOut.Write "Press any key to continue . . . "
WScript.StdIn.Read(1)
End Sub

Windows BAT : test if a specific file is empty

I would like to check if a specific file is empty in a windows .bat file. Here is my non working script :
set dir="C:\test"
set file="%dir%\fff.txt"
cd %dir%
if %file%%~zi == 0 exit
ftp -s:"%dir%\ftp.action"
exit
Could you help me debug this please ?
Or try it with
#echo off
set "dir=C:\temp"
set "file=%dir%\a.txt"
call :CheckEmpty "%file%"
goto :eof
:CheckEmpty
if %~z1 == 0 exit
ftp -s:"%dir%\ftp.action"
goto :eof
The main difference is that I use a function call and use the %~z1, as the modifiers only works for paramters like %1, %2..%9 or for-loop parameters like %%a ...
batch solution using file compare:
type nul > blank
fc myfile blank > nul
if errorlevel 1 echo myfile is not empty
Try this:
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("c:\boot.ini", ForReading)
Dim arrFileLines()
i = 0
Do Until objFile.AtEndOfStream
Redim Preserve arrFileLines(i)
arrFileLines(i) = objFile.ReadLine
i = i + 1
Loop
objFile.Close

VBS Script - Run series of .batch jobs

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

How to set environment variables in vbs that can be read in calling batch script

I have a batch file that calls a vbscript file. I am trying to have the vbscript file change an environment variable that is later used in the batch file that calls the vbscript file.
Here are snippetes from the files.
Parent.bat
Set Value="Initial Value"
cscript Child.vbs
ECHO Value = %VALUE%
Child.vbs
Set wshShell = CreateObject( "WScript.Shell" )
Set wshSystemEnv = wshShell.Environment( "Process" )
wshSystemEnv("VALUE") = "New Value"
You can't. A process can pass environment variables to child processes, but not to its parent - and in this case the parent is cmd.exe, which is running your Parent.bat file.
There are of course other ways to communicate information back to the parent batch file - outputting to stdout or a file is an obvious way, e.g.
== Child.vbs ===
WScript.echo "New Value"
== Parent.cmd ===
for /f "tokens=*" %%i in ('cscript //nologo child.vbs') do set Value=%%i
echo %Value%
yes, you can.... however, you'll have to resetvars in your session. see the following link:
Is there a command to refresh environment variables from the command prompt in Windows?
'RESETVARS.vbs
Set oShell = WScript.CreateObject("WScript.Shell")
filename = oShell.ExpandEnvironmentStrings("%TEMP%\resetvars.bat")
Set objFileSystem = CreateObject("Scripting.fileSystemObject")
Set oFile = objFileSystem.CreateTextFile(filename, TRUE)
set oEnv=oShell.Environment("System")
for each sitem in oEnv
oFile.WriteLine("SET " & sitem)
next
path = oEnv("PATH")
set oEnv=oShell.Environment("User")
for each sitem in oEnv
oFile.WriteLine("SET " & sitem)
next
path = path & ";" & oEnv("PATH")
oFile.WriteLine("SET PATH=" & path)
oFile.Close
This is how I did it:
SET oShell = CREATEOBJECT("Wscript.Shell")
dim varSet
SET varSet = NOTHING
SET varSet = oShell.Environment("SYSTEM")
varSet("WinVer") = "6.0.2008"
Then in a separate VB script (resetvars.vbs) I called from CMD script:
cscript //nologo \\%APPSERVER%\apps\IE9.0\restartvars.vbs
call %TEMP%\resetvars.bat
I don't think you can do this. At least, you would need to mess with the environment block in the calling process, and there's no guarantee that it will respect this...
Ho about this:
#echo off
set vbsFile=%temp%\createguid.vbs
call :CreateVbs
call :GetGuid NewGuid
echo.%NewGuid%
del %vbsFile%>nul
GOTO:EOF
:CreateVbs
echo.set obj = CreateObject("Scriptlet.TypeLib")>%vbsFile%
echo.WScript.StdOut.WriteLine obj.GUID>>%vbsFile%
GOTO:EOF
:GetGuid
for /f "tokens=*" %%i in ('cscript //nologo %vbsFile%') do set %1=%%i
GOTO:EOF
It is not pure batch script but works ok.
#echo off&color 4a&title %~n0&AT>NUL
IF %ERRORLEVEL% EQU 0 (
goto 2
) ELSE (
echo.
)
if not "%minimized%"=="" goto 1
set minimized=true & start /min cmd /C "%~dpnx0"&cls&exit
:1
wmic process where name="cmd.exe" CALL setpriority "realtime">nul&echo set shell=CreateObject("Shell.Application") > %~n0.vbs&echo shell.ShellExecute "%~dpnx0",,"%CD%", "runas", 1 >> %~n0.vbs&echo set shell=nothing >> %~n0.vbs&start %~n0.vbs /realtime&timeout 1 /NOBREAK>nul& del /Q %~n0.vbs&cls&exit
:2
echo %~dpnx0 admin mode look up&wmic process where name="cmd.exe" CALL setpriority "realtime"&timeout 3 /NOBREAK>nul
:3
echo x=msgbox("end of line" ,48, "%~n0") > %~n0.vbs&start %~n0.vbs /realtime&timeout 1 /NOBREAK>nul& del /Q %~n0.vbs&cls&exit

Resources