XCOPY API

Started by Martin Francom, November 28, 2011, 12:44:16 AM

Previous topic - Next topic

Martin Francom

Is there an API for XCOPY ?    Or other file copy routines accomplished with Jose' API ?

What I want to be able to do from my program without using Shell
is to be able to Copy files using wildcards  PB's FileCopy does not allow *.*
  ie: FileCopy  \MyFiles\*.*    \MyBackup

Is there an API method to copy files using wildcards?

José Roca

Yes, using the CopyFolder or CopyFile methods of the FileSystemObject.

Read the following documentation:

CopyFolder method: http://msdn.microsoft.com/en-us/library/xbfwysex%28VS.85%29.aspx
CopyFile method: http://msdn.microsoft.com/en-us/library/e1wf9e7w%28VS.85%29.aspx

Example:


#COMPILE EXE
#DIM ALL
#INCLUDE ONCE "scrrun.inc"

FUNCTION PBMAIN () AS LONG

   LOCAL fso AS IFileSystem
   fso = NEWCOM "Scripting.FileSystemObject"
   IF ISOBJECT(fso) THEN fso.CopyFolder("c:\MyFiles\*", "c:\MyBackup\")
   ' -or-
   IF ISOBJECT(fso) THEN fso.CopyFile("c:\MyFiles\*.*", "c:\MyBackup\")

END FUNCTION


Wildcard characters can only be used in the last path component of the source argument.

You can check if there has been an error with OBJRESULT.

Martin Francom

#2
Thanks Jose'

I want to make PB functions that will return "Success" or "Failure"


Here's what I got. I must be doing some wrong.  ObjResult is = to -2146828230
Not sure what that means but the files are not being copied.


#COMPILE EXE
#DIM ALL
#INCLUDE ONCE "scrrun.inc"

' For more information check
' http://msdn.microsoft.com/en-us/library/e1wf9e7w(v=VS.85).aspx

FUNCTION PBMAIN () AS LONG

   LOCAL objF  AS STRING
   LOCAL destF AS STRING
   result AS LONG

   objF = "C:\Myfiles\*.bas"
   destF = "C:\MyBackup\"

   result = xCopyFile (objF, destF)

   MSGBOX "Done " & STR$(result)

END FUNCTION

FUNCTION xCopyFile (objFile AS STRING, destFile AS STRING) AS LONG
     LOCAL fso AS IFileSystem
     fso = NEWCOM "Scripting.FileSystemObject"
     IF ISOBJECT(fso) THEN fso.CopyFile(objFile, destFile)
     FUNCTION = OBJRESULT 
END FUNCTION

FUNCTION xCopyFolder (objFolder AS STRING, destFolder AS STRING) AS LONG
     LOCAL fso AS IFileSystem
     fso = NEWCOM "Scripting.FileSystemObject"
     IF ISOBJECT(fso) THEN fso.CopyFolder(objFolder, destFolder)
     FUNCTION = OBJRESULT 
END FUNCTION

FUNCTION xMoveFile (objFile AS STRING, destFile AS STRING) AS LONG
     LOCAL fso AS IFileSystem
     fso = NEWCOM "Scripting.FileSystemObject"
     IF ISOBJECT(fso) THEN fso.MoveFile(objFile, destFile)
    FUNCTION = OBJRESULT 
END FUNCTION
               


as with xCopy  is there a way to add an argument the to copy ONLY IF NEWER  ?

Rolf Brandt

#3
Quoteas with xCopy  is there a way to add an argument the to copy ONLY IF NEWER  ?

Yes, there is. In a DOS box (Command) put this command:
help xcopy and press ENTER
It will show you all parameters.

To copy only files that are newer use

xcopy /d /y source destination

This will overwrite files with newer versions only without asking.

Rolf
Rolf Brandt
http://www.rbsoft.eu
http://www.taxifreeware.com
I cook with wine, sometimes I even add it to the food.
(W. C. Fields)

Martin Francom

Ok figured out problem.  If file exists then it will NOT overwrite file and it give the error.
There's a overwrite argument I will add it.

Does the overwrite argument support  Overwrite if newer? 

Martin Francom

Quote from: Rolf Brandt on November 28, 2011, 06:23:25 AM
Quoteas with xCopy  is there a way to add an argument the to copy ONLY IF NEWER  ?

Yes, there is. In a DOS box (Command) put this command:
help xcopy and press ENTER
It will show you all parameters.

To copy only files that are newer use

xcopy /d /y source destination

This will overwrite files with newer versions only without asking.

Rolf


Thanks Rolf but I don't want to be shelling to xCopy  I would prefer to use the API function directly if possible.

José Roca

Quote
Ok figured out problem.  If file exists then it will NOT overwrite file and it give the error.
There's a overwrite argument I will add it.

Be aware that it only accepts 0 (false) or -1 (visual basic true). You should also change the AS STRING to AS WSTRING.

Quote
Does the overwrite argument support  Overwrite if newer? 

No.

Martin Francom

There must be a way to compare the file date of two files to determine which is newer.

José Roca

Indeed. What there is not, as far as I know, is a function or method that does everything you want.

Martin Francom

Jose'   yes I understand THESE functions can't compare files dates
But there should be a way to write a diferent function that can compare
two files and determine which one is older.

So if you wanted to be able to programmatically compare two files and
determine which is older.   How would you do it.?
Are there any PB functions that can do that?
Or, maybe there is some WinAPI function that can determine which is the
older of two files?


Michael Stefanik

Quote from: Marty Francom on November 29, 2011, 02:51:38 AM
So if you wanted to be able to programmatically compare two files and
determine which is older.   How would you do it.?

Using the Win32 API, you would use CreateFile to open the files in question, GetFileTime to get their last modification time, and CompareFileTime to compare the two values. Close the file handles when you're done.
Mike Stefanik
sockettools.com

Martin Francom

Thanks Mike,   I will give that a try.

Douglas McDonald

Marty,

Have you tried microsoft's RICHCOPY. As I understand it is a replacement for xcopy. I don't know if there is an API but you may want to take a look at it.

Just a thought
Doug
Doug McDonald
KD5NWK
www.redforksoftware.com
Is that 1's and 0's or 0's and 1's?

James Padgett

I think there is also  robocopy   a fairly powerful copy program...