forked from sstent/Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSFTP File Manager-Listings_arrayzip.vbs
More file actions
265 lines (225 loc) · 9.69 KB
/
SFTP File Manager-Listings_arrayzip.vbs
File metadata and controls
265 lines (225 loc) · 9.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''SFTP Folder Management Script'''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Author: Stuart Stent
'LastUpdated: 9/24/10
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Description:
'This script looks in a root directory and for each subdirectory
'under that folder it manages the files in folders one level below that.
'
'i.e SrootSource = e:\BulkuploadFiles + \Username + \sAppendSource \ managedfolders
'
'For each of the managed folders three passes are run
' 1 - Delete files older than that defined in MaxAge -- eg. kill all files older than 60 days
' 2 - Zip remaining files in to archives - by week (named by final day of week - sunday)
' 3 - Deletes zip files older than MaxAge
' 4 - finally, check each folder to ensure it is within the prescribed size limit. If not delete old zip files until under defined limit
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Define Constants and Initialise environment
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Const sRootSource = "C:\TESTING\Bulkuploadfiles" 'top level folders
Const sAppendSource = "\Listings\" ' subdir to append to subdirs of sRootSource
const sWinzipLocation = "C:\Progra~1\WinZip\WZZIP.EXE -e0 " 'needs a trailing space-
Const MaxAgeInterval = "D" 'Accepts D for days or M for Months
Const ConstMaxAge = 60 'Maximum intervals to keep a file -- see MaxAgeInterval
Const WeeksofActiveFiles = 0 'number of weeks (over the current week) of active files (not zipped) to keep --zero = this week only
Const MaxSize = 524288000 'Size in bytes -- Size in MB * 1024 * 1024 (524288000 = 500MB)
DIM MaxAge
DIM Ziprun
arrNames = Array()
dim filenames
Const TESTRUN = false 'Log stuff only, don't delete anything
' note: testrun will show duplicate files being zipped etc due to order of operations
' Turn Modules on/off
Const CleanUp = false 'Delete files older than MaxAge before zipping
Const ZipFiles = true 'Zip files into weekly packages
Const CheckZipFiles = true 'clean up zip files older than maxage
Const FoldersizeQuotas = true 'Cull old zip files to meet maximum foldersize
'EXCEPTIONS
'Seller ID BNA0000003 will carry different rules:
'-files will be kept for 30 days, regardless of folder size
Const Exceptions = True
XFolder = "BNA0000003"
XMaxAge = 30 'Maximum intervals to keep a file (on exception match) -- see MaxAgeInterval
'Setup Environment
Set objShell = wscript.createObject("wscript.shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oLogFile = oFSO.OpenTextFile(oFSO.GetParentFolderName(WScript.ScriptFullName) & "\Listings_Cleanup_Log-"&Year(now()) & Right("0" & Month(now()), 2) & Right("00" & Day(now()), 2) &".csv", 8, True, -2)
oLogFile.Write "<----------------------Script started at: " & Now() & "---------------------->" & vbCrLf
if TESTRUN then oLogFile.Write "<----------------------TESTING MODE---------------------->" & vbCrLf
if TESTRUN then oLogFile.Write "note: testrun will show duplicate files being zipped etc due to order of operations" & vbCrLf
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''' DO NOT EDIT BELOW THIS LINE'''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
ListFolder oFSO.GetFolder(sRootSource)
Sub ListFolder (oRootFldr)
For Each oSubfolder In oRootFldr.SubFolders
if oSubfolder.name = XFolder and Exceptions then
MaxAge = XMaxAge
oLogFile.Write "ExceptionMatched,"& oSubfolder & vbCrLf
Else
MaxAge = ConstMaxAge
end if
if oFSO.FolderExists(oSubfolder & sAppendSource) Then
IterateSubfolders oFSO.GetFolder(oSubfolder & sAppendSource)
end if
Next
End Sub
Sub IterateSubfolders (oFldr)
For Each oSubfolder In oFldr.Subfolders
if CleanUp then
CheckFolder_Delete(oSubfolder) ' deletes files older than MaxAge(ConstMaxAge or XMaxAge)
end if
if ZipFiles then
CheckFolder_Zip(oSubfolder) 'zips up files into weekly archives
end if
if CheckZipFiles then
CheckOld_Zip (oSubfolder) 'delete zips older than maxage
end if
if FoldersizeQuotas and not oSubfolder.name = XFolder then
CheckFoldersize(oSubfolder) 'deletes old zip files until MaxSize reached
end if
Next
End Sub
Sub CheckFolder_Delete (oFldr)
For Each oFile In oFldr.Files
if not (Right(oFile.Name, 3)) = "zip" then
If DateDiff(MaxAgeInterval, oFile.DateCreated, Now()) > MaxAge Then
oLogFile.Write "Deleted_OLD_File," & MaxAge & "," & oFile & vbCrLf
if not TESTRUN then oFile.Delete
End If
end if
Next
End Sub
Sub CheckFolder_Zip (oFldr)
'determine number of weeks of zip files
oldestZipDate = DateAdd(MaxAgeInterval,-(MaxAge), now())
ZipIterations = DateDiff("w",oldestZipDate,now())
i = ZipIterations + 5
j = 0
do until i = WeeksofActiveFiles
'caluate ZipAge from WeeksofActiveFiles requirement and todays date
ZipAge = ((WeekDay(Date, vbSaturday))-1)
Ziprun=0
j=0
ReDim arrNames(0)
'modify ZipAge keep current week + x weeks of active(unzipped) files
if ZipAge = 0 then ZipAge = ((i-2) * 7) else ZipAge = ZipAge + 7 + ((i-2) * 7)
For Each oFile In oFldr.Files
if not (Right(oFile.Name, 3)) = "zip" then
If DateDiff("D", oFile.DateLastModified, Now()) >= ZipAge Then
nDate = DateAdd("d", (0-(ZipAge+6)), now())
sDate = Year(nDate) & Right("0" & Month(nDate), 2) & Right("00" & Day(nDate), 2)
zipName = oFldr & "\WK_" & sDate & ".zip " 'zipfilename
if TESTRUN and not DateDiff(MaxAgeInterval, oFile.DateLastModified, Now()) > MaxAge and not DateDiff("D", oFile.DateLastModified, Now()) >= (ZipAge + 7) Then
oLogFile.Write "Zipped_File_1," & oFile & "," & zipName & vbCrLf
end if
if not TESTRUN then
Ziprun=1
end if
if j=UBound(arrNames) then
redim preserve arrnames(j+10)
arrnames(j+1) = " "
arrnames(j+2) = " "
arrnames(j+3) = " "
arrnames(j+4) = " "
arrnames(j+5) = " "
arrnames(j+6) = " "
arrnames(j+7) = " "
arrnames(j+8) = " "
arrnames(j+9) = " "
arrnames(j+10) = " "
end if
oLogFile.Write "Zipped_File," & oFile & "," & zipName & vbCrLf
arrNames(j)=oFile
j=j+1
End If
End If
Next
if Ziprun = 1 then
For x = 0 To UBound(arrNames)-10 Step 10
filenames=filenames & " " & arrNames(x) & " " & arrNames(x+1) & " " & arrNames(x+2) & " " & arrNames(x+3) & " " & arrNames(x+4) & " " & arrNames(x+5) & " " & arrNames(x+6) & " " & arrNames(x+7) & " " & arrNames(x+8) & " " & arrNames(x+9)
strCommand = sWinzipLocation & zipName & filenames
oLogFile.Write "x=" & x & " UBound(arrNames)-5 =" & UBound(arrNames)-10 & vbCrLf
rem oLogFile.Write strCommand & vbCrLf
rem strRun = objShell.Run(strCommand, 0, True)
filenames=""
for n = 0 to 9
if x+n<=UBound(arrNames) and not arrNames(x+n) = " " then
Set MyFile=oFSO.GetFile(""& arrNames(x+n) &"")
MyFile.Delete
end if
next
Next
rem ofile.Delete
end if
erase arrNames
redim arrNames(0)
i = i - 1
Loop
End Sub
Sub CheckOld_Zip (oFldr)
'determine number of weeks of zip files
oldestZipDate = DateAdd(MaxAgeInterval,-(MaxAge), now())
NumberOfZips = DateDiff("w",oldestZipDate,now())
'init array
dim ZipNamesArray()
redim ZipNamesArray(NumberOfZips)
'calculate zip names
i = 0
do until i = NumberOfZips
ZipAge = ((WeekDay(Date, vbSaturday))-1)
'modify ZipAge keep current week + x weeks of active(unzipped) files
if ZipAge = 0 then ZipAge = ((i-2) * 7) else ZipAge = ZipAge + 7 + ((i-2) * 7)
nDate = DateAdd("d", (0-(ZipAge+6)), now())
sDate = Year(nDate) & Right("0" & Month(nDate), 2) & Right("00" & Day(nDate), 2)
zipName = oFldr & "\WK_" & sDate & ".zip" 'zipfilename
ZipNamesArray(i) = zipName 'add zipfilename to array
i = i + 1
loop
' check each file to see if it matches any of the 'safe' zip names
For Each oFile in oFldr.files
toDelete = 0
if (Right(oFile.Name, 3)) = "zip" then
i=0
do until i = NumberOfZips
if not oFile = ZipNamesArray(i) then toDelete = toDelete + 1
i = i + 1
loop
end if
if toDelete = NumberOfZips then
oLogFile.Write "Deleted_OLD_Zip," & oFile & vbCrLf
if not TESTRUN then oFSO.DeleteFile(oFile)
end if
next
End Sub
Sub CheckFoldersize(oFldr)
i = 0
subfolderSize = Int(oFldr.Size)
if TESTRUN then
if subfolderSize > MaxSize then oLogFile.Write "Folder_Over_Quota," & oFldr & vbCrLf
end if
Do While subfolderSize > MaxSize And i < 100 and not TESTRUN
OldestFile = ""
dtmOldestDate = Now
For Each oFile in oFldr.files
if (Right(oFile.Name, 3)) = "zip" then
dtmFileDate = oFile.DateLastModified
If dtmFileDate < dtmOldestDate Then
dtmOldestDate = dtmFileDate
OldestFile = oFile
End If
end if
Next
oLogFile.Write "Deleted_OLDEST_File," & OldestFile & vbCrLf
if not TESTRUN then
if not OldestFile = "" then oFSO.DeleteFile(OldestFile)
end if
subfolderSize = Int(oFldr.Size)
i = i + 1
Loop
End Sub
oLogFile.Write "<----------------------Script ended at: " & Now() & "---------------------->" & vbCrLf
oLogFile.Close