Hi all,
Okay first of, I'm quite a noob with VBScript, so hopefully this is just a beginners issue, rather than something fundamental. Also please be gentle, this is my first post after introducing myself, so this may be more verbose that it needs to be! For background, I do a lot of coding in other tools, so am familiar with scripting and coding using other languages.
I'm in the process of writing a tool to help with deployment of code, the last main function of which needs to zip up some files. I'm deailing with an existing directory structure, so process basically needs to look dynamically down through three levels of folders, and then create multiple zip files named after one of those sub-folders, and put files in the last level of the folder into the zip.
For testing, and to keep things simple while developing, I've put only the looping and zip function into the draft script. The intention being to get this to work first, before adding to the main script afterwards.
The problem is I can create the loop file, and it reports all the correct names etc. and I can create the zip sub, which works on it's own, but the two together fail. So I'm assuming I've fallen into a noob trap or something that will hopefully be obvious to you folks who actually know what your doing.
Please note the original zipping code isn't mine, it was taken from the following page, and then modified slightly (mostly minor name changes). I then added a simple logging function for debug messages (rather than using Echo etc.).
Original zip code from here (about half way down):
http://stackoverflow.com/questions/2888972/write-to-file-using-copyhere-without-using-wscript-sleep Here's the first test code sample (the working one):
debuglog "####### Script START ########"
' Simply run the function twice manually.
debuglog "Zip file = C:\VBScript\FMS.zip"
debuglog "Folder = C:\VBScript\file_list_test_dir\RML30\FMS"
CreateZip "C:\VBScript\FMS.zip", "C:\VBScript\file_list_test_dir\RML30\FMS"
debuglog "Zip file = C:\VBScript\FTG.zip"
debuglog "Folder = C:\VBScript\file_list_test_dir\RML30\FTG"
CreateZip "C:\VBScript\FTG.zip", "C:\VBScript\file_list_test_dir\RML30\FTG"
Sub NewZip(pathToZipFile)
' Create a new empty zip file.
debuglog "NewZip Sub : Creating new empty zip file (" & pathToZipFile & ") "
Dim objFso
Set objFso = CreateObject("Scripting.FileSystemObject")
Dim file
Set file = objFso.CreateTextFile(pathToZipFile)
file.Write Chr(80) & Chr(75) & Chr(5) & Chr(6) & String(18, 0)
file.Close
Set objFso = Nothing
Set file = Nothing
WScript.Sleep 500
End Sub
Sub CreateZip(pathToZipFile, dirToZip)
debuglog "CreateZip Sub : pathToZipFile = " & pathToZipFile
debuglog "CreateZip Sub : dirToZip = " & dirToZip
Dim objFso
Set objFso= Wscript.CreateObject("Scripting.FileSystemObject")
If objFso.FileExists(pathToZipFile) Then
debuglog "Housekeeping - Zip file already exists - so deleting it."
objFso.DeleteFile pathToZipFile
End If
If Not objFso.FolderExists(dirToZip) Then
debuglog "ERROR : The directory of source files does not exist."
Exit Sub
End If
NewZip pathToZipFile
dim objShell
set objShell = CreateObject("Shell.Application")
Dim objZip
Set objZip = objShell.NameSpace(pathToZipFile)
Dim objDirToZip
Set objDirToZip = objShell.NameSpace(dirToZip)
' DEBUG - For diagnostic purposes only
For Each s In objDirToZip.items
debuglog s
Next
' Copying files from source Dir to Zip, no prompt...
debuglog "Copying files from source to new zip file"
objZip.CopyHere objDirToZip.items, 4
' Copy is async, so check item count and sleep till it's <=
Do Until objDirToZip.Items.Count <= objZip.Items.Count
Wscript.Sleep(200)
Loop
End Sub
Sub debuglog (msg)
' Simple Log write function
' Just pass any string to the sub to write to a log file.
Set objFSO = Wscript.CreateObject("Scripting.FileSystemObject")
set stream = objFSO.OpenTextFile(".\debug.log", 8, True)
stream.writeline msg
stream.close
End Sub
The '..\FMS' & '..\FTG' locations used simply contain a few text files.
When the above is run, it simply runs the 'CreateZip' Sub twice, once against each input dir and to a new zip.
This works without error, producing two valid zip files, containing the files that were found in each of the FMS and FTG sub-directories, so as expected.
But this is where it falls down, I replace the two hard coded 'CreateZip' lines with a loop, that simply starts at the "C:\VBScript\file_list_test_dir" level, looks inside there for the first dir (RML30), then inside there for the FMS and FTG dirs and then passes the new full zip name, and the full path to the FMS and FTG directories.
Here's the loop code, this simply replaces the first part of the code above, the lines before the 'Sub NewZip(pathToZipFile)'.
strFolderToSearch = "C:\Documents and Settings\mark.boothroyd\My Documents\Windows\VBScript\file_list_test_dir"
Set fso = CreateObject("Scripting.FileSystemObject")
debuglog "####### Script START ########"
strCurPath = fso.GetAbsolutePathName(".")
' Find the first set of sub folders
Set objRootFolder = fso.GetFolder(strFolderToSearch)
Set colSubfolders = objRootFolder.SubFolders
For Each objFolder in colSubfolders
' Now find the second set of sub folders within the first.
Set objRootFolder2 = fso.GetFolder(objFolder)
Set colSubfolders2 = objRootFolder2.SubFolders
For Each objFolder2 in colSubfolders2
CreateZip strCurPath & "\" & objFolder2.name & ".zip", objFolder2
Next
Next
Running this results in a 'Object Required' error.
If I comment out the 'CreateZip' lines, replacing that with a simple debug message, the loop works correctly, and creates the correct names to pass to the CreateZip sub.
So as far as I can tell, the loop works fine, on it's own, and the zipiing works fine, on it's own, but the two together fail!
Here's the exact error:
Line: 73
Char: 2
Object Required: 'objDirToZip'
Code: 800A01AB
Note: Line 73 is this one: 'For Each s In objDirToZip.items'
So for some reason, 'objDirToZip' isn't an object when run via the loop, despite the 'Set objDirToZip = objShell.NameSpace(dirToZip)' line just above it!
As I've added debug logs, here's the outputs:
Inital run with hardcoded params:
####### Script START ########
Zip file = C:\VBScript\FMS.zip
Folder = C:\VBScript\file_list_test_dir\RML30\FMS
CreateZip Sub : pathToZipFile = C:\VBScript\FMS.zip
CreateZip Sub : dirToZip = C:\VBScript\file_list_test_dir\RML30\FMS
NewZip Sub : Creating new empty zip file (C:\VBScript\FMS.zip)
E123456_RML30_sftp_push_to_FTG.cfg
I123456_RML30_ftp_pull.cfg.sit
RML30_move.cfg
Copying files from source to new zip file
Zip file = C:\VBScript\FTG.zip
Folder = C:\VBScript\file_list_test_dir\RML30\FTG
CreateZip Sub : pathToZipFile = C:\VBScript\FTG.zip
CreateZip Sub : dirToZip = C:\VBScript\file_list_test_dir\RML30\FTG
NewZip Sub : Creating new empty zip file (C:\VBScript\FTG.zip)
E123456_RML30_sftp_push.cfg
New_TextDocument.cfg
New_TextDocument_2.cfg
Copying files from source to new zip file
So the above works, produces two zip files congaing two different sets of files. (all as expected).
Here's the second run via the loop:
####### Script START ########
Zip file = C:\VBScript\FMS.zip
Folder = C:\VBScript\file_list_test_dir\RML30\FMS
CreateZip Sub : pathToZipFile = C:\VBScript\FMS.zip
CreateZip Sub : dirToZip = C:\VBScript\file_list_test_dir\RML30\FMS
NewZip Sub : Creating new empty zip file (C:\VBScript\FMS.zip)
So exactly the same Zip file and folders params being passed to the sub, and the blank zip is created fine, but as soon as the script tries to access the zip file as an object, it fails, despite working fine on the first run?!
So, sorry this got a little long!
Hope someone out there can help me,
Regards,
Mark