calling CMD with VBS hangs at echo - vbscript

im exierience a strange problem.
im trying to call a cmd with a vbs script and it hangs at the uttermost strange point.
heres the oExecShell.StdOut.ReadLine of my cmd that hangs :
set targetDir=\\Backupstorage\Servername$\2014-11-28_12-00
if not exist \\Backupstorage\Servername$\2014-11-28_12-00 (mkdir \\Backupstorage\Servername$\2014-11-28_12-00 )
Rem Works Fine
set LogDir=\\\Backupstorage\Servername$\2014-11-28_12-00\Logs
Rem Does not work for some strange Reason
echo "here"
"here"
and then it hangs, the line that kills it/should follow is :
echo %LogDir%
echo "we have the problem"
anybody has an?
heres some of the code that starts the cmd :
strAction = "C:\makeBackup_test.cmd"
Dim WshShell : Set WshShell = CreateObject("WScript.Shell")
strAction = Wshshell.expandenvironmentstrings(strAction)
Dim oExecShell : Set oExecShell = WshShell.Exec(strAction)
Do While oExecShell.Status = 0
WScript.Sleep 100
Loop
writelog("... ExitCode is: " & oExecShell.ExitCode)
LastShellOutput = ""
Do While not(oExecShell.StdOut.AtEndOfStream)
LastShellOutput = LastShellOutput & oExecShell.StdOut.ReadLine & vbCrlf
Loop
writelog("StdOut: " & LastShellOutput)
anyone has an idea?

Calling int with CMD /C solved it. (strAction = "CMD /C C:\makeBackup_test.cmd")

Related

execute multiple batch file from VBscript

