Sign in to follow this  
Followers 0
goodbyeplanet

How do i copy 2 files at once

16 posts in this topic

I know how to use FileCopy to copy a single file from the current directory to another directory but how do i do it when i want to copy two files at once using a single line. for example how can i come up with a single line of code instead of 2 statements like below:

FileCopy("C:\old\file1.exe", "C:\new\")

FileCopy("C:\old\file2.exe", "C:\new\")

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Why do you want to do it in a single line? Any problems with two lines?

Maybe you can use wildcards.

FileCopy("C:\old\file*.exe", "C:\new\")
Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Why do you want to do it in a single line? Any problems with two lines?

Maybe you can use wildcards.

Edited by goodbyeplanet

Share this post


Link to post
Share on other sites

well hv been just trying to make things as simple as possible since it seems i will be copying and moving lots of files. so i thought if there was a way that would at least keep the code clean and not come up with a myriad of lines....

unfortunately them wiildcards aint working too

How do you get the filenames into your script?

You could fill an array (read the list of files from an ini file, scan through the directory using FileFindFirstFile/FileFindNextFile etc.) and then copy them in a loop.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Put the files into an array and then loop through the array.

#Include<"file.au3">
$sPath = "C"\old\"
$aFiles = _FileListToArray($sPath, "*.exe", 1) ;; << See the help file
If NOT @Error Then
    For $i = 1 To UBound($aFiles) -1
        If FileCopy($sPath & $aFiles[$i], "C:\New\" & $aFiles[$i], 9) = 0 Then ContinueLoop;; See the help file for the flags
    Next
EndIf

Also the example for FileCopy using wild cards should work for you.

FileCopy("C:\old\*.*", "C:\new\", 9)

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

you are so so sweet all of you..without this forum i wouldnt have made the progress i have made since morning..thanks it seems the array method stands to be the best, the wild card method for some reason skips some files....anywayz thanks again wonderful people

Edited by goodbyeplanet

Share this post


Link to post
Share on other sites

The wildcard method should not skip any files - if the wildcard is set properly.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

The wildcard method should not skip any files - if the wildcard is set properly.

ok to be exact I have these files:

france.exe

france_leys.exe

I tried the wildcard method like this, FileCopy("france*.exe", "C:\new\") but it only copied the first one (france and left the other). One thing that i didnt state is that I also wouldnt want to copy all the files in the folder since there are some which i definitely dont want to be copied an dthat means a wildcard like this, FileCopy("*.exe", "C:\new\") or FileCopy("*.*", "C:\new\") would not be useful for me...

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

This code works fine for me. It creates a new directory if necessary and copies the files from the current directory.

As result 1-0-0 is displayed. Could you please test it in your environment?

$R = FileCopy("france*.exe","C:\new\",9)
ConsoleWrite($R & "-" & @error & "-" & @extended & @CRLF)
Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

This code works fine for me. It creates a new directory if necessary and copies the files from the current directory.

As result 1-0-0 is displayed. Could you please test it in your environment?

$R = FileCopy("france*.exe","C:\new\",9)
ConsoleWrite($R & "-" & @error & "-" & @extended & @CRLF)

oops Water, i feel a bit embarrassed here. Now its working exactly as u said I dont know why it only copied a single file before, probably its because i was working with a lot of code and i lost overview of what was happening..yes thanks again and that means i wil be much more comfortable with the wild card method instead of arraying. since it automatically creates a directory is it necessary for me to specify the DirCreate("C:\new\") before the code. here is my code:

DirCreate("C:\new\")
if @error Then exit     

FileCopy("france*.exe", "C:\new\")
Edited by goodbyeplanet

Share this post


Link to post
Share on other sites

Here is my code:

DirCreate("C:\new\")
if @error Then exit     
FileCopy("france*.exe", "C:\new\")

Don't forget to check the returncode of FileCopy as well!

It is good programming practive to do some error checking after commands that might fail.

In your case the disk could run out of free space, some file attributes can make the overwriting impossible (according to the help filet) etc.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Using the flag 9 (or 8) you don't have to create the folder in advance but it never hurts to do so. As I said above, read the help file to see what the flags are. Also, you could have still used the array method by making the same wildcard change in the second parameter of _FileListToArray() but the straight file copy works just as well.


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

Why does this not work?

$CopyThese = "C:\Temp\Willow.m3u" & "C:\Temp\Yellow.fpl"
FileCopy($CopyThese, "D:\MMM\", 8)

Nothing gets copied; only the D:\MMM folder's created.

Edited by bahtea

Share this post


Link to post
Share on other sites

bahtea,

First, please do not hijack threads. ;)

Second, FileCopy needs a single valid file path_name as a "source" parameter - what you are doing is passing it a combination (concatenation) of 2 such path_names. No wonder it gets confused! :)

If you have a number of files to deal with, put them in an array and then loop through the array passing each one in turn.

All clear? ;)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Sorry. I had no intention of hijacking. Since the topic is on "How do i copy 2 files at once" I had hoped that the kind of variable assigning I had done would be feasible. Partly because I was thinking of verifying later on the presence of the files being copied making use of such a variable.

Yours is good advice, M23: I'll have to study the array aspect.

Quite clear now on "FileCopy needs a single valid file path_name". Thanks.

Share this post


Link to post
Share on other sites

I'm thinking you would have to run separate instances to copy 2 or more files at once. 2 or more scripts or 2 or more DOS copy calls (or some other copy operation)

Share this post


Link to post
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
Sign in to follow this  
Followers 0