Jump to content

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


Recommended Posts

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!

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

Link to comment
Share on other sites

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

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.

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...