Noob到PowerShell,复制文件夹中的文件 [英] Noob to PowerShell, copying files within folder

查看:146
本文介绍了Noob到PowerShell,复制文件夹中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉,如果这是一个非常简单的过程。

Sorry if this is a really simple process.

我有一个文件夹结构,在根级别(空)约4,000个目录,

I've got a folder structure with ~4,000 directories at root level (empty), and another location with another set of folders with data.

我想只将location2中的文件夹中的文件复制到location1中的文件夹,即\FolderABC123\Archive,但只有在目录名称匹配。我确定windows explorer复制,或xcopy或其他一些实用程序可以做更容易,但我想把我的头,进入PowerShell,认为这将是一个好的开始?

I want to copy only the files from within the folders in location2 to the folders within location1 ie \FolderABC123\Archive, but only if the directory names match. I'm sure windows explorer copy, or xcopy or some other utility can do it with more ease, but I'd like to get my head into PowerShell, and think this would be a good start?

谢谢。
Chris

thank you. Chris

推荐答案

    # Fist, get a list of all target directories (i.e.: directories under $destination that have a match in $source)

    $targetDirs = dir -Path $source -Recurse -Force |
        ?{ $_.psIsContainer } |
        %{ $_.FullName -replace [regex]::Escape($source), $destination } |
        %{ if (Test-Path $_) { $_ }}

    # Then, enumerate all files from source and copy them only if the corresponding target dir exists

    dir -Path $source -Recurse -Force | 
        ?{ -not $_.psIsContainer } |
        ?{ Test-Path (Split-Path ($_.FullName -replace [regex]::Escape($source), $destination) -Parent) } |
        copy -Force -Destination { $_.FullName -replace [regex]::Escape($source), $destination }

这篇关于Noob到PowerShell,复制文件夹中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