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?
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.
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 ?
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 destinationThis will overwrite files with newer versions only without asking.
Rolf
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?
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.
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.
There must be a way to compare the file date of two files to determine which is newer.
Indeed. What there is not, as far as I know, is a function or method that does everything you want.
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?
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.
Thanks Mike, I will give that a try.
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
I think there is also robocopy a fairly powerful copy program...