Thursday, July 24, 2008

Archiver – Archive out older files by modified date.

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.

Please let me know if there are any bugs in it.

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_outfile=archiver.exe
#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=\\ny1221fs01\abelenkiy$\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, $SS_CENTER)
$Date1 = GUICtrlCreateDate("2008/07/14 17:59:24", 192, 8, 194, 21, $WS_TABSTOP)
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, $SS_CENTER)
$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