I'm trying to compress a folder without any third-party tools using a batch file to call a VBScript.
However, I am getting an error.
Sample of Script (Batch):
@echo off
REM: Copy files from Current Dir to another
set CurrMonth=%date%
set CurrMonth=%CurrMonth:~3,2%
ECHO %CurrMonth%
set PathDir=".\Mon%CurrMonth%"
ECHO %PathDir%
timeout /t 5 /nobreak
ECHO "Copying files"
CScript zip.vbs ".\Mon" ".\%PathDir%.zip"
REM: xcopy /e ".\Mon" "%PathDir%.zip"
PAUSE
The idea is to call the VBScript that will take folder "Mon" and Zip it to the same location as Mon[CurrentMonth].zip.
VBScript:
'Get command-line arguments.
Set objArgs = WScript.Arguments
InputFolder = objArgs(0)
ZipFile = objArgs(1)
'Create empty ZIP file.
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)
'Required!
wScript.Sleep 2000
When running this script, it generates the correct Zip file. However, it throws an error...
"D:...\zip.vbs(11, 1) Microsoft VBScript runtime error: Object required: 'objShell.NameSpace(...)'"
Any thoughts?
Solution:
Dim CurrentDirectory
Set fso = CreateObject("Scripting.FileSystemObject")
Set app = CreateObject("Shell.Application")
' Getting date and weekday for Dynamic Pathing
CurrDate = Date()
CurrWeekday = WeekdayName(Weekday(CurrDate,2),True,2)
CurrStamp = CurrWeekday & "_" & Month(Date) & "_" & Year(Date)
' Get Current Path
CurrentDirectory = fso.GetParentFolderName(WScript.ScriptFullName)
' Set Target Zip Path
zipfile = CurrentDirectory & "\" & CurrStamp & ".zip"
' Set Source Path
fldr = CurrentDirectory & "\" & CurrWeekday
' Set Counter of files in Source
cnt = fso.GetFolder(fldr).Files.Count
' Creating the Target
fso.OpenTextFile(zipfile, 2, True).Write "PK" & Chr(5) & Chr(6) _
& String(18, Chr(0))
' Copy from Source to Target
Set zip = app.NameSpace(zipfile)
zip.CopyHere app.NameSpace(fldr).Items
' Loop and Sleep to Ensure Completed
Do
WScript.Sleep 100
Loop Until zip.Items.Count = cnt