Clarification of VB Script Program to Zip Files Error Message - vbscript

Below is a small VB script that I believe should zip files in a folder.
I am new to VB script.
Set objArgs = WScript.Arguments
InputFolder = objArgs(0)
ZipFile = objArgs(1)
CreateObject("Scripting.FileSystemObject").CreateTextFile(ZipFile, True).Write "PK" & Chr(5) & Chr(6) & String(18, vbNullChar)
Set objShell = CreateObject("Shell.Application")
Set source = objShell.NameSpace(InputFolder).Items
objShell.NameSpace(ZipFile).CopyHere(source)
wScript.Sleep 2000
I found the script here. Batch file script to zip files
I run the script with the following
CScript zipt.vbs ..\AFolder AFolder.zip
I get the following error:
zipIt.vbs(6, 1) Microsoft VBScript runtime error: Object required
: 'objShell.NameSpace(...)'
..\AFolder is not empty. The zip file is created and is empty.
Debugging the script, the error is on this line.
Set source = objShell.NameSpace(InputFolder).Items
What does the error message mean?

Based on this question, try this:
Set objArgs = WScript.Arguments
Set fso = CreateObject("Scripting.FileSystemObject")
InputFolder = fso.GetAbsolutePathName(WScript.Arguments.Item(0))
ZipFile = fso.GetAbsolutePathName(objArgs(1))
CreateObject("Scripting.FileSystemObject").CreateTextFile(ZipFile, True).Write "PK" & Chr(5) & Chr(6) & String(18, vbNullChar)
Set objShell = CreateObject("Shell.Application")
Set source = objShell.NameSpace(InputFolder).Items
objShell.NameSpace(ZipFile).CopyHere(source)
wScript.Sleep 2000
To clarify, the script essentially didn't know what "AFolder.zip" was so it needs the entire, absolute path.

Related

rename a file using VBScript, launch file, wait, and rename again

