• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 669
  • Last Modified:

vbs script for zip a directory doesn't work consistently

My previous question titled Add date and time to zip backup file in Windows 7
doesn't work all of the time. sometimes the zip file doesn't have anything in it, even though the directory EDOC has information.

Also, is there a way to have it ask for the user to click on OK that it finished? Right now the screen doesn't show anything, so they don't know for certain that it ran.

The script:
Option Explicit


Dim oFSO, oShell, oFld, zipFil
Dim strDir, strZip, strDay, strTime, strDate
Dim dteDateTime

dteDateTime = Now()

If Len(DatePart("d", dteDateTime) = 1) Then
      strDay = "0" & DatePart("d", dteDateTime)
Else
      strDay = DatePart("d", dteDateTime)
End If

strDate = Right(DatePart("yyyy", dteDateTime), 2) & DatePart("m", dteDateTime) & strDay & DatePart("h", dteDateTime) & DatePart("n", dteDateTime)

strDir = "c:\zipin\EDOC\"
strZip = "C:\backupzip\backup" & strDate & ".zip"

Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oShell = CreateObject("Shell.Application")
'Set oFile = oFSO.GetFile(strFile)
Set oFld = oFSO.GetFolder(strDir)
 

Set zipFil = oFSO.CreateTextFile(strZip)
zipFil.WriteLine Chr(80) & Chr(75) & Chr(5) & Chr(6) & String(18, 0)
zipFil.Close

oShell.NameSpace(strZip).CopyHere oFld.Path
wscript.sleep 500

Set zipFil = Nothing
Set oShell = Nothing
Set oFld = Nothing
set oFso=Nothing
wscript.quit

Open in new window

0
penguins_rule
Asked:
penguins_rule
  • 4
  • 4
  • 2
2 Solutions
 
RobOwner (Aidellio)Commented:
Just before the last line put:
Msgbox "finished"
That will prompt the user to click ok
0
 
JezWaltersCommented:
It's just a guess, but perhaps your script isn't waiting long enough.

Have you thought of replacing your "WScript.Sleep 500" with a test that the destination zip file has all the content that you are expecting?
0
 
JezWaltersCommented:
By the way, you might like to handle single-digit months, hours and minutes in the same way that you're already handling single-digit days!  :-)

The following should give you the idea of how to do this:
<link removed - GaryC132>
http://msdn.microsoft.com/en-us/library/8aebkz6s(v=vs.84).aspx
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
penguins_ruleAuthor Commented:
thank you tagit for the message box. thank you JezWalters, increasing the Sleep to 2000 makes it better.

I know nothing about VB Script, and even though the link for the information is given, is someone willing to give me the command and where to change it?
0
 
JezWaltersCommented:
This should get you started:

Option Explicit

' Declare constants
Const INPUT_FOLDER = "C:\zipin\EDOC\"
Const OUTPUT_FOLDER = "C:\backupzip\"

' Declare variables
Dim fsoFileSystemObject  ' As Scripting.FileSystemObject
Dim shlShell             ' As Shell32.Shell
Dim fldInputFolder       ' As Scripting.Folder
Dim fldOutputFolder      ' As Shell32.Folder
Dim txsZipFile           ' As Scripting.TextStream
Dim strZipFile           ' As String
Dim strDay               ' As String
Dim strDate              ' As String
Dim datDateTime          ' As Date

' Determine zip file
datDateTime = Now()
strZipFile = OUTPUT_FOLDER & _
             "backup" & _
             Right(Year(datDateTime), 2) & _
             Right("0" & Month(datDateTime), 2) & _
             Right("0" & Day(datDateTime), 2) & _
             Right("0" & Hour(datDateTime), 2) & _
             Right("0" & Minute(datDateTime), 2) & _
             ".zip"

' Create zip file
Set fsoFileSystemObject = CreateObject("Scripting.FileSystemObject")
Set txsZipFile = fsoFileSystemObject.CreateTextFile(strZipFile)
txsZipFile.WriteLine Chr(80) & Chr(75) & Chr(5) & Chr(6) & String(18, 0)
txsZipFile.Close
Set txsZipFile = Nothing

