繁体   English   中英

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

我会在列表中的每个文件夹上使用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.."
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM