复制项递归,但不包括所有子文件夹

问题描述 投票:0回答:1

我正在使用CSV文件来直接复制许多目录和文件。我从此similar question创建了示例。另外,我想选择性地启用递归。有些行我只想复制源目录(0)中的文件,而忽略其中的子文件夹。其他人,我想复制所有内容(1)。本质上,问题是如何在给定目录内的only文件中执行Copy-Item。这里的许多答案都与排除特定文件夹有关。

CSV file

尝试1

Import-CSV C:\Users\XXX\Desktop\test1.csv | foreach{
    If ($_.Recurse -eq 1){
        Copy-item  -Path $_.Source -Destination $_.Destination -Recurse
    } Else {
        Get-ChildItem -Path $_.Source -Recurse -File | 
            Where-Object{ $_.PSIsContainer -eq $False } | 
            ForEach-Object {
                $_ | Copy-Item -Destination $_.Destination -Force
            }
    }
}

非递归项目似乎什么也没发生

尝试2

Import-CSV C:\Users\XXX\Desktop\test1.csv | foreach{
    If ($_.Recurse -eq 1){
        Copy-item -Path $_.Source -Destination $_.Destination -Recurse -Force
    } Else {
        Get-ChildItem -Path $_.Source -Recurse -Depth 0 | Copy-Item -Destination $_.Destination
    }
}

无效,将递归的文件夹复制为文件

powershell
1个回答
0
投票

你是如此亲密。让我知道这是否满足您的需求。

Import-CSV C:\Users\XXX\Desktop\test1.csv | foreach{
    If ($_.Recurse -eq 1){
         Get-ChildItem -Path $_.Source -Recurse | Copy-Item -Destination $_.Destination
    } Else {
        Get-ChildItem -Path $_.Source -Recurse -Depth 0 -File | # Select only files in  directory
        Copy-Item -Destination $_.Destination # Copy files
    }
}

Import-CSV C:\Users\XXX\Desktop\test1.csv | foreach{
    If ($_.Recurse -eq 1){
        Get-ChildItem -Path $_.Source -Recurse | Copy-Item -Destination $_.Destination
    } Else {
        Get-ChildItem -Path $_.Source -Recurse -Depth 0 |  # select files and folders in directory
        Where-Object{ $_.PSIsContainer -eq $False }| # Do not select folders
        Copy-Item -Destination $_.Destination # copy files
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.