我正在嘗試將所有文??件夾(和所有檔案)從一個檔案夾復制到 Powershell 中的另一個檔案夾,其中檔案夾在文本檔案中列出。我有一個成功復制檔案夾的腳本,但檔案沒有復制過來。
$file_list = Get-Content C:\Users\Desktop\temp\List.txt
$search_folder = "F:\Lists\Form601\Attachments\"
$destination_folder = "C:\Users\Desktop\601 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
uj5u.com熱心網友回復:
我將Test-Path在串列中的每個檔案夾上使用以找出該檔案夾是否存在。如果是這樣,請復制。
$folder_list = Get-Content -Path 'C:\Users\Desktop\temp\List.txt'
$search_folder = 'F:\Lists\Form601\Attachments'
$destination_folder = 'C:\Users\Desktop\601 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.."
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/415800.html
標籤:
