Move files based on file date

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Chris70
Posts: 46
Joined: 17 Dec 2019, 12:25

Move files based on file date

05 Aug 2020, 10:22

Hello AHKers! It's been a while since I worked with AHK scripts and I am a bit rusty. I tried searching the forum to find my exact issue, but haven't had any luck, so I am going to ask for help.

Here is what I want to do: search a folder for any files modified prior to the current month and move them to another folder (essentially creating an archive folder for prior month's files. Here is the script I wrote, but it doesn't seem to work as I envisioned as it seems to be moving all files regardless of the date. For example, I was trying to move files prior to August 2020, and it copied files in August 2020. Any help would be greatly appreciated.

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
#Persistent
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#SingleInstance force

Gosub, CheckBatch
SetTimer, CheckBatch, 100

CheckBatch:
{
Folder = C:\PROCESSED ; SOURCE FOLDER
Folder1 = C:\PROCESSED\Previous month's history ; DESTINATION FOLDER
FILEDATE = 20200801000000
FILEDATETEST = FILEDATE

Loop, Files, %Folder%\*.*, F
    FILEDATETEST += A_LoopFileTimeModified
If FILEDATETEST < FILEDATE
	FileMove, %Folder%\*.*, %Folder1%\*.*
}
return
Thanks!
Chris70
User avatar
Chris70
Posts: 46
Joined: 17 Dec 2019, 12:25

Re: Move files based on file date

06 Aug 2020, 14:52

OK, so I think I got a better script after doing some more research, I realize I couldn't just compare the time values, I have to use the EnvSub function to do math first, then compare the results. So, here my current version of the script:

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
#Persistent
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#SingleInstance force

Folder = C:\Test Folder ; SOURCE FOLDER
Folder1 = C:\Test Destination ; DESTINATION FOLDER
FILEDATE = 20200801000000 ;TARGET DATE IN TIME FORMAT

Loop, Files, %Folder%\*.*
{

move_it := false
    {
    FileGetTime, time, %A_LoopFileName%
    EnvSub, time, %FILEDATE%, seconds  ; Subtract the source file's time from the TARGET DATE.
    if (time > 0)  ; Source file is older than destination file.
      move_it := true
    }
    if move_it
    {
        FileMove, %A_LoopFileFullPath%, %Foler1%\%A_LoopFileName%, 1   ; Copy with overwrite=yes
        if ErrorLevel
            MsgBox, Could not move "%A_LoopFileFullPath%" to "%Folder1%\%A_LoopFileName%". 
    }
}
ExitApp
Return
However, when I run it, the error message keeps popping up saying it could not move the files. I created a test batch of files and it finds all the correct files to move, but fails to move them. Am I using the FileMove function incorrectly here? Can anyone tell me what is going wrong?

Thanks!

Chris70
User avatar
Chunjee
Posts: 1421
Joined: 18 Apr 2014, 19:05
Contact:

Re: Move files based on file date

06 Aug 2020, 15:14

I think I see a typo %Foler1%\%A_LoopFileName%

Windows also likes to error on moving files if you have it open or another application is accessing it. But I'm pretty sure it is the typo in this case.


Chris70
User avatar
Chris70
Posts: 46
Joined: 17 Dec 2019, 12:25

Re: Move files based on file date

06 Aug 2020, 15:21

Thanks for catching the typo! That was definitely the issue, I think. However, I also determined that the GetFileTime function was not grabbing the modified date value like I wanted, so I changed the script to the following and it works!

Code: Select all

Loop, Files, %Folder%\*.*
{

move_it := false
    {
    time = %A_LoopFileTimeModified%
    EnvSub, time, %FILEDATE%, seconds  ; Subtract the source file's time from the TARGET DATE.
    if (time < 0)  ; Source file is newer than destination file.
      move_it := true
            
    }
    if move_it
    {
        FileMove, %folder%\%A_LoopFileName%, %folder1%\%A_LoopFileName%, 1    
        if ErrorLevel
            MsgBox, Could not move "%A_LoopFileFullPath%" to "%Folder1%\%A_LoopFileName%".
    }
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: ntepa and 222 guests