从文本文件复制 folders/files 的 Powershell 脚本

Powershell script to copy folders/files from text file

我正在尝试将所有文​​件夹(和所有文件)从一个文件夹复制到 Powershell 中的另一个文件夹,其中文件夹列在一个文本文件中。我有一个成功复制文件夹的脚本,但文件没有复制过来。

$file_list = Get-Content C:\Users\Desktop\temp\List.txt
$search_folder = "F:\Lists\Form601\Attachments\"
$destination_folder = "C:\Users\Desktop1 Attachments 2021b"

foreach ($file in $file_list) {
    $file_to_move = Get-ChildItem -Path $search_folder -Filter $file -Recurse -ErrorAction SilentlyContinue -Force | % { $_.FullName}
    if ($file_to_move) {
        Copy-Item $file_to_move $destination_folder
    }
}

List.text 包含以下文件夹:
4017
4077
4125

我会对列表中的每个文件夹使用 Test-Path 来查明该文件夹是否存在。如果是,请复制。

$folder_list        = Get-Content -Path 'C:\Users\Desktop\temp\List.txt'
$search_folder      = 'F:\Lists\Form601\Attachments'
$destination_folder = 'C:\Users\Desktop1 Attachments 2021b'

# first make sure the destination folder exists
$null = New-Item -Path $destination_folder -ItemType Directory -Force

foreach ($folder in $folder_list) {
    $sourceFolder = Join-Path -Path $search_folder -ChildPath $folder
    if (Test-Path -Path $sourceFolder -PathType Container) {
        # copy the folder including all files and subfolders to the destination
        Write-Host "Copying folder '$sourceFolder'..."
        Copy-Item -Path $sourceFolder -Destination $destination_folder -Recurse
    }
    else {
        Write-Warning "Folder '$folder' not found.."
    }
}