shell script not workig with parameters - shell

This works with CMD:
rar a c:\new.rar c:\*.*
This works running script.vbs, but does not do too much!:
Function qq(strIn)
qq = Chr(34) & strIn & Chr(34)
End Function
Set sh = CreateObject("WScript.Shell")
MyCmd = "Cmd /c CD /D " & qq("C:\Program Files (x86)\WinRAR") & " & a c:\new.rar c:\*.* "
sh.run MyCmd,1,True
It is no longer returning errors, but file new.rar is not being created either, it says 'a' is not a valid parameter

(1) Always echo the command before you try to feed it to run:
>> MyCmd = "Cmd /c CD /D " & qq("C:\Program Files (x86)\WinRAR") & " & a c:\new.rar c:\*.* "
>> WScript.Echo MyCmd
>>
Cmd /c CD /D "C:\Program Files (x86)\WinRAR" & a c:\new.rar c:\*.*
Obviously your concatenation puts a spurious "&" in the result.
(2) Never let you be persuaded to put random additions like "cmd /c cd /d" into you command.
(3) Build your command line in a structured way. E.g:
Option Explicit
Function qq(s) : qq = """" & s & """" : End Function
Dim sCmd : sCmd = Join(Array( _
qq("C:\Program Files (x86)\WinRAR") _
, "a" _
, qq("c:\some path with spaces\new.rar") _
, qq("c:\source dir with more spaces\*.*") _
))
WScript.Echo sCmd
output:
cscript y.vbs
"C:\Program Files (x86)\WinRAR" a "c:\some path with spaces\new.rar" "c:\source dir with more spaces\*.*"
See here for background and reasons why.
Update:
This way, blunders (just the path, not the full filespec of rar mentioned) can be spotted and corrected easily:
Option Explicit
Function qq(s) : qq = """" & s & """" : End Function
Dim sCmd : sCmd = Join(Array( _
qq("C:\Program Files (x86)\WinRAR\rar.exe") _
, "a" _
, qq("c:\some path with spaces\new.rar") _
, qq("c:\source dir with more spaces\*.*") _
))
WScript.Echo sCmd

Try this code and let me know if it works or not for you ?
Set sh = CreateObject("WScript.Shell")
MyCmd = "Cmd /c CD /D " & qq("C:\Program Files (x86)\WinRAR") & " & rar /? & pause"
sh.run MyCmd,1,True
'************************************
Function qq(strIn)
qq = Chr(34) & strIn & Chr(34)
End Function
'************************************
EDIT : Check this code :
Set sh = CreateObject("WScript.Shell")
MyCmd = "Cmd /c CD /D " & qq("C:\Program Files (x86)\WinRAR") & " & rar a c:\new.rar c:\*.* & pause"
sh.run MyCmd,1,True
'************************************
Function qq(strIn)
qq = Chr(34) & strIn & Chr(34)
End Function
'************************************

Related

How to copy the contents of a text file to the clipboard?

