我需要找出在两个目录中查找匹配文件夹的代码。
Source Directory
Destination Directory找到匹配项后,我需要将源目录中的内容移动到目标目录,然后从源目录中删除该文件夹。我只希望在目标目录中存在与源目录中的文件夹相匹配的文件夹时才会发生这种情况。
文件夹名称的格式为3;15-cr-20154或23-5993
字母和数字每次都会不同,所以我需要搜索文件夹名的格式,例如:
[0-9];[0-9][0-9]-..-[0-9][0-9][0-9][0-9][0-9].感谢您的帮助。
发布于 2015-09-24 05:20:16
您可以使用此模式
(\d+;\d+-\w+-\d+)|(\d+-\d+)请在此处查看演示https://regex101.com/r/wL4iL7/1
说明
(\d+;\d+-\w+-\d+): First group to be captured
(\d+-\d+): second group to be captured第一组的
\d+:matches all numbers
;:matches the semicolon
\d+-: matches all numbers and hyphen
\w+-\d+: matches words hyphen and numbers第二组
\d+-: matches all numbers and hyphen
\d+: matches the last set of numbers发布于 2015-09-24 08:52:53
正则表达式在这里是完全不必要的。如果你只是想在我匹配的时候进行复制,那么你只需要使用一个带有Compare-Object cmdlet的小PowerShell来比较列表。更简单的方法是使用where过滤器,这就是我们要做的。我还假设你不需要递归逻辑,因为你没有请求它。
$sourceDirectory = "f:\temp\source"
$destinationDirectory = "f:\temp\destination"
$sourceFolders = Get-ChildItem -Path $sourceDirectory | Where-Object{$_.PSisContainer} | Select-Object -ExpandProperty Name
$destinationFolders = Get-ChildItem -Path $destinationDirectory | Where-Object{$_.PSisContainer} | Select-Object -ExpandProperty Name
$matchesInBoth = $sourceFolders | Where-Object{$destinationFolders -contains $_}
$matchesInBoth | ForEach-Object{
$sourcePath = (Join-Path $sourceDirectory $_)
Copy-Item -Path $sourcePath -Destination $destinationDirectory -Force -Recurse
Remove-Item $sourcePath -Force -Recurse #-WhatIf
}获取两个目录中的所有文件名。对于这两个目录中的每个目录,我们复制所有内容并删除源目录。
https://stackoverflow.com/questions/32749503
复制相似问题