Jump to content

Move the "same" file to multiple folders


Recommended Posts

I have been using this script to move files older then 14 days to an archive folder. It works perfectly when the file name is unique in each of the folders and subfolders. Now I have a situation where the same file will be written to multiple folders. Currently, when I run this script it moves the file and overwrites it until I have only one copy left from the last folder alphabetically that the script touched. Is there a way to write this script to recreate the existing folder structure under the archive folder and write the old files to that location? That way file sales00007 which is generated daily in each of the sales reps folders will be archived.

[/code]; Script Function:
; Move files older than 14 days 
#include <date.au3>
AutoItSetOption ( "RunErrorsFatal", 0) 
;AutoItSetOption ( "TrayIconHide", 1) 
Break(0)
$Hotkey = "{Pause}"
HotKeySet ($Hotkey ,"StopExec") 
$L_Msg = ""
; init variables
$Version = "0.2"
$dir="C:\salesreports"
$Dircount = 0
$n_FileCount = 0
$n_FileSize = 0

;$begin = TimerStart()
$rc = _Remove_Files($Dir, 1,14)
Exit

;===============================================================================
;
;===============================================================================

Func _Remove_Files($T_Dir,$T_Incl_SubDir,$T_Days)
   Dim $n_Dirnames[200000][3]; max number of directories that can be counted
   Local $n_DirCount = 0
   Local $n_File
   Local $n_Search
   Local $n_tFile
   Local $n_Fdate
   Writelog("### Start run ###")
   $T_DirCount = 1
   $T_FileCount = 0
   $T_FileSize = 0
 ; remove the end \
   If StringRight($T_Dir,1) = "\" then $T_Dir = StringTrimRight($T_Dir,1)
   $n_Dirnames[$T_DirCount][0] = $T_Dir
   $n_Dirnames[$T_DirCount][1] = 0
   $T_Incl_SubDir = Int($T_Incl_SubDir)
 ; Exit if base dir doesn't exists
   If Not FileExists($T_Dir) then Return 0
 ; keep on looping until all directories are counted
   While $T_DirCount > $n_DirCount
      $n_DirCount = $n_DirCount + 1
      $n_Search = FileFindFirstFile($n_Dirnames[$n_DirCount][0] & "\*.*" )
      If $n_Search = -1 Then
          ContinueLoop
      EndIf
      ShowMenu("Processing directory:" & @LF & $n_Dirnames[$n_DirCount][0],0)
      While 1
         $n_File = FileFindNextFile($n_Search)
         If @error Then ExitLoop
         $n_tFile = $n_Dirnames[$n_DirCount][0] & "\" & $n_File
         ShowMenu("        File  :" & $n_File,2)
       ; skip these references
         if $n_File = "." or $n_File = ".." then
            ContinueLoop
         EndIf
       ; if Directory than add to the list to be processed later
         If StringInstr(FileGetAttrib ( $n_tFile ),"D") > 0 then
            If $T_Incl_SubDir = 1 then
               $T_DirCount = $T_DirCount + 1
               $n_Dirnames[$T_DirCount][0] = $n_tFile
               $n_Dirnames[$T_DirCount][1] = 0
            EndIf
         Else
    ; Check age of the file
            $n_Fdate = FileGetTime($n_tFile,0)
            $sdate=$n_Fdate[0] & "/" & $n_Fdate[1] & "/" & $n_Fdate[2] & " " &  $n_Fdate[3] & ":" & $n_Fdate[4] & ":" & $n_Fdate[5]
            $edate=@YEAR & "/" & @MON & "/" & @MDAY & " " &  @HOUR & ":" & @MIN & ":" & @SEC
            If _DateDiff ('d',$sdate,$eDate) > $T_Days then
                $rc = FileMove ($n_tFile, "c:\archive\salesreports")
           ;               $rc = FileRecycle($n_tFile)
               Writelog($sdate  & " " & _DateDiff ('d',$sdate,$eDate) & " *** rc:" & $rc & " move-> "  & $n_tFile)
            Else
           ; add 1 to filecount for this directory
             ;Writelog(" Keep -> "  & $n_tFile)
               Writelog(" Keep -> "  & $n_tFile & "  ModifiedDate:" & $sdate & "  Days old:"  & _DateDiff ('d',$sdate,$eDate))
               $n_Dirnames[$n_DirCount][1] = $n_Dirnames[$n_DirCount][1] + 1
            EndIf
         EndIf
      Wend
      FileClose($n_Search)
   Wend
   Writelog("### Removing empty directories ###")
   For $x = $T_DirCount to 2 step -1
       Writelog("Dir: " & $n_Dirnames[$x][0] & "  Files:" & $n_Dirnames[$x][1])
       If $n_Dirnames[$x][1] = 0 then
          If "user\hidden" <> StringRight($n_Dirnames[$x][0],11) then
            DirRemove ($n_Dirnames[$x][0])
            Writelog("Remove Dir: " & $n_Dirnames[$x][0])
          EndIF
       EndIf
   Next
   Writelog("### End Run ###")
   Return ( 1 )
