If - Else If statement in VBScript - vbscript

Else If exist "K:\ICT project"
(Set WshShell = CreateObject("WScript.Shell")
WshShell.Run Chr(34) & "K:\ICT project" & Chr(34), 0
Set WshShell = Nothing)
Else If exist "F:\ICT project"
(Set WshShell = CreateObject("WScript.Shell")
WshShell.Run Chr(34) & "F:\ICT project" & Chr(34), 0
Set WshShell = Nothing)
Else If exist "E:\ICT project"
(Set WshShell = CreateObject("WScript.Shell")
WshShell.Run Chr(34) & "E:\ICT project" & Chr(34), 0
Set WshShell = Nothing)
Else If exist "D:\ICT project"
(Set WshShell = CreateObject("WScript.Shell")
WshShell.Run Chr(34) & "D:\ICT project" & Chr(34), 0
Set WshShell = Nothing)
Else If exist "C:\ICT project"
(Set WshShell = CreateObject("WScript.Shell")
WshShell.Run Chr(34) & "C:\ICT project" & Chr(34), 0
Set WshShell = Nothing)
What is wrong with the code? And how well the If statement is used? please help me.
How can this code be improved?

To search for a program on different drives and call it (perhaps with arguments and decent quotes), use a loop over the drives:
Option Explicit
Const csPrg = "pipapo"
Function qq(s) : qq = """" & s & """" : End Function
Dim goFS : Set goFS = CreateObject("Scripting.FileSystemObject")
Dim sDL, sFSpec, sCmd
For Each sDL In Split("A:\ C:\ K:\ E:\")
sFSpec = goFS.BuildPath(sDL, csPrg)
sCmd = Join(Array("%comspec% /c", qq(sFSpec), "/p:arm", qq("one two three"), 4711))
WScript.Echo sCmd
If goFS.FileExists(sFSpec) Then Wscript.Echo " ==> now run sCmd (and perhaps Exit For)"
Next
output:
cscript 47922850.vbs
%comspec% /c "A:\pipapo" /p:arm "one two three" 4711
%comspec% /c "C:\pipapo" /p:arm "one two three" 4711
%comspec% /c "K:\pipapo" /p:arm "one two three" 4711
%comspec% /c "E:\pipapo" /p:arm "one two three" 4711
==> now run sCmd (and perhaps Exit For)

Related

Run files hidden

I am making a project of batch, and I used this vbscript to run it hidden: but I don't know if it's correct.
echo Set WshShell = CreateObject("WScript.Shell")
echo WshShell.Run chr(34) & "C:\Desktop\BatMan.bat" & chr(34), 0
echo Set WshShell = Nothing
echo WScript.Sleep 7500
do
echo WshShell.Run chr(34) & "C:\Downloads\BatManSetup1.bat" & chr(34), 0
echo Set WshShell = Nothing
echo WshShell.Run chr(34) & "C:\Downloads\BatManSetup4.bat" & chr(34), 0
echo Set WshShell = Nothing
echo WshShell.Run chr(34) & "C:\Downloads\BatManSetup5.bat" & chr(34), 0
echo Set WshShell = Nothing
echo WshShell.Run chr(34) & "C:\Downloads\BatManSetup6.bat" & chr(34), 0
echo Set WshShell = Nothing
echo WshShell.Run chr(34) & "C:\Downloads\BatManMain.bat" & chr(34), 0
echo Set WshShell = Nothing
Do I actually need to type the echo Set WshShell = Nothing multiple times? And is it possible to run many files hidden in one vbs like that? Will it lag the speed?

VBS: oShell.run and robocopy not working

I'm trying to mirror a share to a local computer using VBS and robocopy. The script works, but the folder from the share has spaces in the path and I can't get it to work with a path with spaces.
InputFile = "\\baardrob\Software application\Scripts\Input\computers.Txt"
Const OverWriteFiles = True
Set oShell = WScript.CreateObject("WSCript.shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(InputFile)
Set myLog = objFSO.OpenTextFile("\\baardrob\Software application\Scripts\Input\failed.txt", 2)
Do Until objFile.AtEndOfStream
strComputer = objFile.ReadLine
On Error Resume Next
oShell.run "robocopy "\\baardrob\software application" c:\temps /mir"
If Err Then myLog.WriteLine strComputer
On Error Goto 0
Loop
myLog.Close
MsgBox "Done"
EDIT:
Ekkehard.Horner solution worked, I have another problem though. c:\temps was just a attempt to get it work, it should actually be writing to "\" & strComputer "\c$", but this doesn't work.
I allready tried:
InputFile = "\\baardrob\Software application\Scripts\Input\computers.Txt"
Const OverWriteFiles = True
Set oShell = WScript.CreateObject("WSCript.shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(InputFile)
Set myLog = objFSO.OpenTextFile("\\baardrob\Software application\Scripts\Input\failed.txt", 2)
Do Until objFile.AtEndOfStream
strComputer = objFile.ReadLine
On Error Resume Next
oShell.run "robocopy ""\\baardrob\Software application"" ""\\"" & strComputer & ""\c$"" /mir"
If Err Then myLog.WriteLine strComputer
On Error Goto 0
Loop
myLog.Close
MsgBox "Done"
Your
"robocopy "\\baardrob\software application" c:\temps /mir"
is not valid VBScript:
>> s = "robocopy "\\baardrob\software application" c:\temps /mir"
>>
Error Number: 1002
Error Description: Syntax error
Fix 'inner' quotes by escaping them using "" (cf. here):
>> s = "robocopy ""\\baardrob\software application"" c:\temps /mir"
>> WScript.Echo s
>>
robocopy "\\baardrob\software application" c:\temps /mir
Update wrt to augmented question:
>> strComputer = "pipapo"
>> s = "robocopy ""\\baardrob\Software application"" ""\\"" & strComputer & ""\c$"" /mir"
>> WScript.Echo s
>>
robocopy "\\baardrob\Software application" "\\" & strComputer & "\c$" /mir
>> s = "robocopy ""\\baardrob\Software application"" ""\\" & strComputer & "\c$"" /mir"
>> WScript.Echo s
>>
robocopy "\\baardrob\Software application" "\\pipapo\c$" /mir

How many quotes to use when calling cmd from vbs? mklink example

just a quick question. I am trying to execute the following cmd command from vbs:
mklink /j "%userprofile%\AppData\Roaming\FIEBIG\bin" "%Programfiles(x86)%\fiebig-team\fticclient\bin"
with the following code:
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "cmd.exe mklink /j ""%userprofile%\AppData\Roaming\FIEBIG\bin"" ""Programfiles(x86)%\fiebig-team\fticclient\bin"" "
However, no matter what I've tried with adding or excluding quotes, it still does not work.
Can you please help with this,
Cheers
You don't need the CMD.EXE, do you?
strCommand = "mklink /j"
strCommand = strCommand & " "
strCommand = strCommand & Chr(34) & "%userprofile%\AppData\Roaming\FIEBIG\bin" & Chr(34)
strCommand = strCommand & " "
strCommand = strCommand & Chr(34) & "%Programfiles(x86)%\fiebig-team\fticclient\bin" & Chr(34)
objShell.Run strCommand
BTW, here's a debugging trick I use to make sure I get the proper command line. Use an InputBox() and pass your command string as the 3rd parameter. That allows you to copy and paste it to your command prompt window.
InputBox "", "", strCommand
Thanks a lot for your help. I am really into software packaging and just starting coding. How does that look to you?
Set objShell = WScript.CreateObject ("WScript.Shell")
objShell.run
strCommand = "mklink /j"
strCommand = strCommand & " "
strCommand = strCommand & Chr(34) & "%userprofile%\AppData\Roaming\FIEBIG\bin" & Chr(34)
strCommand = strCommand & " "
strCommand = strCommand & Chr(34) & "%Programfiles(x86)%\fiebig-team\fticclient\bin" & Chr(34)
objShell.Run strCommand

How to compare the result of cmd in the vbscript?

I ask about getting the result of this cmd command netstat -a | find /c "TCP"
and compare it with specific value using VBSCRIPT
thanks
It's difficult to answer question with so less details, but lets try anyway...
Example code below will illustrate 2 ways to store the command-line output to a variable in your vbs. WScript.Echo is used just as evidence (display the result).
'** VAR#1 (using Exec & StdOut) ----------
Dim ObjExec
Dim strFromProc
Set objShell = CreateObject("WScript.Shell")
Set ObjExec = objShell.Exec("%comspec% /c " _
& "netstat -a | find /c " & Chr(34) & "TCP" & Chr(34))
Do Until ObjExec.Stdout.atEndOfStream
strFromProc = strFromProc & ObjExec.StdOut.ReadLine & vbNewLine
Loop
WScript.Echo strFromProc ' display result from variable strFromProc
Set objShell = Nothing
Set ObjExec = Nothing
'** VAR#2 (using Run) --------------------
Const cLogFile = "result.txt"
Set objShell = CreateObject("WScript.Shell")
objShell.Run "%comspec% /c netstat -a | find /c " _
& Chr(34) & "TCP" & Chr(34) & ">" & cLogFile, 0, True
Dim oFile, Result
With CreateObject("Scripting.FileSystemObject")
If .FileExists(cLogFile) Then
Set oFile = .OpenTextFile(cLogFile)
Result = oFile.ReadLine
oFile.Close
Set oFile = .GetFile(cLogFile)
oFile.Delete
End If
End With
WScript.Echo Result ' display result from variable Result
Set oFile = Nothing
Set objShell = Nothing

VBScript error problems: Expected End 800A03F6

I am trying to make a vbs work, the idea is it will remotely install an msi, to a list of machines contained with a txt file.
I am getting multiple errors, the first is:
wrong number of arguments or invalid property assignment: "WshShell.Exec" Line 27, Char 1
WshShell.Exec "%COMSPEC% /C COPY " & StrInstallFile & " \\" & strComputer _
& "\C$\Windows\Temp", 0, TRUE
I seemd to have got round this with:
Set WshExec = WshShell.Exec......
then got:
expected end of statement line 27 cahr 29
adding an &:
Set WshExec = WshShell.Exec & "%COMSPEC%.....
now gets me:
expected end of statement line 27 char 110
which is the penultimate comma
Set WshExec = WshShell.Exec & "%COMSPEC% /C COPY" & StrInstallFile _
& " \\" & strComputer & "\C$\Windows\Temp", 0, TRUE
so i am not sure what is wrong at this point, and whether changing the whole line to a set was the right thing to have done.
You are mixing .Run and .Exec. The prototype for .Exec:
object.Exec(strCommand)
shows that you need someting like:
Set oExec = WshShell.Exec("%COMSPEC% /C COPY " & StrInstallFile & " \" & strComputer & "\C$\Windows\Temp")
If you want .Run instead, try something like:
Dim iRet : iRet = WshShell.Run(strCommand, [intWindowStyle], [bWaitOnReturn])
Dim iRet : iRet = WshShell.Run("%comspec% ...", 0, True)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("MachineList.Txt", 1)
StrInstallFile="install_flash_player_11_active_x.msi"
StrNoUpdateFile="mms.cfg"
StrInstallCMD="msiexec.exe /qn /i "
Do Until objFile.AtEndOfStream
strComputer = objFile.ReadLine
' --------- Check If PC is on -------------
Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshExec = WshShell.Exec("ping -n 1 -w 1000 " & strComputer) 'send 3 echo requests, waiting 2secs each
strPingResults = LCase(WshExec.StdOut.ReadAll)
If InStr(strPingResults, "reply from") Then
' ---------- Successful ping - run remote commands ----------------
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
' --------- Copy msi to windows temp folder
Set oExec = WshShell.Exec("%COMSPEC% /C COPY " & StrInstallFile & " \\" & strComputer & "\C$\Windows\Temp")
' --------- execute msi file on remote machine
Set oExec = WshShell.Exec("%COMSPEC% /C psexec \\" & StrComputer & " " & strInstallCMD & "c:\Windows\Temp\" & StrInstallFile)
' --------- Copy no "no update" file to remote machine, first line is for win7, second for xp
Set oExec = WshShell.Exec("%COMSPEC% /C COPY " & StrNoUpdateFile & " \\" & strComputer & "\C$\Windows\SysWOW64\Macromed\Flash")
Set oExec = WshShell.Exec("%COMSPEC% /C COPY " & StrNoUpdateFile & " \\" & strComputer & "\C$\Windows\system32\macromed\flash")
Else
' ---------- Unsuccessful ping - Leave computer name in MachineList.txt and continue ----------------
strNewContents = strNewContents & strComputer & vbCrLf
End If
Loop
objFile.Close
Set objFile = objFSO.OpenTextFile("MachineList.txt", 2)
objFile.Write strNewContents
objFile.Close

Resources