Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copy a file from one folder to another that does not yet exist 2

Status
Not open for further replies.

PatMcLaughlin

Programmer
Dec 10, 2010
97
US
I am trying to use the "Copy File" command to copy a file from one location (f:\Orig\myfile) to another that does not yet exist. (f:\Dest\folder2\folder3\myfile). In my example, the destination exists to f:\Dest but the two folders may have to be created. (Hopefully by the program on the fly). Can you tell me how this is done?
 
Use Directory() to test whether it exists, and MKDIR to create it if it doesn't.
 
to expand on the danfreemans post - you can create a function you may name it CopyMyFile, where you can pass fully qualified filenames for source and destination. then extract the directory name by using juststem() and check it with directory() function. If all conditions met issue 'copy file' command. return true at success. this will create the destination directory on the fly for any 'copymyfile' command. syntax will look like CopyMyFile("f:\Orig\myfile","f:\Dest\folder2\folder3\myfile")

nasib
 
Nasib is right, except JustStem("f:\Dest\folder2\folder3\myfile.dbf") will give you "myfile", JustPath("f:\Dest\folder2\folder3\myfile.dbf") will give you f:\Dest\folder2\folder3 and JustFName("f:\Dest\folder2\folder3\myfile.dbf") will give you myfile.dbf

So the function would be like this - easy once you know the ingredients:
Code:
Function CopyMyFile(tcSourceFile, tcDestinationFile)

LOCAL llSuccess, llDirexists
* llSuccess is inited as .F. by the LOCAL declaration

If File(tcSourceFile)
   llDirexists = Directory(JustPath(tcDestinationFile))
   If Not llDirexists
      Try
         MkDir (JustPath(tcDestinationFile))
         llDirexists = .T.
      Catch
         * If MkDir causes an error (due to insufficient rights to create a dir, for example), this will catch the error, llDirexists = .T. then isn't executed
      Endtry
   Endif
   If llDirexists
      Try
         Copy File (tcSourceFile) TO (tcDestinationFile)
         llSuccess = .T.
      Catch
         * again, if an error is triggered, this will catch it, llSuccess is still .F. in this case
      Endtry 
   Endif
Endif
Return llSuccess

It's not ideal, though, if you want to copy a whole source dir of files to a new destination directory. Because in short, you can do a bulk copy, once the destination directory is created:
COPY FILE d:\some\source\*.* TO d:\some\destination\*.*

So you could also do:
Code:
Function CopyMyDir(tcSourceDir, tcDestinationDir)

Local llSuccess, llDirexists

If Directory(tcSourceDir)
   llDirexists = Directory(tcDestinationDir)
   If Not llDirexists
      Try
         MkDir (tcDestinationDir)
         llDirexists = .T.
      Catch
         * If MkDir causes an error (due to insufficient rights to create a dir, for example), this will catch the error, llDirexists = .T. then isn't executed
      Endtry
   Endif
   If llDirexists
      Try
         Copy File (AddBS(tcSourceDir)+"*.*") TO (AddBS(tcDestinationDir)+"*.*")
         llSuccess = .T.
      Catch
         * again, if an error is triggered, this will catch it, llSuccess is still .F. in this case
      Endtry 
   Endif
Endif
Return llSuccess

If destination file or directory exists and files are overwritten, it depends on SET SAFETY, if you get safety questions about overwriting files.

Bye, Olaf.
 
For the way it is used in the program, your first code (Olaf) was exactly what I needed. We use it in a program that runs approximately 45,000 processes a day (Runs on 4 servers) doing many different tasks. This portion of the code may have only one file or it may be run 60 or 70 times in a row for multiple files each going to different locations depending on how it is needed. We had been using a script that shelled to the DOS and used XCopy. This worked but took 40 - 45 seconds to complete each for the time the process started to ended. Not a problem when single files are ran however as you can imagine it becomes a roadblock when the multiples appear. Your quick help was greatly appreciated as always!
 
I understand your pain. I know how slow it can be to start a cmd.com or some bat or cmd file with xcopy or any other shell command.
You may also try robocopy, it's a standalone exe. And it has several advantages over how VFPs COPY FILE command works, eg it can retry copies failing in a first attempt.

Bye, Olaf.
 
I looked at robocopy. It appears to do what you say but is not available until Server2008/Windows7 and after. Since we are still using Server2003 and 200-300 XP machines, it would not be an option. I will store that info in my weak brain for further reference later. Until then, your answer above has helped greatly. Thanks again
 
Well, you can still get it from an SDK and use it on XP/2003 Server, too. I don't remember exactly in what SDK/Kit, but you'll find out, it's a freebie anyway.

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top