ajtsystems
IS-IT--Management
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
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