I have the following command
"C:\Program Files\File Checksum Integrity Verifier\fciv.exe" -sha1 "D:\new.txt"
How to run this command from vbscript? Can anybody help me?
VBScript comes with a couple of different ways to execute command lines, both of which are on the WshShell object (WScript.Shell)
Using WshShell.Exec
Dim WshShell, oExec
Set WshShell = CreateObject("WScript.Shell")
Set oExec = WshShell.Exec("""C:\Program Files\File Checksum Integrity Verifier\fciv.exe"" -sha1 ""D:\new.txt""")
Do While oExec.Status = 0
WScript.Sleep 100
Loop
WScript.Echo oExec.Status
Using WshShell.Run
Dim WshShell
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run """C:\Program Files\File Checksum Integrity Verifier\fciv.exe"" -sha1 ""D:\new.txt""", 1, true
Related
I'm trying to create a script that runs the command: RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 255
Right now this errors out
Dim objShell
Set objShell = Wscript.CreateObject("WScript.Shell")
sEXE = """C:\Windows\system32\RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 255 """
with CreateObject("WScript.Shell")
.Run sEXE & " " , 1, true
end with
Wscript.Quit
Any suggestions? Could I make the script so I could run a bunch of commands and wait until each process executed has finished before executing the other command?
The command line should appear exactly as it would if you typed it at the command prompt (verify by Wscript.Echo sEXE):
sEXE = """C:\Windows\system32\RunDll32.exe"" InetCpl.cpl,ClearMyTracksByProcess 255"
So, I'm using (after modification) this code, from here: How to set recurring schedule for xlsm file using Windows Task Scheduler
My error: Runtime error: Unknown runtime error.
I've searched far and wide to find an way to close the Excel process, but almost everybody uses .Quit sadly it gives the above error. I've also tried .Close, but that is not recognized
' Create a WshShell to get the current directory
Dim WshShell
Set WshShell = CreateObject("WScript.Shell")
' Create an Excel instance
Dim myExcelWorker
Set myExcelWorker = CreateObject("Excel.Application")
' Disable Excel UI elements
myExcelWorker.DisplayAlerts = False
myExcelWorker.AskToUpdateLinks = False
myExcelWorker.AlertBeforeOverwriting = False
myExcelWorker.FeatureInstall = msoFeatureInstallNone
' Tell Excel what the current working directory is
Dim strSaveDefaultPath
Dim strPath
strSaveDefaultPath = myExcelWorker.DefaultFilePath
strPath = "C:\Users\hviid00m\Desktop"
myExcelWorker.DefaultFilePath = strPath
' Open the Workbook specified on the command-line
Dim oWorkBook
Dim strWorkerWB
strWorkerWB = strPath & "\Status Report (Boxplots) TEST.xlsm"
Set oWorkBook = myExcelWorker.Workbooks.Open (strWorkerWB, , , , , , True)
' Build the macro name with the full path to the workbook
Dim strMacroName
strMacroName = "Refresh"
on error resume next
myExcelWorker.Run strMacroName
if err.number <> 0 Then
WScript.Echo "Fejl i macro"
End If
err.clear
on error goto 0
oWorkBook.Save
' Clean up and shut down
' Don’t Quit() Excel if there are other Excel instances
' running, Quit() will shut those down also
myExcelWorker.Quit <--- ERROR
Set oWorkBook = Nothing
Set myExcelWorker = Nothing
Set WshShell = Nothing
Found some code on a different side.
The reason why (as far as I understood) is that .Quit and .Close is for VBA not VBS.
' Clean up and shut down
' Don’t Quit() Excel if there are other Excel instances
' running, Quit() will shut those down also
Dim objWMIService, objProcess, colProcess
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colProcess = objWMIService.ExecQuery ("Select * from Win32_Process Where Name = " & "'EXCEL.EXE'")
For Each objProcess in colProcess
objProcess.Terminate()
Next`
Set oWorkBook = Nothing
Set myExcelWorker = Nothing
Set WshShell = Nothing
Error System can't find the file specified
strCline = Document.getElementById("head").innerHtml
msgbox strCline
strCline = replace(strCline, " ",Chr(32))
oShell.run strCline
Set oShell = Nothing
Above code produces error because it can't read file name properly. It's all because of space characters in file name. After reading, i found chr(32) would replace space character but it won't. How do I make it take space character.
Edit:
My final code looked like this which worked. I made mistake while creating object.
Sub funEdit
set oShell=createobject("Wscript.shell")
strCline = Document.getElementById("head").innerHtml
msgbox strCline
strCline = replace(strCline, " ",Chr(32))
oShell.run strCline
Set oShell = Nothing
End Sub
The shell splits a command line into parameters using blank(s) for a delimiter. If you want to send text file specifications to .Run to display them automagically in the default editor, you must double quote the (logically) single parameter. This demo code:
Option Explicit
Dim sFSpec : sFSpec = "C:\Documents and Settings\eh\tmp.txt"
Dim sCmd : sCmd = sFSpec
Dim oWSH : Set oWSH = CreateObject("WScript.Shell")
On Error Resume Next
oWSH.Run sCmd
WScript.Echo qq(sCmd), "=>", Err.Number, Err.Description
Err.Clear
sCmd = qq(sFSpec)
oWSH.Run sCmd
WScript.Echo qq(sCmd), "=>", Err.Number, Err.Description
On Error GoTo 0
Function qq(s)
qq = """" & s & """"
End Function
will output:
"C:\Documents and Settings\eh\tmp.txt" => -2147024894
""C:\Documents and Settings\eh\tmp.txt"" => 0
and open only one Notepad.
See here for some context.
Hi I cant get the below script ive worked on to pickup the extension of the files, Can any help me out by pointing where I have gone wrong?
dim fileSystem, folder, file
dim path
dim count : count = 0
path = "C:\temp"
Set fileSystem = CreateObject("Scripting.FileSystemObject")
Set folder = fileSystem.GetFolder(path)
for each file in folder.Files
if file.DateLastModified > dateadd("h", -24, Now) & File.name = "txt" then
count = count + 1
end if
Next
if count < 4 then
Set WshShell = WScript.CreateObject("WScript.Shell")
strcommand = "eventcreate /T ERROR /ID 666 /L Application /SO BESROffsite /D " & _
Chr(34) & count & " Files found please check offsite copy " & Chr(34)
WshShell.Run strcommand
wScript.Quit ( 1001 )
Else
Set WshShell = WScript.CreateObject("WScript.Shell")
strcommand = "eventcreate /T Information /ID 666 /L Application /SO BESROffsite /D " & _
Chr(34) & count & " Files found offsite is working fine " & Chr(34)
WshShell.Run strcommand
wScript.Quit ( 0 )
End if
File.name is the full name including the extension, to test for the extension;
if ... fileSystem.getExtensionName(file.name) = "txt" then
You also want the logical And not the bitwise concatenating & in their too.
Alex's answer is the one you want, but just for reference if you were working just with vbs and a string filename, without the filesystemobject collection you could achieve the same via:
Right(strFilename, Len(strFilename) - Instrrev(strFilename, "."))
This essentially finds the position of the final "." in the filename, takes this away from the length of your filename, and then gives you however many character's that equals from the right hand side. This could be amended slightly to use the "Mid" command rather than the "Right" but I don't think it matters too much in a case like this.
I have the following VBScript:
Set Shell = WScript.CreateObject("WScript.Shell")
commandLine = puttyPath & "\plink.exe -v" & " -ssh" [plus additional commands here]
Set oExec = Shell.Exec(commandLine)
This causes a DOS window to appear but the output from plink.exe is not displayed. Is there any way to get the DOS window to display this output?
Windows scripting host lacks a system() command so you have to implement your own, IMHO my helper function is superior to stealthyninja's version since it waits for process exit and not just empty stdout and it also handles stderr:
Function ExecuteWithTerminalOutput(cmd)
Set sh = WScript.CreateObject("WScript.Shell")
Set exec = sh.Exec(cmd)
Do While exec.Status = 0
WScript.Sleep 100
WScript.StdOut.Write(exec.StdOut.ReadAll())
WScript.StdErr.Write(exec.StdErr.ReadAll())
Loop
ExecuteWithTerminalOutput = exec.Status
End Function
call ExecuteWithTerminalOutput("cmd.exe /c dir %windir%\*")
Try --
Set Shell = WScript.CreateObject("WScript.Shell")
commandLine = puttyPath & "\plink.exe -v" & " -ssh" [plus additional commands here]
Set oExec = Shell.Exec(commandLine)
Set oStdOut = oExec.StdOut
While Not oStdOut.AtEndOfStream
sLine = oStdOut.ReadLine
WScript.Echo sLine
Wend
The correct way is :
Set Shell = WScript.CreateObject("WScript.Shell")
commandLine = puttyPath & "\plink.exe -v" & " -ssh" [plus additional commands here]
Set oExec = Shell.Exec(commandLine)
Set oStdOut = oExec.StdOut
While Not oStdOut.AtEndOfStream
sLine = oStdOut.ReadLine
WScript.Echo sLine
Wend
Or:
Set Shell = WScript.CreateObject("WScript.Shell")
commandLine = puttyPath & "\plink.exe -v" & " -ssh" [plus additional commands here]
Set oExec = Shell.Exec(commandLine)
WScript.Echo oExec.StdOut.ReadAll