Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error Handling for Zip Application

Status
Not open for further replies.

ajtsystems

IS-IT--Management
Jan 15, 2009
80
GB
Hi,

I have written a script which finds files older than X moves them and then zips the file. For work I am trying to integrate a delete function which deletes the files after a successfully zipped. I have used error handling where wscript.shell catches the error from 7-zip.exe and if it's not '0' log and kill the script.

During testing I killed 7-zip.exe with task manager but the files were still deleted... disasterous (potentially anyway...)

Does anyone know why this might be...

Script:

Dim WSshell

Set fso = CreateObject("Scripting.FileSystemObject")
Set objShell = wscript.createObject("wscript.shell")

'Install path of 7 Zip, ignor if using Environment variables
InstallPath = "c:\Program Files\7-Zip"

'Target Folder created on fly (and deleted, after zip (strDirectory) is created
myTargetFolder = "c:\store\" & replace(date,"/","_")

'Zip archive location ( Need to create c:\Archive manually )
'Can be a network drive
strDirectory = "c:\Archived_LogFiles\" & "DIONE_IIS_Logs_" & replace(date,"/","_")

'Folder to search through
mySourceFolder="C:\logs"

'wscript.echo strdirectory



'set demofolder = fso.GetFolder (strdirectory)

Set WSHShell = WScript.CreateObject("WScript.Shell")



set root=fso.getFolder(mySourceFolder)

call folderlist(root)

sub folderlist(grp)

call filelist(grp)

for each fldr in grp.subFolders

set nf=fso.GetFolder(fldr.path)

call folderlist(nf)

set nf=nothing

next

end sub


sub filelist(grp)




subfldr=myTargetFolder & mid(grp,len(mySourceFolder)+1)

if fso.folderExists(subfldr)=false then fso.CreateFolder(subfldr)

for each file in grp.files

if fileTest(file) = true then


file.move subfldr & "\"

end if

next

end sub

function fileTest(item)

fileTest=False

modDate=item.DateLastModified


If DateDiff("d", item.DateLastModified, Now) >0 Then

fileTest=True
end if

end function

call Sevenzip

sub Sevenzip

on error resume next

strCommand = "7za a -tzip -v100m " & strDirectory & " " & myTargetFolder
wscript.echo strCommand
objShell.CurrentDirectory = InstallPath

strRun = objShell.Run(strCommand, 0, True)

If Err <> 0 Then
wscript.echo Err.Number & ", " & Err.Description
set oTSOut = fso.openTextFile("c:\errors.txt", 8)
oTSOut.writeline date & " " & time & " Error Num- " & Err.Number & ", " & " Error Description " & " " & Err.Description
wscript.quit
Else
On Error Goto 0



call deleteFolder(MyTargetFolder)
end if
end sub



sub DeleteFolder(MyTargetFolder)

fso.DeleteFolder(MyTargetFolder)
set oTSOut = fso.openTextFile("c:\errors.txt", 8)
oTSOut.writeline date & " " & MyTargetFolder & " Has been Deleted"
oTSOut.writeline "=============================================================="
End sub


 
What happens when you don't kill 7zip with the task manager?
 
The archive is created and the source directory deleted.

strRun = objShell.Run(strCommand, 0, True)
True on the end of this line of code tells objshell.run to wait until the command being run has finished and collect any errors returned by the command.... E.g If 7-zip runs out of disk space or whatever.
 
My point is: your script is probably fine the way it is. Killing it with the task manager is not the best way to test for an error condition.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top