【发布时间】:2021-03-21 16:23:38
【问题描述】:
如何通过两个不同的字符串(“Touch”或“mpr”)根据文件夹名称复制整个文件夹结构,但只选择几个子目录下的一些文件夹?
我需要的文件在例如:
"C:\Users\Desktop\shizzle\mro\pre\subject_01\Touch"
但是有两个时间点文件夹(前和后)和几个主题,因此我尝试了更高的目录:
$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 中使用)。
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
【问题讨论】:
-
您需要对
-Destination参数进行字符串操作。-Destination $manipulatedPath而不是-Destination "C:\Users\shizzle\Downloads\test_shi10" -
谢谢,我试过
$manipulatedPath = "C:\Users\shizzle\Downloads\test_shi16"和Get-ChildItem "C:\Users\shizzle\Downloads\copy_test\copy_test\mro" -Recurse | Where-Object{$_.Name -match $includes} | Copy-Item -Destination $manipulatedpath -Recurse -Force但不幸的是没有骰子
标签: powershell recursion match get-childitem copy-item