AutoHotkey Community

It is currently May 26th, 2012, 2:24 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 10 posts ] 
Author Message
PostPosted: June 8th, 2005, 11:22 pm 
Offline

Joined: June 8th, 2005, 10:38 pm
Posts: 5
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%".
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 8th, 2005, 11:39 pm 
Offline

Joined: November 8th, 2004, 12:46 am
Posts: 1271
Please use the code tags when posting scripts, it makes it easier for others to read. :)

_________________
"Anything worth doing is worth doing slowly." - Mae West
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Thank so much...
PostPosted: June 8th, 2005, 11:52 pm 
Offline

Joined: June 8th, 2005, 10:38 pm
Posts: 5
I was not aware of the different tags until your reply... :oops:


Report this post
Top
 Profile  
Reply with quote  
PostPosted: June 9th, 2005, 12:01 am 
Offline

Joined: March 2nd, 2004, 3:36 pm
Posts: 10720
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


Report this post
Top
 Profile  
Reply with quote  
PostPosted: June 9th, 2005, 8:05 am 
Offline

Joined: June 8th, 2005, 10:38 pm
Posts: 5
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%".
}



Report this post
Top
 Profile  
Reply with quote  
PostPosted: June 9th, 2005, 8:14 pm 
Offline

Joined: June 4th, 2005, 11:56 am
Posts: 26
Location: Lokeren, Belgium
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Hi, guivho
PostPosted: June 10th, 2005, 11:39 pm 
Offline

Joined: June 8th, 2005, 10:38 pm
Posts: 5
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%".
}



Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 11th, 2005, 12:23 am 
Offline

Joined: December 15th, 2004, 10:24 pm
Posts: 303
Location: United States
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>>


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Decarlo110...
PostPosted: June 11th, 2005, 1:39 am 
Offline

Joined: June 8th, 2005, 10:38 pm
Posts: 5
Im new to open source code can you give me the abrdiged version of what your trying to say...??


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 11th, 2005, 10:06 am 
Offline

Joined: June 4th, 2005, 11:56 am
Posts: 26
Location: Lokeren, Belgium
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%".
  }
}


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bing [Bot], G. Sperotto, poserpro, Yahoo [Bot] and 15 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group