' Populate zip file
Set fldInputFolder = fsoFileSystemObject.GetFolder(INPUT_FOLDER)
Set shlShell = CreateObject("Shell.Application")
Set fldOutputFolder = shlShell.NameSpace(strZipFile)
fldOutputFolder.CopyHere fldInputFolder.Path

' Wait until zip file populated
Do
    Set fldOutputFolder = shlShell.NameSpace(strZipFile & "\" & fldInputFolder.Name)
    WScript.Sleep 500
Loop While fldOutputFolder Is Nothing
Do
    WScript.Sleep 500
Loop Until fldInputFolder.Files.Count = fldOutputFolder.Items.Count

Set fldOutputFolder = Nothing
Set fldInputFolder = Nothing
Set shlShell = Nothing
Set fsoFileSystemObject = Nothing

MsgBox "Done!"

Open in new window


As you can see, I've made all of the date/time components of the zip file name 2 digits too!  :-)
0
 
RobOwner (Aidellio)Commented:
The key thing here is testing for the number of items in the zip.  As you're copying a folder you will only ever see 1, which happens when the copy finishes.  So not much of a change to your code:

Option Explicit


Dim oFSO, oShell, oFld, zipFil
Dim strDir, strZip, strDay, strTime, strDate
Dim dteDateTime

dteDateTime = Now()

If Len(DatePart("d", dteDateTime) = 1) Then
      strDay = "0" & DatePart("d", dteDateTime)
Else
      strDay = DatePart("d", dteDateTime)
End If

strDate = Right(DatePart("yyyy", dteDateTime), 2) & DatePart("m", dteDateTime) & strDay & DatePart("h", dteDateTime) & DatePart("n", dteDateTime)

strDir = "c:\zipin\EDOC\"
strZip = "C:\backupzip\backup" & strDate & ".zip"

Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oShell = CreateObject("Shell.Application")
'Set oFile = oFSO.GetFile(strFile)
Set oFld = oFSO.GetFolder(strDir)

Set zipFil = oFSO.CreateTextFile(strZip)
zipFil.WriteLine Chr(80) & Chr(75) & Chr(5) & Chr(6) & String(18, 0)
zipFil.Close

oShell.NameSpace(strZip).CopyHere strDir

Do Until oShell.NameSpace(strZip).Items.Count = 1
	WScript.Sleep 500 
Loop

Set zipFil = Nothing
Set oShell = Nothing
Set oFld = Nothing
set oFso=Nothing

msgbox "finished"

wscript.quit

Open in new window


Just wrapped your sleep in a do loop checking for the number of items = 1
0
 
RobOwner (Aidellio)Commented:
BTW my code tested and working.  Also should note that the following won't work because the zip's top level has just the folder ie a count of 1, not the number of items in the folder you are copying:

fldInputFolder.Files.Count = fldOutputFolder.Items.Count
0
 
JezWaltersCommented:
Sorry to contradict tagit, but my script does very much work (also tested and working), as the fldOutputFolder variable is reassigned to include the top level EDOC subfolder before it is used:

Set fldOutputFolder = shlShell.NameSpace(strZipFile & "\" & fldInputFolder.Name)

Open in new window


For what it's worth (and absolutely no offense intended), tagit's solution only checks that the EDOC top level folder is in the zip - not that all the contents has been successfully copied over.

Tagit's script also only pads day numbers to 2 digits - which is the same as in your original script, of course!

Finally, and on a very minor point, you don't need the explicit "WScript.Quit" statement if it's the last line of your script file - the script ends when there are no more statements to execute.

Hope that helps - and very good luck!  :-)
0
 
RobOwner (Aidellio)Commented:
No offense taken. My point was to not change to much of the original code.
Correct,  The wscript.quit is superfluous but does no harm being there.
Both scripts have the same issue that only the count of items in the top level folder are compared.  In other words it doesn't do a recursive comparison. So both should work for you but be aware of this point
0
 
penguins_ruleAuthor Commented:
tagit answered the questions, JezWalters fixed the date time problem. Both work. Thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 4
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now