AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Copy files by modification date

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
ScriptStudent



Joined: 08 Jun 2005
Posts: 5

PostPosted: Wed Jun 08, 2005 11:22 pm    Post subject: Copy files by modification date Reply with quote

Hi all,

Im new to AutoHotKey, Im working on a script to copy files by modification date. This is a straight copy with no source files to compare. Im trying to recurse through C:\ drive and copy all documents older than 10 days. Im running into error messages like the one below :

---------------------------
AHKeyCpyByDate.exe
---------------------------
Error: This date-time string contains at least one invalid component.

Specifically: 10

Line#
---> 008: var1 -= 10

The program will exit.


here is the current script any input would greatly appreciated:


; Copy only files based on the modification date

CopyIfNewer:
FileCreateDir, C:\AHkeyTest
SetWorkingDir, C:\

var1 = ; Make it blank so that the below will use the current time instead.
var1 -= 10, days
Loop, %A_WorkingDir%\*.doc, , 1 ; Recurse into subfolders.

{
FileGetTime, time, %A_WorkingDir%\%A_LoopFileName%
EnvSub, %var1%, %A_LoopFileTimeModified%, days ; Subtract the source file's time in days from the destination's.
if time <= 0 ; Source file is newer than destination file.
copy_it = y

}
if copy_it = y
{
FileCopy, %A_LoopFileFullPath%, C:\AHkeyTest\%A_LoopFileName%, 1 ; Copy with overwrite=yes
if ErrorLevel <> 0
MsgBox, Could not copy "%A_LoopFileFullPath%" to "C:\AHkeyTest\%A_LoopFileName%".
}
Back to top
View user's profile Send private message
Serenity



Joined: 08 Nov 2004
Posts: 982

PostPosted: Wed Jun 08, 2005 11:39 pm    Post subject: Reply with quote

Please use the code tags when posting scripts, it makes it easier for others to read. Smile
_________________
"Anything worth doing is worth doing slowly." - Mae West
Back to top
View user's profile Send private message Visit poster's website
ScriptStudent



Joined: 08 Jun 2005
Posts: 5

PostPosted: Wed Jun 08, 2005 11:52 pm    Post subject: Thank so much... Reply with quote

I was not aware of the different tags until your reply... Embarassed
Back to top
View user's profile Send private message
Chris
Site Admin


Joined: 02 Mar 2004
Posts: 10467

PostPosted: Thu Jun 09, 2005 12:01 am    Post subject: Re: Copy files by modification date Reply with quote

ScriptStudent wrote:
Error: This date-time string contains at least one invalid component.
---> 008: var1 -= 10
This is caused by the fact that subtraction compares two dates. To instead adjust one date by a certain number of days, add a negative value: var1 += -10, days
Back to top
View user's profile Send private message Send e-mail
ScriptStudent



Joined: 08 Jun 2005
Posts: 5

PostPosted: Thu Jun 09, 2005 8:05 am    Post subject: thanks Guys, Im very close... Reply with quote

I got part of the code working it seems to be copying some of the files correctly however some files are older than the modified date (which should be all files that are ten days old ) this is the current script:


Code:

; Copy only files based on the modification date

CopyIfNewer:
FileRemoveDir, C:\AHkeyTest, 1
FileCreateDir, C:\AHkeyTest

SetWorkingDir, C:\*.*

var1 = ; Make it blank so that the below will use the current time instead.
var1 += -10, days


Loop, %WorkingDir%\*.doc",0 , 1  ; Recurse into subfolders.
{
FileGetTime, time, %A_LoopFileName%
EnvAdd, var1, %A_LoopFileName%, days ; Subtract the source file's time in days from the destination's.
if time < 0 ; Source file is newer than destination file.
copy_it = y

}
if copy_it = y
{
FileCopy, %WorkingDir%\%A_LoopFileName%, C:\AHkeyTest\%A_LoopFileName%, 1 ; Copy with overwrite=yes
if ErrorLevel <> 0
MsgBox, Could not copy "%A_LoopFileFullPath%" to "C:\AHkeyTest\%A_LoopFileName%".
}

Back to top
View user's profile Send private message
guivho



Joined: 04 Jun 2005
Posts: 26
Location: Lokeren, Belgium

PostPosted: Thu Jun 09, 2005 8:14 pm    Post subject: Re: thanks Guys, Im very close... Reply with quote

Quote:
Code:

Loop, %WorkingDir%\*.doc",0 , 1  ; Recurse into subfolders.

Drop the double quote
Quote:
Code:

