Sign in to follow this  
Followers 0
sciosalvi

I have a list of files with their full path in an array. How do I copy all those files to a folder?

4 posts in this topic

Hey guys!

Quick question. As the output of a command (dir /b /s *.*) I get a list of files with their full path.

How do I copy them to a folder?

I've tried "file copy", I assume I need to get on a loop and start reading line by line until I get to the end of the var, but I can't figure out how to do it, I'm pretty green still.

Thanks!

Share this post


Link to post
Share on other sites



For $i = 1 To $aMyfilelist[0]
        FileCopy(@ScriptDir & '\' & $aMyfilelist[$i], @DocumentsCommonDir, $FC_OVERWRITE)
            Next

You need to look at a for / next loop to do what you want

This examples assumes you are in the same dir as the files and the destination is just an example

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

There is also a different way to get your files into an array. _FileListToArray

Here is a working sample: (basically the example from the Help File changed to CopyFile's)

#include <File.au3>
#include <Array.au3>
#include <MsgBoxConstants.au3>
$destDir = @ScriptDir & "\DestFolder\"
Example()

Func Example()
    ; List all the files and folders in the desktop directory using the default parameters.
    Local $aFileList = _FileListToArray(@DesktopDir, "*", 1)
    If @error = 1 Then
        MsgBox($MB_SYSTEMMODAL, "", "Path was invalid.")
        Exit
    EndIf
    If @error = 4 Then
        MsgBox($MB_SYSTEMMODAL, "", "No file(s) were found.")
        Exit
    EndIf
    ;==========================================================
    For $i = 1 To $aFileList[0]
;~        ConsoleWrite("$aFileList[$i] =  " & $aFileList[$i] & @CRLF)
          FileCopy(@DesktopDir & "\" & $aFileList[$i], $destDir, 9)
;~        ConsoleWrite("@DesktopDir & $aFileList[$i] =  " & @DesktopDir & $aFileList[$i] & @CRLF)
          Next
    ;==========================================================
;~  _ArrayDisplay($aFileList, "$aFileList")
EndFunc   ;==>Example

Depending on the size of your files you might want to add a MsgBox tp tell you when the copy process is finished.

Bill

Edited by l3ill

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Hello.

First _FileListToArrayRec all files with $iReturn = 1 (files only). Store relative path is default in _FileListToArrayRec, so don't bother with that

Then use the paths in a For loop:

#include <File.au3>

Local $szSourceDir = "C:\SourceDir\"
Local $szDestDir = "C:\DestDir\"

Local $aFiles = _FileListToArrayRec($szSourceDir, "*", 1, 1)

For $i = 1 To $aFiles[0]
    FileCopy($szSourceDir&$aFiles[$i], $szDestDir&$aFiles[0], $FC_CREATEPATH)
Next

Of course, you will have to make some error checking (file listing, file exists, overwriting, etc.), which is lacked in my example.

BR.

CiV.

Edited by CiVQ

I know that my code is ugly. But it works. Mostly.

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