Solved

vbs script for zip a directory doesn't work consistently

Posted on 2013-11-25
10
599 Views
Last Modified: 2013-12-03
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
Comment
Question by:penguins_rule
  • 4
  • 4
  • 2
10 Comments
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
Comment Utility
Just before the last line put:
Msgbox "finished"
That will prompt the user to click ok
0
 
LVL 17

Expert Comment

by:JezWalters
Comment Utility
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
 
LVL 17

Expert Comment

by:JezWalters
Comment Utility
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
 
LVL 1

Author Comment

by:penguins_rule
Comment Utility
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
 
LVL 17

Assisted Solution

by:JezWalters
JezWalters earned 250 total points
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 42

Accepted Solution

by:
Rob Jurd, EE MVE earned 250 total points
Comment Utility
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
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
Comment Utility
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
 
LVL 17

Expert Comment

by:JezWalters
Comment Utility
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
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
Comment Utility
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
 
LVL 1

Author Closing Comment

by:penguins_rule
Comment Utility
tagit answered the questions, JezWalters fixed the date time problem. Both work. Thanks.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
This Micro Tutorial will teach you the basics of configuring your computer to improve its speed. It will also teach you how to disable programs that are running in the background simultaneously. This will be demonstrated using Windows 7 operating…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now