{
FileGetTime, time, %A_LoopFileName%
EnvAdd, var1, %A_LoopFileName%, days ; Subtract the source file's time in days from the destination's.
if time < 0 ; Source file is newer than destination file.

Replace the two last stements by following line
Code:

if time < %var1%

HTH,
guivho
Back to top
View user's profile Send private message
ScriptStudent



Joined: 08 Jun 2005
Posts: 5

PostPosted: Fri Jun 10, 2005 11:39 pm    Post subject: Hi, guivho Reply with quote

Thanks for the reply,

The code is still copying files with older Modification dates than ten days (one file was last modified in 1991). Im not sure what its seeing thats validating the files. here is the current script (its basically the same script with the modifications).

Code:
; Copy only files based on the modification date

CopyIfNewer:

FileRemoveDir, C:\AHkeyTest, 1
FileCreateDir, C:\AHkeyTest

SetWorkingDir, C:

var1 = ; Make it blank so that the below will use the current time instead.
var1 += -10, days


Loop, %WorkingDir%\*.*, 0 , 1  ; Recurse into subfolders.
{
FileGetTime, time, %A_LoopFileName%, M
if time < %var1%
copy_it = y

}
if copy_it = y
{
FileCopy, %WorkingDir%\%A_LoopFileName%, C:\AHkeyTest\%A_LoopFileName%, 1 ; Copy with overwrite=yes
if ErrorLevel <> 0
MsgBox, Could not copy "%A_LoopFileFullPath%" to "C:\AHkeyTest\%A_LoopFileName%".
}

Back to top
View user's profile Send private message
Decarlo110



Joined: 15 Dec 2004
Posts: 303
Location: United States

PostPosted: Sat Jun 11, 2005 12:23 am    Post subject: Reply with quote

Code:

; -----------------place all initialization settings at the top
SetWorkingDir, C:
var1 = ; Make it blank so that the below will use the current time instead.
var1 += -10, days

; Copy only files based on the modification date

CopyIfNewer:

;-------the above routine label isnt referenced elsewhere.
;-------Since there is no "return" line above,
;-------the entire script auto-executes

FileRemoveDir, C:\AHkeyTest, 1
FileCreateDir, C:\AHkeyTest


Loop, %WorkingDir%\*.*, 0 , 1  ; Recurse into subfolders.
{
    FileGetTime, time, %A_LoopFileName%, M
    ;------------the following line is a hint
    MsgBox %time%
    ;------------(study the String manipulation commands)
    if time < %var1%
        copy_it = y
}
if copy_it = y
{
    FileCopy, %WorkingDir%\%A_LoopFileName%, C:\AHkeyTest\%A_LoopFileName%, 1 ; Copy with overwrite=yes
    if ErrorLevel <> 0
        MsgBox, Could not copy "%A_LoopFileFullPath%" to "C:\AHkeyTest\%A_LoopFileName%".
}

_________________
1) The Open Source Definition http://www.opensource.org/docs/definition_plain.php

2) Intuitive. Logical. Versatile. Adaptable. <<AutoHotkey>>
Back to top
View user's profile Send private message
ScriptStudent



Joined: 08 Jun 2005
Posts: 5

PostPosted: Sat Jun 11, 2005 1:39 am    Post subject: Decarlo110... Reply with quote

Im new to open source code can you give me the abrdiged version of what your trying to say...??
Back to top
View user's profile Send private message
guivho



Joined: 04 Jun 2005
Posts: 26
Location: Lokeren, Belgium

PostPosted: Sat Jun 11, 2005 10:06 am    Post subject: Reply with quote

ScriptStudent,

Your working dir, the root of your loop, is set at c:\
Your target dir is set at C:\AHKeyTest
So within your loop, you will read your target dir and check whether its content needs to be copied to itself. Luckily, the test should always turn negative, but this does not seem a clean approach.

Your loop recurses over your file system, and changes the value of the %copy_it% variable from the moment a positive test is detected. The loop happily continues, without ever resetting the value of %copy_it%, nor copying a file.

After your loop, you check whether there was any file in the filesystem that needed to be copied, and than you copy the last seen file.

You might want to try this one:

Code:
; Copy only files based on the modification date

SetWorkingDir, C:
var1 += -10, days
Target = c:\AHKeytest

FileRemoveDir, %target%, 1
FileCreateDir, %target%

Loop, %A_WorkingDir%\*.*, 0 , 1  ; Recurse into subfolders.
{
  FileGetTime, time, %A_LoopFileLongPath%, M
  if time < %var1%
  {
    StringMid, newfile, A_LoopFileLongPath, 3, 999 ;drop drive part, keep \
    FileCopy, %A_LoopFileLongPath%, %target%%newfile%, 1 ; Copy with overwrite=yes
    if ErrorLevel <> 0
      MsgBox, Could not copy "%A_LoopFileLongPath%" to "%target%%newfile%".
  }
}
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group