PlanetSquires Forums

Support Forums => General Board => Topic started by: Martin Francom on November 28, 2011, 12:44:16 AM

Title: XCOPY API
Post by: Martin Francom on November 28, 2011, 12:44:16 AM
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?
Title: Re: XCOPY API
Post by: José Roca on November 28, 2011, 01:54:17 AM
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.
Title: Re: XCOPY API
Post by: Martin Francom on November 28, 2011, 05:27:30 AM
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  ?
Title: Re: XCOPY API
Post by: 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
Title: Re: XCOPY API
Post by: Martin Francom on November 28, 2011, 06:40:05 AM
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? 
Title: Re: XCOPY API
Post by: Martin Francom on November 28, 2011, 06:42:10 AM
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.
Title: Re: XCOPY API
Post by: José Roca on November 28, 2011, 11:00:17 AM
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.
Title: Re: XCOPY API
Post by: Martin Francom on November 28, 2011, 09:42:07 PM
There must be a way to compare the file date of two files to determine which is newer.
Title: Re: XCOPY API
Post by: José Roca on November 28, 2011, 10:45:06 PM
Indeed. What there is not, as far as I know, is a function or method that does everything you want.
Title: Re: XCOPY API
Post by: Martin Francom on November 29, 2011, 02:51:38 AM
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?

Title: Re: XCOPY API
Post by: Michael Stefanik on November 29, 2011, 04:00:29 AM
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 (http://msdn.microsoft.com/en-us/library/windows/desktop/aa363858.aspx) to open the files in question, GetFileTime (http://msdn.microsoft.com/en-us/library/windows/desktop/ms724320.aspx) to get their last modification time, and CompareFileTime (http://msdn.microsoft.com/en-us/library/windows/desktop/ms724214.aspx) to compare the two values. Close the file handles when you're done.
Title: Re: XCOPY API
Post by: Martin Francom on November 29, 2011, 12:58:42 PM
Thanks Mike,   I will give that a try.
Title: Re: XCOPY API
Post by: Douglas McDonald on November 30, 2011, 02:10:49 PM
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
Title: Re: XCOPY API
Post by: James Padgett on November 30, 2011, 07:41:48 PM
I think there is also  robocopy   a fairly powerful copy program...