繁体   English   中英

Powershell复制所有文件夹和文件夹中的文件

[英]powershell copy all folders and files in folder

对你们来说可能非常容易。

我想将所有文件夹和文件从此路径c:\\ default \\复制到此目标c:\\ environment \\ customfolder \\,在customerfolder文件夹中还有其他文件夹,它们的名称不同。 它只应将文件和文件夹复制到目标文件夹中自定义文件夹包含名称DEMO_test的位置

最好和最简单的方法是什么? 我应该使用for-each吗?

谢谢你的帮助。

抱歉,我应该更清楚。 ;-)

我有一个文件夹c:\\ default

该文件夹中的所有文件和子文件夹c:\\ default

应该复制到这些文件夹

c:\\ environment \\ customfolder \\ demo_test

c:\\ environment \\ customfolder \\ demo_test01

c:\\ environment \\ customfolder \\ demo_test02

c:\\ environment \\ customfolder \\ demo_test03

我知道应该可以从此路径(源)复制所有文件和子文件夹c:\\ default \\

到此路径(目标)c:\\ environment \\ customfolder \\

并且仅将其复制到具有demo_test *的名称的文件夹中

这个问题更好吗?

谢谢。

获取文件列表:

Get-ChildItem -Path "C:\default\" -Recurse

-Recurse参数搜索子文件夹。

现在过滤列表以仅显示符合特定格式的文件

Get-ChildItem -Path "C:\default\" -Recurse |
    Where-Object Name -like "*test*"

注意管道| 有效地将这些命令链接在一起。

现在,将过滤后的项目列表复制到目标文件夹:

Get-ChildItem -Path "C:\default\" -Recurse |
    Where-Object Name -like "*test*" | 
    Copy-Item -Destination "C:\destination\"

如果我从根本上理解您,则目录的结构扁平:源:C \\ Catalog [lookupCatalogs] \\ files DEST:c:\\ Catalog \\ SomeCatlogs [lookupCatalogs] \\ files

如是,

此功能应该可以:

function copy-TestdemoFolders
{
    param ($source,
    $destination,
    $filter,
    $recursive = $false)


$folders = Get-ChildItem  $source -filter $filter -Directory

$folders | % {

    $copyPath = Join-Path $destination $_.name

    if (!(Test-Path $copyPath))
    {
        New-Item $copyPath -ItemType Directory | Out-Null
        "Created New Folder: $($_.name)"
    }

    $scriptBlock = { Get-ChildItem $_.Fullname }

    if ($recursive -eq $true)
    {
        $scriptBlock = { Get-ChildItem $_.Fullname -Recurse }
    }

    Invoke-Command -ScriptBlock $scriptBlock | %{

        Copy-Item $_.Fullname $copyPath -ErrorAction SilentlyContinue

        if (!($?)) { $error[0].Exception.Message }

    }
  }
}


copy-TestdemoFolders -source 'C:\Source' -filter "*demo_test*" -destination D:\TEST

您可以使用开关copy-TestdemoFolders -source 'C:\\Source' -filter "*demo_test*" -destination D:\\TEST -recursive:$true将文件从子文件夹递归复制到[lookupCatalog]

暂无
暂无

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

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