I am trying to execute multiple batch files in a folder using vbscript. can anyone help me how to do it.
Here is my code.
Varr1 = hostname
UN = username
password = pass
set ObjFSO = createobject("Scripting.FileSystemObject")
set FilePath = ObjFSO.getfolder("C:\test\script")
set BatFile = FilePath.files
for each m in BatFile
If LCase(objFSO.GetExtension(FilePath.files)) = "bat" Then
Set WShell = CreateObject("WScript.Shell")
WShell.Run ("CMD /K C:\test\script "&BatFile &" " & Varr1 &" "& UN &" "& password )
End If
Next
Given .BAT files like:
#echo off
echo a, $1, $2
in the current directory, a .VBS like:
Option Explicit
Const u = "user"
Const p = "passw"
Dim goFS : Set goFS = CreateObject("Scripting.FileSystemObject")
Dim goWS : Set goWS = CreateObject("WScript.Shell")
Dim f, c
For Each f In goFS.GetFolder(".\").Files
If "bat" = goFS.GetExtensionName(f.Name) Then
c = Join(Array("%comspec%", "/K", f.Name, u, p))
WScript.Echo "will call", c
goWS.Run c
End If
Next
will execute all of them in new consoles.
output:
cscript 47609016.vbs
will call %comspec% /K b.bat user passw
will call %comspec% /K a.bat user passw
(and some windows containing something like "a 'user' 'passw'")

VBScript script as a wrapper for a silent Batch file

I have a batch file that gets some parameters from the command line and returns some values into STDOUT.
I want the batch file to be "silent" (such that the console is not shown), and found out probably the only possibility of using vbs script.
I used this thread for implementing the argument forwarding to the batch file in VBS.
Then, I used the following command for calling the batch file I wrapped:
CreateObject("WScript.Shell").Run batchFilePath & " " & Trim(arglist), 0, False
It turns out that my batch file does run, but its STDOUT is discarded somewhere, and does not make its way back to whom called the VBS script. I.e. the batch file's STDOUT is not redirected into the VBS script's STDOUT.
How can I make the batch file's STDOUT being redirected to the VBS script STDOUT, such that if I start the VBS script from some shell, the batch file's output will be printed to the shell too?
Use Exec instead of Run, like this:
set objShell = CreateObject( "WScript.Shell" )
cmd = "echo Hello World!"
' Run the process
set objRes = objShell.Exec( "cmd /c """ & cmd & """" )
' Wait for the child process to finish
Do While objRes.Status = 0
WScript.Sleep 100
Loop
' Show whatever it printed to its standard output
Wscript.Echo "The output was:" & vbNewLine & objRes.StdOut.ReadAll()
Try this...
Intreturn = WshShell.Run("cmd /c " & path& " " & args & ">c:\batchoutput.txt", 0, true)
Set fso = CreateObject("Scripting.FileSystemObject")
Set objfile = fso.OpenTextFile("c:\batchoutput.txt", 1)
text = objfile.ReadAll
Objfile.Close
Or try this...
Set WshShell = WScript.CreateObject("WScript.Shell")
Set objexc = WshShell.Exec("cmd /c " & command and args) 'replace command and args with proper variables
strOutputText = ""
While Not objexc.StdOut.AtEndOfStream
strOutputText = strOutputText & objexc.StdOut.ReadLine()
Loop
Msgbox strOutputText
You may need some debugging on this.

How can I redirect my vbscript output to a file using batch file?

I am new to Windows Scripting. I have a simple script for archiving using WinRAR CLI utility. I have to schedule this script using batch file. During archiving there are some errors and I want them to write in a simple text file or at least I can write entire output of archiving in a file. How can I change my code to do this?
Dim MyDate
Dim OutputFile
const WaitUntilFinished = true, DontWaitUntilFinished = false, ShowWindow = 1, DontShowWindow = 0
MyDate = Replace(Date, "/", "-")
OutputFile = "backup-" & mydate & ".rar"
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.CurrentDirectory = "C:\Users\ABC\Desktop\"
objShell.Run "C:\windows\Rar.exe a .\VBScripts\backups\" & OutputFile & " software", ShowWindow, WaitUntilFinished
objShell.Popup "Archiving Completed Successfully!",5, "Scheduled Backup"
Set objShell = Nothing
Batch file is like this;
#echo off
start /wait C:\Users\ABC\Desktop\VBScripts\scheduled_backup.vbs
Change your command line to include redirection to a log file:
logfile = "C:\path\to\your.log"
objShell.Run "%COMSPEC% /c C:\windows\Rar.exe a .\VBScripts\backups\" & _
OutputFile & " software >""" & logfile & """", ShowWindow, WaitUntilFinished
Use this function instead of WScript.Shell.Run:
' Runs an external program and pipes it's output to
' the StdOut and StdErr streams of the current script.
' Returns the exit code of the external program.
Function Run (ByVal cmd)
Dim sh: Set sh = CreateObject("WScript.Shell")
Dim wsx: Set wsx = Sh.Exec(cmd)
If wsx.ProcessID = 0 And wsx.Status = 1 Then
' (The Win98 version of VBScript does not detect WshShell.Exec errors)
Err.Raise vbObjectError,,"WshShell.Exec failed."
End If
Do
Dim Status: Status = wsx.Status
WScript.StdOut.Write wsx.StdOut.ReadAll()
WScript.StdErr.Write wsx.StdErr.ReadAll()
If Status <> 0 Then Exit Do
WScript.Sleep 10
Loop
Run = wsx.ExitCode
End Function
Call script instead of start in your batch and use redirection:
script //nologo C:\Users\ABC\Desktop\VBScripts\scheduled_backup.vbs 2> errors.txt

Get the exit value of batch file using vbscript

Please lend some help, I'm a beginner with batch file and vbscript.
I would be grateful if you could help me solve my problem.
Given:
a:
#echo off
pushd "C:\Program Files\appName" && popd
IF ERRORLEVEL 1 EXIT 0
pushd "C:\Program Files\appName"
IF EXIST application.exe GOTO Installed
popd
EXIT 1
:Installed
EXIT 2
b:
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Set objFile = objFSO.OpenTextFile(listFile, 1)
dim sh , res
objName = objFSO.GetTempName
objTempFile = objName
'---- Sample Code 0 ----------------------'
'set sh = WScript.CreateObject("WScript.shell")
'res = sh.run("fileFullPath",0,true)
'wScript.echo res
'----Sample Code 1 ---------------'
'set sh = WScript.CreateObject("WScript.shell")
'res = sh.run("fileName",0,true)
'wScript.echo res
'---- Sample Code 2 ---------------------
'set sh = WScript.CreateObject("WScript.shell")
'res = sh.run("cmd /c fileName",0,true)
'wScript.echo res
'---- Sample Code 3 ---------------------
'set sh = WScript.CreateObject("WScript.shell")
'res = sh.run("cmd /c fileFullPath",0,true)
'wScript.echo res
The b:sample code 1 & 2 works fine with me but I need the sample code 0 & 3.
with sample code 1 I always get an error : The system cannot find the file selected
,while with sample code 3 it would only return 0 when failed then 1 when successful, and not the returned value of the batch file.
Thanks,
I already got my problem solved just a while ago. I found out that even-though i already have had "cmd /c fileFullPath" double-quoted. When a folder name(s) consists some spaces, it would cause an issue --> file can't be found. Now I'm using "cmd /c ""fileFullPath""" instead.
Sorry for having my first post lack of information. I will do my best to make it clearer next time. Thanks a lot. :D

VB script + automate CLI command by VB script

I write the following VB script in order to run the CLI command - vpnclient.exe
my target is to automate the vpnclcient process and answer “y” when question appears,
I have WIN XP PC
During running the vpnclient.exe in CMD window we get then following question
Do you wish to continue? (y/n):
In my VB I write the “echo y” in order to answer on this question automatically
but question is still stuck in CMD window ,and I cant continue
please advice what chuld be wrong in my code and how to fix it?
MY VB script (vpnclient.exe – exist under VPN directory)
Dim oShell
Set oShell = WScript.CreateObject ("WScript.Shell")
oShell.run "cmd /K CD C:\Program Files\Cisco\VPN & ( echo y | vpnclient.exe connect ""site moon"" )"
Set oShell = Nothing
You can try by creating a file with the commands to be executed on the command line instead of echoing the password.
Here's an example where a text file is created first with the required command and then those commands are invoked from the file.
Public Function FTPDownload(serverName, ftpuser, ftppassword, dirPath, localpath, fileName)
Dim fso, myfile
Set fso = CreateObject("Scripting.FileSystemObject")
'Create FTP.EXE commands file
Set fso = CreateObject("Scripting.FileSystemObject")
Set myfile= fso.OpenTextFile("C:\Regression\Results\ftp_cmd.ini", 2, True)
myfile.WriteLine("open " &serverName )
myfile.WriteLine(ftpuser)
myfile.WriteLine(ftppassword)
myfile.WriteLine("lcd " & localpath )
myfile.WriteLine("cd " & dirPath)
myfile.WriteLine("prompt")
myfile.WriteLine("cr")
myfile.WriteLine("mget *" &fileName &"*" )
myfile.WriteLine("mdelete *" &fileName &"*" )
myfile.WriteLine("close")
myfile.WriteLine("quit")
myfile.Close
'====================The following code executes the FTP script. It creates a Shell object and run FTP program on top of it.===================
Set objShell = CreateObject( "WScript.Shell" )
objShell.Run ("ftp -i -s:" & chr(34) & "C:\Regression\Results\ftp_cmd.ini" & chr(34))
Set objShell = Nothing
End Function

Resources