Sign in to follow this  
Followers 0
emmanuel

writing a little udf

6 posts in this topic

the goal is to kinda do like xcopy's /D option

only problem, can't decide the best way to compare dates. here's what I have and it works fine aside from the part where it compares the dates.

#include-once
;===============================================================================
;
; Description:      Copy file and overwrite only if target is newer
; Syntax:           __FileCopyNewer( $sSrc, $sTrgt, $iOpt )
; Parameter(s):     $sSrc - Path and filename to 
;                   $sTrgt - Path and filename to 
;                   $iOpt  - Time attribute to be used, same as FileGetTime
;                                0 = Modified
;                                1 = Created
;                                2 = Accessed
; Requirement(s):   None
; Return Value(s):  On Success - Returns 1
;                                @error = -1; if the target file is newer than the source
;                   On Failure - Returns 0 and sets:
;                                @error = 1: Error finding file or retreiving it's time
;                                @error = 2: File copy failed
; Author(s):        Emmanuel Pleshe <emmanuel.pleshe@gmail.com>
; Note(s):          I don't know what this will do with wildcards.
;
;===============================================================================

;~ #cs
;demo:
$source = "C:\Documents and Settings\Eman\Desktop\icons from iconhell.txt"
$target = "C:\test.txt"
$opt = 0
$testing = _FileCopyNewer($source, $target, $opt)
$error = @error
MsgBox(4096, 'debug:', '$testing:' & $testing);### Debug MSGBOX 
MsgBox(4096, 'debug:', '$error:' & $error);### Debug MSGBOX 
;~ #ce

Func _FileCopyNewer($sSrc, $sTrgt, $iOpt)
   $sSrcMod = FileGetTime($sSrc, $iOpt)
   $sTrgtMod = FileGetTime($sSrc, $iOpt)
   
   If $sSrcMod = 1 Then
      SetError(1)
      Return 0
   EndIf

;my first thought was that if I added all the elements of each array, the older file would have a smaller number
;yeah, I'm short on caffeen.
   if ($sSrcMod[0] + $sSrcMod[1] + $sSrcMod[2] + $sSrcMod[3] + $sSrcMod[4] + $sSrcMod[5]) > _
      ($sTrgtMod[0] + $sTrgtMod[1] + $sTrgtMod[2] + $sTrgtMod[3] + $sTrgtMod[4] + $sTrgtMod[5]) _
      Or FileExists($sTrgt) = 0 Then
      $iCopyTest = FileCopy($sSrc, $sTrgt, 1)
      
      If $iCopyTest = 0 Then
         SetError(2)
         Return 0
      Else
         Return 1
      EndIf
   Else
      SetError(-1)
      Return 1
   EndIf
   
EndFunc  ;==>_FileCopyNewer

"I'm not even supposed to be here today!" -Dante (Hicks)

Share this post


Link to post
Share on other sites



Hiya,

i dont know if this will work and if yes, how fast it will be, but you could try:

$newer = 0
For $I = 0 To 5
  If $sSrcMod[$I]>$sTrgtMod[$I] Then
    $newer = 1
    ExitLoop
  EndIf
  If $sSrcMod[$I]<$sTrgtMod[$I] Then ExitLoop
Next

If $newer is 1 then source is newer, if $newer is 0 then it is same date or older as target.

Greetings,

ZeD

Share this post


Link to post
Share on other sites

the goal is to kinda do like xcopy's /D option

only problem, can't decide the best way to compare dates.  here's what I have and it works fine aside from the part where it compares the dates.

<{POST_SNAPBACK}>

Don't use + but concatenate it with & ... that should work:

if ($sSrcMod[0] & $sSrcMod[1] & $sSrcMod[2] & $sSrcMod[3] & $sSrcMod[4] & $sSrcMod[5]) > _
     ($sTrgtMod[0] & $sTrgtMod[1] & $sTrgtMod[2] & $sTrgtMod[3] & $sTrgtMod[4] & $sTrgtMod[5]) _
     Or FileExists($sTrgt) = 0 Then

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

@zedmin: thanks... saw JdeB's first...

Don't use + but concatenate it with & ... that should work:

if ($sSrcMod[0] & $sSrcMod[1] & $sSrcMod[2] & $sSrcMod[3] & $sSrcMod[4] & $sSrcMod[5]) > _
     ($sTrgtMod[0] & $sTrgtMod[1] & $sTrgtMod[2] & $sTrgtMod[3] & $sTrgtMod[4] & $sTrgtMod[5]) _
     Or FileExists($sTrgt) = 0 Then

<{POST_SNAPBACK}>

@JdeB: great, that works, at least to my limited testing... (one filecopy :ph34r:) I'll go post it in scripts and scraps... How does one submit something to be included in the includes?

"I'm not even supposed to be here today!" -Dante (Hicks)

Share this post


Link to post
Share on other sites

@zedmin: thanks...  saw JdeB's first...

@JdeB: great, that works, at least to my limited testing...  (one filecopy :() I'll go post it in scripts and scraps... How does one submit something to be included in the includes?

<{POST_SNAPBACK}>

Jeremy is compiling the UDF's or was at least till a few months ago.....

just check the helpfile :ph34r:


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Hiya,

i dont know if this will work and if yes, how fast it will be, but you could try:

$newer = 0
For $I = 0 To 5
  If $sSrcMod[$I]>$sTrgtMod[$I] Then
    $newer = 1
    ExitLoop
  EndIf
  If $sSrcMod[$I]<$sTrgtMod[$I] Then ExitLoop
Next

If $newer is 1 then source is newer, if $newer is 0 then it is same date or older as target.

Greetings,

ZeD

<{POST_SNAPBACK}>

ZeD,

this isn't going to work because.... take this as an example:

2004/09/17 15:00:00 target file

2004/08/17 17:00:00 source file

With your script the target hour value would be smaller thus qualify for copying...


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
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
Sign in to follow this  
Followers 0