I need to create a vbs script (for maintenance purposes) that renames foo.txt to a foo.bat and launch foo.bat and when foo.bat ends, rename foo.bat again to foo.txt
This is my script vbs:
On Error Resume next
Dim Fso
Set Fso = WScript.CreateObject("Scripting.FileSystemObject")
Fso.MoveFile "foo.txt", "foo.bat"
SCRIPT = "foo.bat"
Set objShell = CreateObject("Wscript.Shell")
strPath = Wscript.ScriptFullName
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(strPath)
strFolder = objFSO.GetParentFolderName(objFile)
NewPath = objFSO.BuildPath(strFolder, SCRIPT)
set objshell = createobject("wscript.shell")
objshell.Run NewPath, vbHide, true
Fso.MoveFile "foo.bat", "foo.txt"
On Error GoTo 0
the script executes well. Rename foo.txt to foo.bat. Launches foo.bat, but does not expect foo.bat to end and renames it to foo.txt.
I changed this line, nothing happens:
objshell.Run NewPath, vbHide, 1, true
What do I need or what did I do wrong?
Alternative Solution (no VBScript): (By suggestion of #KenWhite)
code:
On Error Resume next
Dim Fso
Set Fso = WScript.CreateObject("Scripting.FileSystemObject")
Fso.MoveFile "foo.txt", "foo.bat"
SCRIPT = "foo.bat"
Set objShell = CreateObject("Wscript.Shell")
strPath = Wscript.ScriptFullName
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(strPath)
strFolder = objFSO.GetParentFolderName(objFile)
NewPath = objFSO.BuildPath(strFolder, SCRIPT)
set objshell = createobject("wscript.shell")
objshell.Run NewPath, true
On Error GoTo 0
And at the end of foo.bat:
ren foo.bat foo.txt
exit
Thanks
Here is a possible solution just in case anyone is wondering how to solve this problem without resorting to the alternate proposal mentioned above.
Dim Fso
Set Fso = WScript.CreateObject("Scripting.FileSystemObject")
Fso.MoveFile "foo.txt", "foo.bat"
SCRIPT = "foo.bat"
Set objShell = CreateObject("Wscript.Shell")
strPath = Wscript.ScriptFullName
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(strPath)
strFolder = objFSO.GetParentFolderName(objFile)
NewPath = objFSO.BuildPath(strFolder, SCRIPT)
set objshell = createobject("wscript.shell")
objshell.Run "%COMSPEC% /c " & NewPath, 1, true
' Changes start here
'===================================================================
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
' Hold execution until cmd.exe process is done
do
' Get cmd.exe processes
Set colProcessList = objWMIService.ExecQuery _
("Select Name from Win32_Process WHERE Name LIKE 'cmd.exe'")
WScript.Sleep 250
Loop while colProcessList.count > 0
Fso.MoveFile "foo.bat", "foo.txt"

Create a folder on desktop via Vbscript

I have 2 scripts but could not combine them
Create a folder script
Option Explicit
Dim objNetwork, objComputer
Dim objFSO, objFSOText, objFolder, objFile
Dim strDirectory, strFile, MakeObject
strDirectory = "Folder"
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists(strDirectory) Then
Wscript.Echo strDirectory & " already exists"
Else
'Below is the added line
Set objFolder = objFSO.CreateFolder(strDirectory)
Wscript.Echo "The folder " & strDirectory & " has just been created"
End if
Wscript.Quit
desktop path script
set WshShell = WScript.CreateObject("WScript.Shell")
strDesktop = WshShell.SpecialFolders("Desktop")
wscript.echo(strDesktop)
Con some one help me please ?
Use .BuildPath to combine the Desktop folder path and the intended (sub)folder(name):
>> Set objFSO = CreateObject("Scripting.FileSystemObject")
>> Set WshShell = WScript.CreateObject("WScript.Shell")
>> strDirectory = "Folder"
>> strDirectory = objFSO.BuildPath(WshShell.SpecialFolders("Desktop"), strDirectory)
>> WScript.Echo strDirectory
>>
C:\Documents and Settings\eh\Desktop\Folder

vbs run program with objShell.run

I have the following vbscript that fail with an error on last line
Option Explicit
Dim objShell
Dim strComputer, strCmd , strVar
strComputer = "."
Set objShell = CreateObject("WScript.Shell")
' strVar = objShell.ExpandEnvironmentStrings("%ProgramFiles(x86)%")
' strCmd = strVar & "\CaptureBites\Express\Programs\AutoBites\Autobites.exe"
' wscript.echo strCmd
objShell.Run "taskkill /im Autobites.exe",,True
WScript.Sleep(5000)
objShell.run """%ProgramFiles(x86)%""" &"\CaptureBites\Express\Programs\AutoBites\Autobites.exe"
The problem is that script open the folder %ProgramFiles(x86)% but don't run the exe autobites.exe
Can you help me to debug it
The whole file spec has to be quoted, not only the first part:
objShell.run """%ProgramFiles(x86)%" &"\CaptureBites\Express\Programs\AutoBites\Autobites.exe"""

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

vb script: Trying to open a command prompt, navigate to a directory and run a command

I need to write a script that looks in a directory, finds the latest .zip file (there are .zip and .log in there) then opens a command prompt in a different directory and runs the following command:
loaddb.bat -Dlc.file="C:\Program Files\XyEnterprise\SDL LiveContent\data_old\export\<name of the newest file.zip>" -Dlc.pswd=<oor password> RESTORE
We can not install any languages so it has to be able to run on a Windows 2003 & 2008 server so I chose vbscript...
I have everything working apart from the running the command and can't seem to crack it.
My code is as follows:
Dim fileNewest
Dim fso
Set fso = WScript.CreateObject("Scripting.FileSystemObject")
Set oFolder = fso.GetFolder("C:\Program Files\XyEnterprise\SDL LiveContent\data\export")
For Each aFile In oFolder.Files
sExtension = fso.GetExtensionName(aFile.Name)
If sExtension = "log" Then
'Msgbox "The file extension is a " & sExtension
Else
'Msgbox "The file extension is a " & sExtension
If fileNewest = "" Then
Set fileNewest = aFile
Else
If fileNewest.DateCreated < aFile.DateCreated Then
Set fileNewest = aFile
End If
End If
End If
Next
Msgbox "The Newest File in the folder is " & fileNewest.Name & chr(13) & "Size: " & fileNewest.Size & " bytes" & chr(13) & "Was last modified on " & FileNewest.DateLastModified
Dim objShell
Set objShell = CreateObject("WScript.Shell")
objShell.Run "%comspec% /k c: & cd ../../../Program Files\XyEnterprise\SDL LiveContent\data\export"
How can I now run a command after opening that Dos prompt?
Thanks,
EDIT:
Adding the answer worked with a lot of help from Alex K:
objShell.Run "%comspec% /k c: & cd ""C:\Program Files (x86)\XyEnterprise\SDL LiveContent\"" & """"loaddb RESTORE -Dlc.file=C:\PROGRA~2\XYENTE~1\SDLLIV~1\data\Import\" & fileNewest.Name & " -Dlc.pswd=N2kAs72z"""""
You need to quote that path as it contains spaces;
objShell.Run "%comspec% /k c: & cd ""../../../Program Files\XyEnterprise\SDL LiveContent\data\export"""

Resources