Solved

vbs script for zip a directory doesn't work consistently

Posted on 2013-11-25
10
620 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 43

Expert Comment

by:Rob
ID: 39676928
Just before the last line put:
Msgbox "finished"
That will prompt the user to click ok
0
 
LVL 17

Expert Comment

by:JezWalters
ID: 39676970
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
ID: 39676976
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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 1

Author Comment

by:penguins_rule
ID: 39678331
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
ID: 39678625
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
 
LVL 43

Accepted Solution

by:
Rob earned 250 total points
ID: 39679180
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 43

Expert Comment

by:Rob
ID: 39679193
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
ID: 39679519
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 43

Expert Comment

by:Rob
ID: 39679534
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
ID: 39693455
tagit answered the questions, JezWalters fixed the date time problem. Both work. Thanks.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The recent Microsoft changes on update philosophy for Windows pre-10 and their impact on existing WSUS implementations.
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
In this Micro Tutorial viewers will learn how to use Boot Corrector from Paragon Rescue Kit Free to identify and fix the boot problems of Windows 7/8/2012R2 etc. As an example is used Windows 2012R2 which lost its active partition flag (often happen…

861 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