Jump to content

Copy all files of a folder without overwriting existing ones


Go to solution Solved by Earthshine,

Recommended Posts

8 minutes ago, Earthshine said:

the line of code


    $sFilePath = _WinAPI_GetTempFileName(@TempDir & "\MySourceDir\")

you DO NOT NEED!!! it creates a temp file and was used in the example just to create a file to copy. DO NOT USE THAT FUNCTION and you will not get .TMP files in your dest directory

Why do you want me to copy only certain file types? Why do you want me not to copy TMP files?

Link to comment
Share on other sites

#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>

Example()

Func Example()
    Local $sFilePath

    ; Creates the source folder.


    ; Should copy all files from the source folder to the destination folder without overwriting existing files.
    FileCopy(@TempDir & "\MySourceDir\*.*", @TempDir & "\MyDestDir\", $FC_NOOVERWRITE + $FC_CREATEPATH)

    ; Display the temporary directory.
    ShellExecute(@TempDir)
EndFunc   ;==>Example

use this code. prepopulate MySourceDir before your run. then run it once. Then add a few files and run again. Ok, I am done here.

you can copy tmp files all you want, but in the real world, you would be dealing with real files I assume. Test with REAL files to ensure they are not being overwritten

Edited by Earthshine

My resources are limited. You must ask the right questions

 

Link to comment
Share on other sites

10 minutes ago, Earthshine said:

Ok, I modified the sample code again. The 

#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>

Example()

Func Example()
    Local $sFilePath

    ; Creates the source folder.


    ; Should copy all files from the source folder to the destination folder without overwriting existing files.
    FileCopy(@TempDir & "\MySourceDir\*.*", @TempDir & "\MyDestDir\", $FC_NOOVERWRITE + $FC_CREATEPATH)

    ; Display the temporary directory.
    ShellExecute(@TempDir)
EndFunc   ;==>Example

 

I tested it again, it does NOT work! If there are 3 files in the destination folder that are also in the source folder, nothing is copied.

Can someone else please test this to see if Earthshine and I are talking past each other?

Link to comment
Share on other sites

Please excuse me if we have talked past each other. I have now done it from scratch again.

- Deleted all files in MySourceDir and MyDestDir.
- Manually created 2 new files in MySourceDir: 1.au3 and 2.au3.
- Executed your code.

Spoiler
#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>

Example()

Func Example()
    Local $sFilePath

    ; Creates the source folder.


    ; Should copy all files from the source folder to the destination folder without overwriting existing files.
    FileCopy(@TempDir & "\MySourceDir\*.*", @TempDir & "\MyDestDir\", $FC_NOOVERWRITE + $FC_CREATEPATH)

    ; Display the temporary directory.
    ShellExecute(@TempDir)
EndFunc   ;==>Example

- In MyDestDir are now 1.au3 and 2.au3.
- Manually created 1 more file in MySourceDir: 3.au3.
- Executed your code.
- In MyDestDir are still only 1.au3 and 2.au3. The 3.au3 was not copied.

 

Link to comment
Share on other sites

I guess that the TEMP files are the reason for all the confusion.

It is as @Earthshine already wrote : The TEMP file in the help example will only be created so that anything exists to copy. Usually, there is already a 'normal' file (or a source folder with files) that should be copied to a destination folder.

OT : I'm AFK ("Away from Keyboard") now, as a bottle of wine is waiting for me (or maybe two) :).

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

15 minutes ago, Professor_Bernd said:

Unfortunately this is too much for "/AutoIt3ExecuteLine" (called in VBScript). But thanks for the tip!

Maybe you can find an approach here :think::

https://www.autoitscript.com/forum/topic/183637-autoit3executeline-question/

https://www.autoitscript.com/forum/topic/181491-question-about-autoit3executeline/?do=findComment&comment=1303214

 

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

In case it is the tmp folder, I have now tested it with 2 folders that have full permissions and are not in the tmp folder.

- Folders created manually: "E:\MySourceDir" and "E:\MyDestDir".
- Manually created 2 new files in MySourceDir: 1.au3 and 2.au3.
- FileCopy executed as in this code.

#include <FileConstants.au3>

; Should copy all files from the source folder to the destination folder without overwriting existing files.
FileCopy("E:\MySourceDir\*.*", "E:\MyDestDir\", $FC_NOOVERWRITE)

- In MyDestDir are now 1.au3 and 2.au3.
- Manually created 1 more file in MySourceDir: 3.au3.
- FileCopy executed again as in the code above.
- In MyDestDir are still only 1.au3 and 2.au3. The 3.au3 was not copied.

My operating system is Window 10 Pro 64 bit. AutoIt version 3.3.14.5

Edited by Professor_Bernd
Link to comment
Share on other sites

ok i repeated your test, i did the same steps. I see it now. in Temp everything was working fine but when I use your code and add 3 .txt files they don't get created on the target dir after re-running the script. Maybe open a ticket for support.

For now use @Musashi's code and check out those links he provided.

My resources are limited. You must ask the right questions

 

Link to comment
Share on other sites

I did notice one thing, in the helpfile stating that the file attributes may stop FileCopy from working. I removed my three txt files that had been previously created by another app, and created a new .au3 file in source and it DID copy. Very strange but file attribs might have something to do with it.

 

@Professor_Bernd:

BINGO! The help file states that the if the file's archive attribute exists, then FileCopy WILL NOT WORK. My three text files that were created had their Archive attribute set so they refused to copy. I believe that is what you are seeing. There is a workaround, read FileCopy Help and follow the links to the File Attribute function.

 

I would still open a support ticket for this squirrely behavior though. It's not easy to figure out what is really happening because I can't read the code behind FileCopy. I don't see how the archive attribute could affect it, nor should it affect it in any way.

Edited by Earthshine

My resources are limited. You must ask the right questions

 

Link to comment
Share on other sites

  • Solution

I would use a call to Robocopy. read this link

https://stackoverflow.com/questions/5152835/having-xcopy-copy-a-file-and-not-overwrite-the-previous-one-if-it-exists-withou

use the following syntax when calling it from AutoIt

robocopy x:\sourcefolder Y:\destfolder /s /e /r:0 /z

 

that should solve your issue

My resources are limited. You must ask the right questions

 

Link to comment
Share on other sites

@Professor_Bernd

Use this code:

#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>

$result = 0



Example()

Func Example()
    Local $sFilePath

    ; Creates the source folder.

    ; Should copy all files from the source folder to the destination folder without overwriting existing files.
;~     FileCopy("d:\xSlog\*.*", "d:\MyDestDir\*.*", $FC_NOOVERWRITE + $FC_CREATEPATH)
    Run("robocopy d:\xSlog d:\MyDestDir\ /r:0 /z", @ScriptDir)
    ; Display the temporary directory.

EndFunc   ;==>Example

That does what you want exactly. If you ALSO want to pick up subdirectories and their files, do this:

#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>

$result = 0



Example()

Func Example()
    Local $sFilePath

    ; Creates the source folder.

    ; Should copy all files from the source folder to the destination folder without overwriting existing files.
;~     FileCopy("d:\xSlog\*.*", "d:\MyDestDir\*.*", $FC_NOOVERWRITE + $FC_CREATEPATH)
    Run("robocopy d:\xSlog d:\MyDestDir\ /s /e /r:0 /z", @ScriptDir)
    ; Display the temporary directory.

EndFunc   ;==>Example

Just make sure to substitute YOUR input and output directories in the code. I used mine as a test because it had lots of files and subfolders.

Edited by Earthshine

My resources are limited. You must ask the right questions

 

Link to comment
Share on other sites

1 hour ago, Earthshine said:

I don't see how the archive attribute could affect it, nor should it affect it in any way.

I agree ! Fiddling around with the Archive attribute didn't work in my tests. Therefore I also consider this approach to be unreliable.

1 hour ago, Earthshine said:

I would still open a support ticket for this squirrely behavior though.

I suspect that would have little chance of success, see :

https://www.autoitscript.com/forum/topic/205898-copy-all-files-of-a-folder-without-overwriting-existing-ones/?do=findComment&comment=1482372

48 minutes ago, Earthshine said:

I would use a call to Robocopy ...

This is probably the best solution 👍. In addition, the call can be triggered with a one-liner, which seems to be crucial for @Professor_Bernd ;).

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

@Earthshine

I really appreciate you doing the test again! 👍 So we got some new knowledge and even a possible solution with Robocopy that I can use with "/AutoIt3ExecuteLine". Thanks a lot! :)

11 hours ago, Musashi said:

Fiddling around with the Archive attribute didn't work in my tests. Therefore I also consider this approach to be unreliable.

I also tested the effect of the archive attribute a bit and think it's too shaky to work reliably. So now I will take a closer look at Robocopy, which looks promising.

Link to comment
Share on other sites

The idea with Robocopy worked. At first it took a while to find the right switches for my purposes. But what really took a long time was to find a way to pass the call from VBScript to AutoIt (/AutoIt3ExecuteLine). The difficulty was the double quotes around paths and that VBScript uses the same escape character as AutoIt: doubling the double quotes.

But now it's done and it works. Thank you! @Earthshine, @Musashi

Link to comment
Share on other sites

On 5/25/2021 at 12:13 PM, Professor_Bernd said:

At first it took a while to find the right switches for my purposes. But what really took a long time was to find a way to pass the call from VBScript to AutoIt (/AutoIt3ExecuteLine).

So why not just have VBS call RoboCopy directly?

Code hard, but don’t hard code...

Link to comment
Share on other sites

16 hours ago, JockoDundee said:

So why not just have VBS call RoboCopy directly?

Yes, in hindsight it probably would have been easier. Maybe I'll change it too.

Initially, I was going to use AutoIt to move the copying to its own thread so the VBScript could continue without waiting. With Robocopy, it also has its own thread. I had thought of that briefly too, but decided to go with the AutoIt solution because I wasn't sure if the CMD window flashing could be prevented.

In VBScript you can set a parameter for Run, so that the CMD window is not shown and under Windows 10 it is then mostly not to be seen. But in my experience it can still happen that under Windows 7 the flashing of the CMD window can be seen.

Then I got so bogged down with the problem of parameters and double quotes that I didn't think about it anymore.

But I guess you didn't want to know all that and just wanted to give me the tip. ;) If I get the chance to do tests on Windows 7 and the CMD window does not flash there, the call of robocopy can be called directly in VBScript.

Thanks for the good idea! :)

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...