【问题标题】:how to copy folders from one location to another excluding files in the folders如何将文件夹从一个位置复制到另一个位置,不包括文件夹中的文件
【发布时间】:2015-04-20 20:05:24
【问题描述】:

我只需要将文件夹从一个位置复制到文件系统上的另一个位置,不包括文件夹内的文件。

我尝试过的以下 sn-p 是复制文件夹和文件 例如:

MoveToPath\A\B\X.xml。复制项目 $MoveFromPath $MoveToPath -recurse -EA SilentlyContinue

我只想要“MoveToPath\A\B”

【问题讨论】:

  • 尝试使用谷歌搜索 Get-Childitem 管道到 where 子句,您可能希望使用它并将其管道到过滤掉文件并在目标位置创建文件夹的 where 子句中。
  • 获取子项 c:\temp -Recurse | ?{ $_.PSIsContainer } |copy-item -destination c:\target 将复制目录,但不维护目录结构。即,c:\temp\test\sub1 最终将成为 c:\target\sub1。
  • 另外,你可以调用 robocopy: robocopy c:\temp c:\target /e /xf *

标签: powershell copy-item


【解决方案1】:

这是保留结构,但您必须(显然)拥有目标文件夹的权限才能创建对象。

Clear-Host
$Source = "C:\SourceFolder"
$Destination = "D:\DestinationFolder"

Get-ChildItem $Source* -Recurse | ForEach-Object{
    if($_.Attributes -eq 'Directory')
    {
        # Build new files path with source folder name
        [string] $dirName = "$Destination\" + $_.BaseName

        # Create new folder under destination directory
        New-Item dirName -ItemType directory

        # Inform user of destination folder name
        Write-Host $dirName
    }
}

【讨论】:

    猜你喜欢
    • 2011-05-03
    • 2021-12-18
    • 2018-06-25
    • 1970-01-01
    • 1970-01-01
    • 2015-04-16
    • 1970-01-01
    • 2013-02-02
    • 2022-12-05
    相关资源
    最近更新 更多