【问题标题】:how to copy files from a folder and also files in sub folder in powershell script如何从文件夹中复制文件以及powershell脚本中的子文件夹中的文件
【发布时间】:2016-02-15 08:38:50
【问题描述】:

如何使用 powershell 脚本从文件夹及其子文件夹中复制 .txt 类型的文件。如果文件我是子文件夹,我希望子文件夹也复制到目标。谢谢

我尝试了以下

Copy-item c:\tes\* *.txt c:\dest -recurse -force

这只会复制根文件夹中的文件,而不是子文件夹中的文件。

【问题讨论】:

    标签: powershell nuget-package


    【解决方案1】:

    这可能不是最好的解决方案,但应该可以:

    我使用Get-ChildItem cmdlet 过滤所有*.txt文件,确定新路径,使用md 创建新目录,最后使用Copy-Item cmdlet 复制文件:

    $source = 'D:\test1'
    $destination = 'D:\test2'
    
    Get-ChildItem -Path $source -Filter *.txt -Recurse | % {
     $newDirectory = Join-Path $destination ($_.DirectoryName.Substring($source.Length))
      md $newDirectory -Force | out-null
      Copy-Item $_.FullName -Destination $newDirectory
     } 
    

    【讨论】:

    • 您好,谢谢。如果目录已经存在,这个命令会创建目录吗?
    • 它只是确保目录存在。如果您愿意,也可以将其包装成 Test-Path,但这也应该始终有效。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-07
    • 1970-01-01
    • 2013-02-27
    • 1970-01-01
    • 1970-01-01
    • 2022-06-13
    • 2018-11-21
    相关资源
    最近更新 更多