Jump to content
Sign in to follow this  


Recommended Posts


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

$source = "C:\Documents and Settings\&eplesh\Desktop\leet.jpg"
$target = "C:\test.jpg"
$opt = 0
$testing = _FileCopyNewer($source, $target, $opt)
$error = @error
MsgBox(4096, 'debug:', '$testing:' & $testing);### Debug MSGBOX 
MsgBox(4096, 'debug:', '$error:' & $error);### Debug MSGBOX 

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

;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
         Return 0
         Return 1
      Return 1
EndFunc  ;==>_FileCopyNewer

Edited by emmanuel

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

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  


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.