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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324 | #Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_outfile=archiver2.exe
#AutoIt3Wrapper_Compression=4
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#cs
Archiver.
Alex Belenkiy - ab@nyitsupport.com
07/24/2008
This script was created with a purpose of archiving old files from one location to another.
To use, select the date of files to be archived, files on or before that day will be affected.
Type the source folder, all files within the source will be affected. Folder structure will remain.
Select whether to copy, or move the files.
Select whether to create shortcuts to the moved files in the source folder.
There is also an undo function that will put files back based on the log.
RecursiveFileSearch function was modified from http://www.autoitscript.com/forum/index.php?showtopic=58558&st=0
#ce
#include <array.au3>
#include <file.au3>
#include <GUIConstants.au3>
AutoItSetOption("TrayIconHide", 1)
dim $LogArray
dim $jobRunning = "0"
dim $PreSetDate = @YEAR & @MON & @MDAY
dim $errorSet = "0"
dim $fileMove, $Array, $copyMove, $destFolder, $fileMove, $FilePathSplit, $NewDestFolder, $oldFile, $OldFolder, $OldPathSplit
dim $FileTotalSize, $fileSize
dim $FileModDate
dim $UNC
Opt("GUIOnEventMode", 1)
#Region ### START Koda GUI section ### Form=\\SRV1\ab\my documents\dev\nyitsupport\archiver\frmarchiver.kxf
$frmArchiver = GUICreate("Archiver", 395, 243, 343, 243)
GUISetOnEvent($GUI_EVENT_CLOSE, "frmArchiverClose")
GUISetOnEvent($GUI_EVENT_MINIMIZE, "frmArchiverMinimize")
GUISetOnEvent($GUI_EVENT_MAXIMIZE, "frmArchiverMaximize")
GUISetOnEvent($GUI_EVENT_RESTORE, "frmArchiverRestore")
$lblDate = GUICtrlCreateLabel("Select files modified on or before:", 8, 8, 176, 21, 0x01)
$Date1 = GUICtrlCreateDate("", 192, 8, 194, 21,0x00010000)
GUICtrlSetOnEvent(-1, "Date1Change")
$btnSource = GUICtrlCreateButton("Source Folder", 8, 40, 97, 21, 0)
GUICtrlSetOnEvent(-1, "btnSourceClick")
$btnDest = GUICtrlCreateButton("Destination Folder", 8, 70, 97, 21, 0)
GUICtrlSetOnEvent(-1, "btnDestClick")
$radMove = GUICtrlCreateRadio("Move", 8, 104, 65, 25)
GUICtrlSetOnEvent(-1, "radMoveClick")
$radCopy = GUICtrlCreateRadio("Copy", 72, 104, 65, 25)
GUICtrlSetState(-1, $GUI_CHECKED)
GUICtrlSetOnEvent(-1, "radCopyClick")
$chkShortcuts = GUICtrlCreateCheckbox("Create Shortcuts In Source Folder", 144, 104, 241, 25)
GUICtrlSetOnEvent(-1, "chkShortcutsClick")
GUICtrlSetState(-1, $GUI_DISABLE)
$btnOK = GUICtrlCreateButton("OK", 40, 136, 148, 49)
GUICtrlSetFont(-1, 14, 800, 0, "MS Sans Serif")
GUICtrlSetOnEvent(-1, "btnOKClick")
$btnUndo = GUICtrlCreateButton("Undo", 216, 136, 140, 49)
GUICtrlSetFont(-1, 14, 400, 0, "MS Sans Serif")
GUICtrlSetOnEvent(-1, "btnUndoClick")
$txtSource = GUICtrlCreateInput("", 112, 40, 273, 21)
$txtDest = GUICtrlCreateInput("", 112, 70, 273, 21)
$lblStatus = GUICtrlCreateLabel("", 8, 192, 380, 25, 0x01)
$lblCredits1 = GUICtrlCreateLabel("Alex Belenkiy", 8, 224, 76, 17)
GUICtrlSetFont(-1, 6, 400, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0x008080)
$lblCredits2 = GUICtrlCreateLabel("ab@nyitsupport.com", 277, 224, 108, 17)
GUICtrlSetFont(-1, 6, 400, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0x008080)
GUICtrlSetState($btnOK, $GUI_DISABLE)
GUICtrlSetState($btnDest, $GUI_DISABLE)
GUICtrlSetState($txtSource, $GUI_DISABLE)
GUICtrlSetState($txtDest, $GUI_DISABLE)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
GuiCtrlSendMsg($Date1, 0x1032, 0, "yyyyMMdd")
GuiSetState()
While GuiGetMsg() <> $GUI_EVENT_CLOSE
WEnd
While 1
Sleep(100)
WEnd
Func btnOKClick()
$FileTotalSize = 0
$fileSize = 0
$PreSetDate = GUICtrlRead($Date1)
$sourceFolder = GUICtrlRead($txtSource)
$destFolder = GUICtrlRead($txtDest)
if GUICtrlRead($radCopy) = $GUI_CHECKED then $copyMove = "Copy"
if GUICtrlRead($radMove) = $GUI_CHECKED then $copyMove = "Move"
if GUICtrlRead($chkShortcuts) = $GUI_CHECKED then
$shortCut = "yes"
Else
$shortCut = "no"
EndIf
$jobRunning = "1"
GUICtrlSetState($txtSource, $GUI_DISABLE)
GUICtrlSetState($btnSource, $GUI_DISABLE)
GUICtrlSetState($btnDest, $GUI_DISABLE)
GUICtrlSetState($btnOK, $GUI_DISABLE)
GUICtrlSetState($btnUndo, $GUI_DISABLE)
GUICtrlSetState($Date1, $GUI_DISABLE)
GUICtrlSetState($frmArchiver, $GUI_DISABLE)
GUICtrlSetState($radCopy, $GUI_DISABLE)
GUICtrlSetState($radMove, $GUI_DISABLE)
GUICtrlSetState($chkShortcuts, $GUI_DISABLE)
GUICtrlSetData($lblStatus, "Please wait for the operation to complete.")
If StringRight($sourceFolder, 1) <> "\" Then $sourceFolder &= "\"
If StringRight($destFolder, 1) <> "\" Then $destFolder &= "\"
$logFile = FileOpen ("Archiver - " & $CopyMove & " - " & @MON & "-" & @MDAY & "-" & @YEAR & " - " & @HOUR & "-" & @MIN & ".csv",9)
FileWriteLine ($logFile, "Operation,Shortcut Created,Date,Time,File Size,File Modified Date,Source Folder,Source File,Destination Folder")
$Array = RecursiveFileSearch($sourceFolder)
For $a = 1 To $Array[0]
$NewDestFolder = $destFolder
$FilePathSplit = StringSplit($Array[$a], "\")
if StringInStr($Array[$a], "\\") <> 0 then $UNC = 4
if StringInStr($Array[$a], ":") <> 0 then $UNC = 2
for $b = $UNC to $FilePathSplit[0] - 1
$NewDestFolder = $NewDestFolder & $FilePathSplit[$b] & "\"
Next
$oldFile = $FilePathSplit[$FilePathSplit[0]]
$OldPathSplit = StringSplit($Array[$a], "\")
$OldFolder = StringLeft($Array[$a], StringLen($Array[$a]) - StringLen($OldPathSplit[$OldPathSplit[0]]))
$FileTotalSize = $FileTotalSize + $fileSize
$fileSize = FileGetSize ($Array[$a])
$FileModDate = StringLeft (FileGetTime ($array[$a], 0,1), 8)
if $copyMove = "Move" Then $fileMove = FileMove($Array[$a], $NewDestFolder , 9)
if $copyMove = "Copy" Then $fileMove = FileCopy($Array[$a], $NewDestFolder , 9)
if $fileMove = 1 then FileWriteLine ($logFile, $CopyMove & "," & $ShortCut & "," & @MON & "/" & @MDAY & "/" & @YEAR & "," & @HOUR & ":" & @MIN & ":" & @SEC & "," & $fileSize & "," & $FileModDate & "," & $OldFolder & "," & $oldFile & "," & $NewDestFolder)
if $ShortCut = "yes" then FileCreateShortcut($NewDestFolder & $oldFile, $OldFolder & $oldFile, $NewDestFolder, "", "Link to file that was moved by ARCHIVER")
GUICtrlSetData($lblStatus, "Processing " & $a & " / " & $array[0] & @crlf & $array[$a])
Next
FileWriteLine($logFile, "-------")
FileWriteLine($logFile, "Number of files:" & $Array[0])
FileWriteLine($logFile, "Total size of files: " & Round($FileTotalSize / 1048576, 3) & " MB" & " / " & Round($FileTotalSize / 1073741824, 3) & " GB" )
FileClose($logFile)
$jobRunning = "0"
GUICtrlSetState($btnSource, $GUI_ENABLE)
GUICtrlSetState($btnDest, $GUI_ENABLE)
GUICtrlSetState($btnOK, $GUI_ENABLE)
GUICtrlSetState($btnUndo, $GUI_ENABLE)
GUICtrlSetState($Date1, $GUI_ENABLE)
GUICtrlSetState($frmArchiver, $GUI_ENABLE)
GUICtrlSetState($radCopy, $GUI_ENABLE)
GUICtrlSetState($radMove, $GUI_ENABLE)
if GUICtrlRead($radMove) = $GUI_CHECKED Then GUICtrlSetState($chkShortcuts, $GUI_ENABLE)
GUICtrlSetData($lblStatus, "")
MsgBox(0, "Job Complete", "Number of files:" & $Array[0] & @CRLF & "Total size:" & @CRLF & Round($FileTotalSize / 1048576, 3) & " MB" & @CRLF & Round($FileTotalSize / 1073741824, 3) & " GB")
EndFunc
Func btnUndoClick()
dim $undoCount
$logPath = FileOpenDialog("Select log file with undo information", @WorkingDir, "(*.csv)", 1)
if not @error Then
$UndoLog = FileOpen ("Archiver - UNDO - " & @MON & "-" & @MDAY & "-" & @YEAR & " - " & @HOUR & "-" & @MIN & ".csv",9)
FileWriteLine ($UndoLog, "Status,Archived Folder,Archived File,Restore Folder")
$jobRunning = "1"
GUICtrlSetState($btnSource, $GUI_DISABLE)
GUICtrlSetState($btnDest, $GUI_DISABLE)
GUICtrlSetState($btnOK, $GUI_DISABLE)
GUICtrlSetState($btnUndo, $GUI_DISABLE)
GUICtrlSetState($Date1, $GUI_DISABLE)
GUICtrlSetState($frmArchiver, $GUI_DISABLE)
GUICtrlSetState($radCopy, $GUI_DISABLE)
GUICtrlSetState($radMove, $GUI_DISABLE)
GUICtrlSetState($chkShortcuts, $GUI_DISABLE)
$logFile = FileOpen($logPath,0)
$undoCount = _FileCountLines($logPath)
dim $undoArray[$undoCount]
_FileReadToArray($logPath, $undoArray)
for $a = 2 to $undoArray[0] - 3
if $undoArray[$a] = "-------" then ExitLoop
$uSplit = StringSplit($undoArray[$a], ",") ;Operation[1],Shortcut Created[2],Date[3],Time[4],File Size[5],File Date[6],Source Folder[7],Source File[8],Destination Folder[9]
if FileExists($uSplit[7] & $uSplit[8]) Then
$fileSizeSource = FileGetSize($uSplit[9] & $uSplit[8]) ; archive location
$fileSizeDest = FileGetSize($uSplit[7] & $uSplit[8]) ; original location
$fileModSource = StringLeft(FileGetTime($uSplit[9] & $uSplit[8], 0, 1),8)
$fileModDest = StringLeft(FileGetTime($uSplit[7] & $uSplit[8],0,1),8)
$replace = MsgBox(36, "File Exists", "Would you like to replace the existing file?" & @CRLF & $uSplit[7] & $uSplit[8] & @CRLF & "Size: " & $fileSizeDest & @CRLF & "Date: " & $fileModDest & @CRLF & @crlf & "With archived file?" & @CRLF & "Size: " & $fileSizeSource & @CRLF & "Date: " & $fileModSource & @CRLF & @crlf & "Will skip the file in 30 seconds", 30)
if $replace = 6 Then
$fileDelete = FileMove($uSplit[9] & $uSplit[8], $uSplit[7],9)
if $fileDelete = 1 then
if $uSplit[2] = "yes" then FileDelete($uSplit[7] & $uSplit & ".lnk")
FileWriteLine($UndoLog, "Success - Replaced existing file" & "," & $uSplit[9] & "," & $uSplit[8] & "," & $uSplit[7])
Else
FileWriteLine($UndoLog, "Failed - Could not replace existing file" & "," & $uSplit[9] & "," & $uSplit[8] & "," & $uSplit[7])
EndIf
Else
FileWriteLine($UndoLog, "Failed - Existing file replace prompt declined or timed out" & "," & $uSplit[9] & "," & $uSplit[8] & "," & $uSplit[7])
EndIf
Else
$filedelete = FileMove($uSplit[9] & $uSplit[8], $uSplit[7],9)
if $fileDelete = 1 Then
if $uSplit[2] = "yes" then FileDelete($uSplit[7] & $uSplit[8] & ".lnk")
FileWriteLine($UndoLog, "Success - Replaced moved file" & "," & $uSplit[9] & "," & $uSplit[8] & "," & $uSplit[7])
Else
FileWriteLine($UndoLog, "Failed - Could not replace moved file" & "," & $uSplit[9] & "," & $uSplit[8] & "," & $uSplit[7])
EndIf
EndIf
GUICtrlSetData($lblStatus, "Processing " & $a & " / " & $undoArray[0] - 4 & @CRLF & $uSplit[7] & $uSplit[8])
Next
FileClose($UndoLog)
$jobRunning = "0"
GUICtrlSetState($btnSource, $GUI_ENABLE)
GUICtrlSetState($btnDest, $GUI_ENABLE)
GUICtrlSetState($btnOK, $GUI_ENABLE)
GUICtrlSetState($btnUndo, $GUI_ENABLE)
GUICtrlSetState($Date1, $GUI_ENABLE)
GUICtrlSetState($frmArchiver, $GUI_ENABLE)
GUICtrlSetState($radCopy, $GUI_ENABLE)
GUICtrlSetState($radMove, $GUI_ENABLE)
if GUICtrlRead($radMove) = $GUI_CHECKED Then GUICtrlSetState($chkShortcuts, $GUI_ENABLE)
GUICtrlSetData($lblStatus, "")
MsgBox(0, "Undo Completed", "Completed " & $undoArray[0] - 4 & " Files." & @CRLF & "Please check the UNDO log file for any errors.")
EndIf
EndFunc
Func chkShortcutsClick()
EndFunc
Func Date1Change()
EndFunc
Func frmArchiverClose()
if $jobRunning <> "1" then Exit
EndFunc
Func frmArchiverMaximize()
EndFunc
Func frmArchiverMinimize()
EndFunc
Func frmArchiverRestore()
EndFunc
Func radCopyClick()
GUICtrlSetState($chkShortcuts, $GUI_DISABLE)
GUICtrlSetState($chkShortcuts, $GUI_UNCHECKED)
EndFunc
Func radMoveClick()
GUICtrlSetState($chkShortcuts, $GUI_ENABLE)
GUICtrlSetState($chkShortcuts, $GUI_CHECKED)
EndFunc
Func btnDestClick()
$destFolder = FileSelectFolder("Destination Folder", "",5)
if not @error then
$c = StringSplit($destFolder,"\")
if not @error Then
GUICtrlSetState($btnOK, $GUI_ENABLE)
GUICtrlSetData($txtDest, $destFolder)
EndIf
EndIf
EndFunc
Func btnSourceClick()
GUICtrlSetState($btnDest, $GUI_DISABLE)
$sourceFolder = FileSelectFolder("Source Folder", "",4)
if not @error then
$c = StringSplit($sourceFolder,"\")
if not @error Then
GUICtrlSetState($btnDest, $GUI_ENABLE)
GUICtrlSetData($txtSource, $sourceFolder)
EndIf
EndIf
EndFunc
Func RecursiveFileSearch($RFSstartDir, $RFSpattern = ".", $RFSdepth = 0)
;Ensure starting folder has a trailing slash
If StringRight($RFSstartDir, 1) <> "\" Then $RFSstartDir &= "\"
If $RFSdepth = 0 Then
;Get count of all files in subfolders for initial array definition
$RFSfilecount = DirGetSize($RFSstartDir, 1)
Global $RFSarray[$RFSfilecount[1] + 1]
EndIf
$RFSsearch = FileFindFirstFile($RFSstartDir & "*.*")
If @error Then Return
;Search through all files and folders in directory
While 1
$RFSnext = FileFindNextFile($RFSsearch)
If @error Then ExitLoop
;If folder, recurse
If StringInStr(FileGetAttrib($RFSstartDir & $RFSnext), "D") Then
RecursiveFileSearch($RFSstartDir & $RFSnext, $RFSpattern, $RFSdepth + 1)
Else
If StringRegExp($RFSnext, $RFSpattern, 0) Then
;Append filename to array
$FileDate = StringLeft (FileGetTime ($RFSstartDir & "\" & $RFSnext, 0,1), 8)
if $FileDate <= $PreSetDate Then
$RFSarray[$RFSarray[0] + 1] = $RFSstartDir & $RFSnext
$RFSarray[0] += 1
EndIf
;Increment filecount
EndIf
EndIf
WEnd
FileClose($RFSsearch)
If $RFSdepth = 0 Then
Redim $RFSarray[$RFSarray[0] + 1]
Return $RFSarray
EndIf
EndFunc ;==>RecursiveFileSearch
|