EndFunc
;
;======================================
; Stop executing when Hotkey is pressed
;======================================
Func StopExec()
   $mrc=Msgbox(4,"Cancel Execution?","You pressed " & $Hotkey & ". Do you want to cancel the Program?")
   if $mrc = 6 then
      writelog("*** Utility Cancelled by operator.")
      Exit
   EndIf
   Return
EndFunc
;=====================================================
; Show Splash screen or update its content
;=====================================================
Func ShowMenu($g_msg,$concat)
   $W_Title=" file remove Utility (ver:" & $Version & ")             Press " & $Hotkey & " to stop execution"
   $D_msg = ""
   If $concat > 0 Then
      If $concat = 1 Then
         $L_msg = $L_msg & @LF & $g_msg
         $d_msg = $L_msg
      Else
         $d_msg = $L_msg & @LF & $g_msg
      EndIf
   Else
      $L_msg = $g_msg
      $d_msg = $g_msg
   EndIf
   if WinExists($W_Title) then
      ControlSetText($W_Title,"","Static1",$d_msg)
   else
      SplashTextOn($W_Title,$d_msg,800,100,1,1,6,"Courier",10,600)
   endif
   Return 0
EndFunc
;==============================
; Write to logfile
;==============================
Func WriteLog($msg)
   $msg =  @YEAR & "/" & @MON & "/" & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC & " " & "==>" & $msg
   FileWriteLine(@ScriptDir & "\RemFiles3.log", $msg )
   Return 0
EndFunc[code]
Link to comment
Share on other sites

I have been using this script to move files older then 14 days to an archive folder. It works perfectly when the file name is unique in each of the folders and subfolders. Now I have a situation where the same file will be written to multiple folders. Currently, when I run this script it moves the file and overwrites it until I have only one copy left from the last folder alphabetically that the script touched. Is there a way to write this script to recreate the existing folder structure under the archive folder and write the old files to that location? That way file sales00007 which is generated daily in each of the sales reps folders will be archived.

Your code is processing the first folder, and for each subfolder it finds, it adds it to the array of folders to be processed, then processes each of those in turn etc. etc. Seems like one approach would be to prepend the current folder name to any new folders added to the array, as a way to get hold of the current path, then use that path to check/create folders as necessary before moving files to the archive folder.

So here:

If StringInstr(FileGetAttrib ( $n_tFile ),"D") > 0 then
            If $T_Incl_SubDir = 1 then
               $T_DirCount = $T_DirCount + 1
               $n_Dirnames[$T_DirCount][0] = $n_tFile
               $n_Dirnames[$T_DirCount][1] = 0
            EndIf
         Else

change $n_Dirnames[$T_DirCount][0] = $n_tFile to be $n_Dirnames[$T_DirCount][0] = $n_Dirnames[$n_DirCount][0] & "\" & $n_tFile.

And here:

$rc = FileMove ($n_tFile, "c:\archive\salesreports")

put code before this that tests to see if StringReplace($n_Dirnames[$n_DirCount][0], "C:\salesreports", "c:\archive\salesreports") exists, and if not creates the appropriate subfolders - maybe in a Func that StringSplits the current folder, tests each level in turn, and when it gets to the subfolder(s) that don't exist, creates them.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...