I use the following code VBS to read a text file. How can I save the contents of this text file to my computer clipboard to paste elsewhere?
Option Explicit
Dim objFileToRead, strFileText, content
Set objFileToRead = CreateObject("Scripting.FileSystemObject").OpenTextFile("C:\1.txt",1)
strFileText = objFileToRead.ReadAll()
objFileToRead.Close
Here is an easy way to do it using clip command line :
Option Explicit
Dim Title : Title = "Copy the contents of a text file to the clipboard"
Dim File2Read : File2Read = "C:\1.txt"
If CreateObject("Scripting.FileSystemObject").FileExists(File2Read) Then
CreateObject("WScript.Shell").Run "cmd.exe /c clip < "& File2Read &"",0,TRUE
MsgBox "The contents of file : " & chr(34) & File2Read & chr(34) & vbCrlf & _
" is copied to the clipboard !",vbInformation,Title
Else
MsgBox "Check if the " & chr(34) & File2Read & chr(34) &" exists !",vbExclamation,Title
End If
EDIT : Save a ping test into file and copy it to clipboard !
Option Explicit
Dim Title : Title = "Copy the contents of a text file to the clipboard"
Dim File2Read : File2Read = "E:\Yahoo_Ping.txt"
CreateObject("WScript.Shell").Run "CMD /C Ping www.yahoo.com > "& File2Read &"",1,True
If CreateObject("Scripting.FileSystemObject").FileExists(File2Read) Then
CreateObject("WScript.Shell").Run "CMD /U /C clip < "& File2Read &"",0,TRUE
MsgBox "The contents of file : " & chr(34) & File2Read & chr(34) & vbCrlf & _
" is copied to the clipboard !",vbInformation,Title
Else
MsgBox "Check if the " & chr(34) & File2Read & chr(34) &" exists !",vbExclamation,Title
End If
With Batch and Powershell :
#echo off
Title Set-Clipboard and Get-ClipBoard with Powershell and Batch
Set "InputFile=%Temp%\%~n0.txt"
echo Please be patient ....
Ping www.stackoverflow.com > "%InputFile%"
Set "OutPutFile=%userprofile%\Desktop\MyClipBoard.txt"
Call :RunPS-Get-ClipBoard "%InputFile%" "%OutPutFile%"
Cls & Type "%OutPutFile%"
Pause & Exit
REM ----------------------------------------------------------------------
:RunPS-Get-ClipBoard <InputFile> <OutPutFile>
Set psCmd="&{Set-Clipboard -Value (GC '"%~1"') | Get-ClipBoard}"
If Exist "%~2" Del "%~2"
#for /F "tokens=*delims=" %%i in ('Powershell %psCmd%') do >> "%~2" echo %%i
Goto:eof
REM ----------------------------------------------------------------------

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.

Can't find where "Expected End of Statement is"

