复制文件夹结构,但通过两个不同的字符串根据子目录中的文件夹名称匹配子文件夹? [英] Copy folder structure but match subfolders based on the folder name in subdirectories by two different strings?

查看:59
本文介绍了复制文件夹结构,但通过两个不同的字符串根据子目录中的文件夹名称匹配子文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何复制整个文件夹结构,但只能通过两个不同的字符串("Touch"或"mpr")根据文件夹名称向下选择几个子目录中的某些文件夹?

How can one copy an entire folder structure but only select some folders several subdirectories down based on the folder name by two different strings ('Touch' or 'mpr') ?

我需要的文件位于例如:

the files I need are in e.g.:

"C:\Users\Desktop\shizzle\mro\pre\subject_01\Touch"

但是有两个时间点文件夹(Pre和Post)和几个主题,因此我尝试了更高的目录:

but there are two Timepoint-folders (Pre and Post) and several subjects, hence I have tried a higher directory:

$includes = 'Touch|mpr'
Get-ChildItem "C:\Users\Desktop\shizzle\mro" -Directory |
    Where-Object{$_.Name -match $includes} |
    Copy-Item -Destination "C:\Users\shizzle\Desktop\shi_data6" -Recurse -Force

效果很好,但仅当搜索到的文件夹位于指定目录中且没有多个子目录时(因此,它只能在 C:\ Users \ Desktop \ shizzle \ mro \ pre \ subject_01 ).

which works perfectly, but only if the searched folders are in the specified directory and not several subdirectories down (so it would only work in C:\Users\Desktop\shizzle\mro\pre\subject_01).

Get-ChildItem "C:\Users\shizzle\Downloads\copy_test\copy_test\mro" -Recurse | Where-Object{$_.Name -match $includes} | Copy-Item -Destination "C:\Users\shizzle\Downloads\test_shi10" -Recurse -Force

没有保持文件夹结构.

文件夹结构如下:

C:\Users\Desktop\shizzle\mro\

+---pre
|   +---subject_01 
|   |   +---dontcopythisfolder
|   |   +---dontcopythisfolder
|   |   +---0024_63_Touch
|   |   |       File_1.txt
|   |   |       File_2.txt
|   |   |       File_3.txt
|   |   +---dontcopythisfolder
|   |   \---654_mpr_8364
|   |          File_1.txt
|   |          File_2.txt
|   |          File_3.txt
|   \---subject_02
|      +---dontcopythisfolder
|      +---dontcopythisfolder
|      +---0024_63_Touch
|      |       File_1.txt
|      |       File_2.txt
|      |       File_3.txt
|      +---dontcopythisfolder
|      \---654_mpr_8364
|             File_1.txt
|             File_2.txt
|             File_3.txt
\---post
   +---subject_01 
   |   +---dontcopythisfolder
   |   +---dontcopythisfolder
   |   +---0024_63_Touch
   |   |       File_1.txt
   |   |       File_2.txt
   |   |       File_3.txt
   |   +---dontcopythisfolder
   |   \---654_mpr_8364
   |          File_1.txt
   |          File_2.txt
   |          File_3.txt
   \---subject_02
      +---dontcopythis folder
      +---dontcopythisfolder
      +---0024_63_Touch
      |       File_1.txt
      |       File_2.txt
      |       File_3.txt
      +---dontcopythisfolder
      \---654_mpr_8364
             File_1.txt
             File_2.txt
             File_3.txt

推荐答案

您可以尝试以下操作:

$sourcePath  = 'D:\Test'  # 'C:\Users\Desktop\shizzle\mro'
$Destination = 'D:\Data'  # 'C:\Users\shizzle\Desktop\shi_data6'
$includes    = 'Touch|mpr'
Get-ChildItem -Path $sourcePath -Recurse -Directory |
    Where-Object{$_.Name -match $includes} |
    ForEach-Object {
        $targetPath = Join-Path -Path $Destination -ChildPath $_.Parent.FullName.Substring($sourcePath.Length)
        $null = New-Item -Path $targetPath -ItemType Directory -Force
        $_ | Copy-Item -Destination $targetPath -Recurse -Force
    }

除了使用上面的 Substring()方法,您还可以使用正则表达式 -replace 构造目标路径.
但是,请记住,路径包含对正则表达式具有特殊含义的字符,因此您应该使用源代码的 [regex] :: Escape()版本,并将其锚定到字符串以 ^ 开头:

Instead of using the Substring() method above, you can also use regex -replace to construct the target path.
However, keep in mind that a path contains characters that have special meaning for regex, so you should then use the [regex]::Escape()'d version of the source path, and anchor it to the beginning of the string with ^:

$sourcePath  = 'D:\Test'  # 'C:\Users\Desktop\shizzle\mro'
$Destination = 'D:\Data'  # 'C:\Users\shizzle\Desktop\shi_data6'
$includes    = 'Touch|mpr'

# if you want to use regex `-replace` instead, use this:
$escapedSource = '^{0}' -f [regex]::Escape($sourcePath)
Get-ChildItem -Path $sourcePath -Recurse -Directory |
    Where-Object{$_.Name -match $includes} |
    ForEach-Object {
        $targetPath = Join-Path -Path $Destination -ChildPath ($_.FullName -replace $escapedSource)
        $null = New-Item -Path $targetPath -ItemType Directory -Force
        $_ | Copy-Item -Destination $targetPath -Recurse -Force
    }

下面是我的测试结果(当然使用不同的路径.)

Below the results of my test (using different paths of course..)

来源

D:\TEST
+---post
|   +---subject_01
|   |   +---0024_63_Touch
|   |   |       File1.txt
|   |   |       File2.txt
|   |   |
|   |   +---654_mpr_8364
|   |   |       File1.txt
|   |   |       File2.txt
|   |   |
|   |   +---dontcopythisfolder
|   |   \---ignorethisonetoo
|   \---subject_02
|       +---0024_63_Touch
|       |       File1.txt
|       |       File2.txt
|       |
|       +---654_mpr_8364
|       |       File1.txt
|       |       File2.txt
|       |
|       +---dontcopythisfolder
|       \---ignorethisonetoo
\---pre
    +---subject_01
    |   +---0024_63_Touch
    |   |       File1.txt
    |   |       File2.txt
    |   |
    |   +---654_mpr_8364
    |   |       File1.txt
    |   |       File2.txt
    |   |
    |   +---dontcopythisfolder
    |   \---ignorethisonetoo
    \---subject_02
        +---0024_63_Touch
        |       File1.txt
        |       File2.txt
        |
        +---654_mpr_8364
        |       File1.txt
        |       File2.txt
        |
        +---dontcopythisfolder
        \---ignorethisonetoo

目的地

D:\DATA
+---post
|   +---subject_01
|   |   +---0024_63_Touch
|   |   |       File1.txt
|   |   |       File2.txt
|   |   |
|   |   \---654_mpr_8364
|   |           File1.txt
|   |           File2.txt
|   |
|   \---subject_02
|       +---0024_63_Touch
|       |       File1.txt
|       |       File2.txt
|       |
|       \---654_mpr_8364
|               File1.txt
|               File2.txt
|
\---pre
    +---subject_01
    |   +---0024_63_Touch
    |   |       File1.txt
    |   |       File2.txt
    |   |
    |   \---654_mpr_8364
    |           File1.txt
    |           File2.txt
    |
    \---subject_02
        +---0024_63_Touch
        |       File1.txt
        |       File2.txt
        |
        \---654_mpr_8364
                File1.txt
                File2.txt

这篇关于复制文件夹结构,但通过两个不同的字符串根据子目录中的文件夹名称匹配子文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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