#include <File.au3>
#include <Array.au3>
; Путь к файлу со списком
Local $filelistPath = @ScriptDir &"\filelist.txt"
; Папки для поиска
Local $searchDirs[2] = [ "\\PC-02\Distrib", "\\PC-02\Music" ] ;перечисляем каталоги поиска
; Папка для копирования
Local $destinationDir = @ScriptDir &"\catalog"
; Создаем папку назначения, если она не существует
If Not FileExists($destinationDir) Then
DirCreate($destinationDir)
EndIf
; Читаем список файлов и каталогов
Local $fileList = FileReadToArray($filelistPath)
If @error Then
MsgBox(0, "Ошибка", "Не удалось прочитать файл filelist.txt")
Exit
EndIf
; Функция для поиска и копирования файлов и каталогов
Func FindAndCopyFiles($searchDirs, $fileList, $destinationDir)
For $i = 0 To UBound($searchDirs) - 1
Local $searchDir = $searchDirs[$i]
_SearchAndCopy($searchDir, $fileList, $destinationDir)
Next
EndFunc
; Рекурсивная функция для поиска и копирования
Func _SearchAndCopy($currentDir, $fileList, $destinationDir)
Local $hSearch = FileFindFirstFile($currentDir & "\*")
If $hSearch = -1 Then Return
While 1
Local $file = FileFindNextFile($hSearch)
If @error Then ExitLoop
; Полный путь к текущему файлу или каталогу
Local $sourcePath = $currentDir & "\" & $file
; Проверяем, является ли это файлом или каталогом
If StringInStr($file, ".") Then ; Это файл
For $j = 0 To UBound($fileList) - 1
If StringCompare($file, $fileList[$j]) = 0 Then
Local $destPath = $destinationDir & "\" & $file
FileCopy($sourcePath, $destPath, 1) ; 1 - перезаписать
ConsoleWrite("Скопирован файл: " & $sourcePath & " -> " & $destPath & @CRLF)
EndIf
Next
Else ; Это каталог
; Проверяем, есть ли каталог в списке
For $j = 0 To UBound($fileList) - 1
If StringCompare($file, $fileList[$j]) = 0 Then
Local $destPath = $destinationDir & "\" & $file
If Not FileExists($destPath) Then
DirCreate($destPath) ; Создаем каталог в папке назначения
EndIf
ConsoleWrite("Скопирован каталог: " & $sourcePath & " -> " & $destPath & @CRLF)
; Рекурсивный вызов для копирования содержимого каталога
_CopyDirectory($sourcePath, $destPath)
EndIf
Next
EndIf
WEnd
FileClose($hSearch)
EndFunc
; Функция для рекурсивного копирования каталога
Func _CopyDirectory($sourceDir, $destDir)
Local $hSearch = FileFindFirstFile($sourceDir & "\*")
If $hSearch = -1 Then Return
While 1
Local $file = FileFindNextFile($hSearch)
If @error Then ExitLoop
Local $sourcePath = $sourceDir & "\" & $file
Local $destPath = $destDir & "\" & $file
If StringInStr($file, ".") Then ; Это файл
FileCopy($sourcePath, $destPath, 1) ; 1 - перезаписать
ConsoleWrite("Скопирован файл: " & $sourcePath & " -> " & $destPath & @CRLF)
Else ; Это каталог
If Not FileExists($destPath) Then
DirCreate($destPath)
EndIf
ConsoleWrite("Скопирован каталог: " & $sourcePath & " -> " & $destPath & @CRLF)
_CopyDirectory($sourcePath, $destPath)
EndIf
WEnd
FileClose($hSearch)
EndFunc
; Запускаем поиск и копирование
FindAndCopyFiles($searchDirs, $fileList, $destinationDir)