So I am creating a code in VBS to automate setting up computers. Step 1 of my code works well, Step 2 not implemented in this example but works good, Step 3 throws me a "Expected end of Statement" error at Command_3A.
The string I am trying to set is start "Lightspeed" /wait /i "Programs\Lightspeed\UserAgentx64 V2.1.14.msi"
I have tried these ways but I am missing something.
Command_3A = "start "Lightspeed" /wait /i "Programs\Lightspeed\UserAgentx64 V2.1.14.msi" "`
Command_3A = "start " ""Lightspeed"" " /wait /i " ""Programs\Lightspeed\UserAgentx64 V2.1.14.msi"" "`
Command_3A = (start "Lightspeed" /wait /i "C:\Users\ccollins\Desktop\ThumbDrive\Programs\Lightspeed\UserAgentx64 V2.1.14.msi" )`
Here is my code:
'Dim objShell
Set objShell = WScript.CreateObject ("WScript.Shell")
Drive_Letter = "C:"
File_Path = "C:\Users\ccollins\Desktop\ThumbDrive\"
' Step 1 - Set Power Settings
Command_1A = "powercfg /change standby-timeout-ac 0"
Command_1B = "powercfg /change standby-timeout-dc 15"
Command_1C = "powercfg /change monitor-timeout-ac 0"
Command_1D = "powercfg /change monitor-timeout-dc 15"
Command_1E = "powercfg /change hibernate-timeout-ac 0"
Command_1F = "powercfg /change hibernate-timeout-dc 15"
objShell.Run "cmd /k " & Command_1A & "&" & Command_1B & "&" & Command_1C & "&" & Command_1D & "&" & Command_1E & "&" & Command_1F & "& exit"
' Step 2 - Remove Bloatware (Win10Apps)
' Step 3 - Install wanted programs
Command_3A = (start "Lightspeed" /wait /i "C:\Users\ccollins\Desktop\ThumbDrive\Programs\Lightspeed\UserAgentx64 V2.1.14.msi" )
Command_3B = "start "Acrobat" /wait "Programs\AcroRdrDC1801120058_en_US.exe" /sAll "
Command_3C = "start "AZMerit" /wait /I "Programs\AzMERITSecureBrowser10.4-2018-08-02.msi" /passive "
Command_3D = "start "Java" /wait "Programs\jre-8u201-windows-x64.exe" /s "
Command_3E = "start "Chrome" /wait "Programs\ChromeStandaloneSetup64.exe" /silent /install "
Command_3F = "start "Eset" /wait "Programs\ESet Rip and Replace.exe" "
objShell.Run "cmd /k " & Drive_Letter & "&" & Command_3A & "&" & Command_3B & "&" & Command_3C & "&" & Command_3D & "&" & Command_3E & "&" & Command_3F & "& exit"
Set oShell = Nothing'
I am definitely missing something and just need another set of eyes to look at my code.
So this is what I came up with. I had to use Chr(34) for the " in the code that wanted inserted as part of the command. I also inserted an input box for sPath and UAC control.
Set wshShell = WScript.CreateObject("WScript.Shell")
dim sPath
' ----------------------------------------------------------'
' UAC Control
If WScript.Arguments.Length = 0 Then
Set objShell = CreateObject("Shell.Application")
objShell.ShellExecute "wscript.exe" _
, Chr(34) & WScript.ScriptFullName & Chr(34) & " RunAsAdministrator", , "runas", 1
WScript.Quit
End If
'Step 0 - Set Drive Letter or File Path
sPath = InputBox("Enter the Path to the Drive. If entering just drive letter then add :, if file path remove the end \")
' Step 1 - Set Power Settings
' Step 2 - Remove Bloatware (Win10Apps)
' Step 3 - Install wanted programs
Function GetOsBits()
Set shell = CreateObject("WScript.Shell")
If shell.ExpandEnvironmentStrings("%PROCESSOR_ARCHITECTURE%") = "AMD64" Then
GetOsBits = 64
Else
GetOsBits = 32
End If
End Function
Command_3A = Chr(34) & sPath & "\Programs\Lightspeed\UserAgentx64 V2.1.14.msi" & Chr(34)
Command_3B = Chr(34) & sPath & "\Programs\Lightspeed\UserAgentx86 V2.1.14.msi" & Chr(34)
Command_3C = Chr(34) & sPath & "\Programs\AcroRdrDC1801120058_en_US.exe" & Chr(34) & "& /sAll "
Command_3D = Chr(34) & sPath & "\Programs\Java\jre-8u201-windows-x64.exe" & Chr(34) & "& /s "
Command_3E = Chr(34) & sPath & "\Programs\Java\jre-8u201-windows-i586.exe" & Chr(34) & "& /s "
Command_3F = Chr(34) & sPath & "\Programs\Chrome\ChromeStandaloneSetup64.exe" & Chr(34) & "& /silent /install "
Command_3G = Chr(34) & sPath & "\Programs\Chrome\ChromeStandaloneSetupi586.exe" & Chr(34) & "& /silent /install "
Command_3H = Chr(34) & sPath & "\Programs\ESet Rip and Replace.exe" & Chr(34)
If GetOsBits = 64 Then
wshShell.Run(Command_3A) 'LightSpeed
wscript.Sleep 4000
wshShell.Run(Command_3C) 'Adobe Reader
wscript.Sleep 30000
wshShell.Run(Command_3D) 'Java
wscript.Sleep 30000
wshShell.Run(Command_3F) 'Chrome
wscript.Sleep 30000
wshShell.Run(Command_3H) 'Eset
wscript.Sleep 30000
Else
wshShell.Run(Command_3B) 'LightSpeed x86
wscript.Sleep 4000
wshShell.Run(Command_3C) 'Adobe Reader
wscript.Sleep 4000
wshShell.Run(Command_3E) 'Java x86
wscript.Sleep 30000
wshShell.Run(Command_3G) 'Chrome x86
wscript.Sleep 30000
wshShell.Run(Command_3H) 'Eset
wscript.Sleep 30000
End If
Set oShell = Nothing

vbsScript remove space echo

Hello im making unhide usb files i modify konboot usb installer to usb unhide
CMD
cscript //Nologo USB.vbs>unhide.bat
Wscript.Echo "attrib -s -h -r /s /d "& SingleLogicalDisk.DeviceID,"\*.*"
the output
attrib -s -h -r /s /d F:(have space here)*.*
i need no space on F:(Here)*.*
my final output i want
attrib -s -h -r /s /d F:*.*
vbscript
Dim query
Dim WMBIObj
Dim AllDiskDrives
Dim SingleDiskDrive
Dim AllLogicalDisks
Dim SingleLogicalDisk
Dim AllPartitions
Dim Partition
Dim result
Dim textmsg
Dim wshShell
Dim FileObj
Dim Counter
set FileObj = CreateObject("Scripting.FileSystemObject")
set wshShell = wscript.createObject("wscript.shell")
Set WMBIObj = GetObject("winmgmts:\\.\root\cimv2")
Set AllDiskDrives = WMBIObj.ExecQuery("SELECT * FROM Win32_DiskDrive where InterfaceType='USB'") '
For Each SingleDiskDrive In AllDiskDrives
counter = counter + 1
query = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" + SingleDiskDrive.DeviceID + "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition"
Set AllPartitions = WMBIObj.ExecQuery(query)
For Each Partition In AllPartitions
query = "ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" + Partition.DeviceID + "'} WHERE AssocClass = Win32_LogicalDiskToPartition"
Set AllLogicalDisks = WMBIObj.ExecQuery (query)
For Each SingleLogicalDisk In AllLogicalDisks
textmsg = "============================================" & VbCr & _
"DeviceID: " & SingleDiskDrive.DeviceID & VbCr & _
"Logical Drive: " & SingleLogicalDisk.DeviceID & VbCr & _
"Model: " & SingleDiskDrive.Model & VbCr & _
"Manufacturer: " & SingleDiskDrive.Manufacturer & VbCr & _
"============================================" & VbCr & _
"Would you like to use this drive as destination?" & VbCr & _
"Warning, disk data may be overwritten"
result = MsgBox(textmsg, vbQuestion + vbOKCancel, "USB")
if result = vbOk Then
WScript.Echo "echo DeviceID: " & SingleDiskDrive.DeviceID
WScript.Echo "echo Logical Drive: " & SingleLogicalDisk.DeviceID
WScript.Echo "echo Model: " & SingleDiskDrive.Model
WScript.Echo "echo Manufacturer: " & SingleDiskDrive.Manufacturer
Wscript.Echo "attrib -s -h -r /s /d "& SingleLogicalDisk.DeviceID,"\*.*"
wshShell.Run "unhide.bat"
WScript.quit
MsgBox "Your USB is ready!", vbInformation + vbOKOnly, "USB"
WScript.quit
End If
Next
Next
Next
if counter = 0 Then
MsgBox "No USB disks detected or unknown error!", vbCritical + vbOkOnly, "USB"
End If
In this line:
Wscript.Echo "attrib -s -h -r /s /d "& SingleLogicalDisk.DeviceID,"\*.*"
You need to concatenate the strings "attrib -s -h -r /s /d ", SingleLogicalDisk.DeviceID and "\*.*"
You are using , instead of & to concatenate the second and third strings. It should actually be:
Wscript.Echo "attrib -s -h -r /s /d "& SingleLogicalDisk.DeviceID & "\*.*"
When you use , instead of &, the third string is considered as another argument to the echo method. So, it adds a space between the arguments in the output.
From: Echo Method
Each displayed item is separated with a space character.

Popup to appear and close with a target process

I am trying to use the code located here, and with some tweaking for HTA. It works to a point:
Set wshShell = WScript.CreateObject( "WScript.Shell" )
changes into:
Set wshShell = CreateObject( "WScript.Shell" )
The popup comes up, but it won't go away until I click it. I need it appear when a process is running, then disappear when it ends. Why is my execution failing to do this?
ProgressMsg "Copying, Please wait.", "File Being Copied"
strCMD = "cmd.exe /c robocopy " & strLocalSemesterCourse & " " & strServerSemesterCourse &" " & strFileName & " /LOG+:" & strLogName & " /V /FP /TEE"
nReturn = objShell.Run(strCMD, 1, true)
ProgressMsg "", "Finished"
You need to define objProgressMsg as a global variable for this to work:
Dim objProgressMsg
...
ProgressMsg "Copying, Please wait.", "File Being Copied"
strCMD = "cmd.exe /c robocopy " & strLocalSemesterCourse & " " _
& strServerSemesterCourse &" " & strFileName & " /LOG+:" & strLogName _
& " /V /FP /TEE"
nReturn = objShell.Run(strCMD, 1, true)
ProgressMsg "", "Finished"
Without the global variable, ProgressMsg() will use a local variable objProgressMsg. Local variables don't retain their value after the function exits, so the variable will be empty every time you call the function.

Resources