Looping issue with zip function

Author Message
Boothy

  • Total Posts : 4
  • Scores: 0
  • Reward points : 0
  • Joined: 12/2/2011
  • Status: offline
Looping issue with zip function Tuesday, December 13, 2011 3:19 AM (permalink)
0
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
 
#1
    Boothy

    • Total Posts : 4
    • Scores: 0
    • Reward points : 0
    • Joined: 12/2/2011
    • Status: offline
    Re:Looping issue with zip function Monday, December 19, 2011 10:55 PM (permalink)
    0
    So no one can help?
    May have to abandon the use of VBScript if I can't get this to work
     
    #2
      Wakawaka

      • Total Posts : 456
      • Scores: 23
      • Reward points : 0
      • Joined: 8/27/2009
      • Status: offline
      Re:Looping issue with zip function Tuesday, December 20, 2011 1:30 AM (permalink)
      0
      TLDRM but, VBS doesn't recognize that a path is a folder unless you end the string with a slash to denote it's a directory and not a file without an extension.  This doesn't apply to all instances of folder manipluation, but it has caught me up a fair number of times.
       
      #3
        Boothy

        • Total Posts : 4
        • Scores: 0
        • Reward points : 0
        • Joined: 12/2/2011
        • Status: offline
        Re:Looping issue with zip function Friday, January 13, 2012 5:01 AM (permalink)
        0
        I know this was a while ago, but been on other projects since, so only just managed to get back to this piece of work.
         
        But just wanted to say thanks, the tip about adding a slash to the path worked. Oddly the none looped code works without the slash, but the looped code doesn't work unless you add a slash, strange!
         
        Sorry no idea what 'TLDRM' is, never seen that used before.
         
        Cheers,
        Mark
         
        #4

          Online Bookmarks Sharing: Share/Bookmark

          Jump to:

          Current active users

          There are 0 members and 1 guests.

          Icon Legend and Permission

          • New Messages
          • No New Messages
          • Hot Topic w/ New Messages
          • Hot Topic w/o New Messages
          • Locked w/ New Messages
          • Locked w/o New Messages
          • Read Message
          • Post New Thread
          • Reply to message
          • Post New Poll
          • Submit Vote
          • Post reward post
          • Delete my own posts
          • Delete my own threads
          • Rate post

          2000-2012 ASPPlayground.NET Forum Version 3.9