Jump to content

Move files based on date


Recommended Posts

I know this has been discussed before and I have read all that I could find but I am getting an error. Any help would be much appreciated.

I have two folders on the desktop: sampleTXT and Moved. The sampleTXT folder contains a few dozen text files. My goal is to sort through the files and move any older than 35 days over to the "Moved" folder.

Here is my text:

#include <File.au3>
#include <Array.au3>
#include <Date.au3>

Local $sPath = @DesktopDir & "\sampleTXT"

Local $aLogs = _FileListToArray($sPath, "*", 1)

For $i = 1 to $aLogs[0]
   $aDate = FileGetTime($sPath & $aLogs[$i], 0, 0)
   $aaDate = $aDate[0] & "/" & $aDate[1] & "/" & $aDate[2]
   If _DateDiff("D", $aaDate, _NowCalcDate()) > 35 Then FileMove($aLogs[$i], @DesktopDir & "\Moved")
Next

Here is the error I am getting:

Line 11 (File "xxxxxx")

$aaDate = $aDate[0] & "/" & $aDate[1] & "/" & $aDate[2]

$aaDate = $aDate^ ERROR

Error: Subscript used on non-accessible variable

Link to comment
Share on other sites

Try:

#include <File.au3>
#include <Array.au3>
#include <Date.au3>

Local $sPathIN = @DesktopDir & "\DirIN"
Local $sPathOUT = @DesktopDir & "\DirOUT"

Local $aLogs = _FileListToArray($sPathIN, "*", 1)
If Not @error And IsArray($aLogs) Then
    For $i = 1 To 0
        $aDate = FileGetTime($sPathIN & "\" & $aLogs[$i], 0, 0)
        If Not @error And IsArray($aDate) Then
            $aaDate = $aDate[0] & "/" & $aDate[1] & "/" & $aDate[2]
;~      FileMove($sPathIN & "\" & $aLogs[$i], $sPathOUT & "\", 1 + 8)
            If _DateDiff("D", $aaDate, _NowCalcDate()) > 35 Then FileMove($sPathIN & "\" & $aLogs[$i], $sPathOUT & "\", 1 + 8)
        EndIf
    Next
EndIf

 

Edited by Trong
add check error!

Regards,
 

Link to comment
Share on other sites

Thanks for the great response. Unfortunately, I am still getting a similar error. I didn't bother editing my script with your changes. Since they are so similar I copied and pasted and got a similar error:

Line 10

For $i = 1 To $aLogs[0]

For $i = 1 To $aLogs^ERROR

Error:P Subscript used on non-accessible variable.

I feel like I(we) are just missing something small. Any ideas?

Link to comment
Share on other sites

Local $aLogs = _FileListToArray($sPath, "*", 1)
If (Not @error) Then
    For $i = 1 To $aLogs[0]
        $aDate = FileGetTime($sPathIN & "\" & $aLogs[$i], 0, 0)
        If Not @error And IsArray($aDate) Then
            $aaDate = $aDate[0] & "/" & $aDate[1] & "/" & $aDate[2]
;~      FileMove($sPathIN & "\" & $aLogs[$i], $sPathOUT & "\", 1 + 8)
            If _DateDiff("D", $aaDate, _NowCalcDate()) > 35 Then FileMove($sPathIN & "\" & $aLogs[$i], $sPathOUT & "\", 1 + 8)
        EndIf
    Next
EndIf

 

Link to comment
Share on other sites

Thank you, thank you, thank you! So helpful. It is working. It looks like my FileGetTime statement was off. I have to look back at why I had it that way. Also my FileMove statement needed some work. Here is the final code that does the trick:

#include <File.au3>
#include <Array.au3>
#include <Date.au3>

Local $sPathIN = @DesktopDir & "\sampleTXT"
Local $sPathOUT = @DesktopDir & "\Moved"

Local $aLogs = _FileListToArray($sPathIN, "*", 1)
If (Not @error) Then
For $i = 1 to $aLogs[0]
   $aDate = FileGetTime($sPathIN & "\" & $aLogs[$i], 0, 0)
   If Not @error And IsArray($aDate) Then
       $aaDate = $aDate[0] & "/" & $aDate[1] & "/" & $aDate[2]
       If _DateDiff("D", $aaDate, _NowCalcDate()) > 35 Then FileMove($sPathIN & "\" & $aLogs[$i], $sPathOUT & "\", 1 + 8)
   EndIf
Next
EndIf

So grateful for your help. Thanks again!

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...