Regex Powershell根据文件名将文件从目录移动到子目录

Regex Powershell根据文件名将文件从目录移动到子目录,regex,windows,powershell,pattern-matching,batch-processing,Regex,Windows,Powershell,Pattern Matching,Batch Processing,我想使用该模式一次完成所有8k+文件,因为文件的左5个字符与要移动到的子目录的右5个字符相匹配 这是一个接一个的: move-item -path X:\"Property Files"\05165*.pdf -destination X:\"Property Files"\"* -- 05165"; move-item -path X:\"Property Files"\05164*.pdf -destination X:\"Property Files"\"* -- 05164"; 提前感

我想使用该模式一次完成所有8k+文件,因为文件的左5个字符与要移动到的子目录的右5个字符相匹配

这是一个接一个的:

move-item -path X:\"Property Files"\05165*.pdf -destination X:\"Property Files"\"* -- 05165";
move-item -path X:\"Property Files"\05164*.pdf -destination X:\"Property Files"\"* -- 05164";

提前感谢您的帮助。

好的,您的整个RegEx标记都在正确的路径上。我所做的是查找所有内容,直到最后一个反斜杠,然后捕获5位数字,然后在行尾查找所有不是反斜杠的内容,只返回捕获的组。我将其设置为变量
$ItemNumber
,并在目标中使用它。我在ForEach循环中对目标源文件夹中的所有内容运行了该操作。以下是我最终得到的代码:

ForEach($File in (GCI "X:\Property Files\*.PDF")){
$ItemNumber = $File.Fullname -replace ".+?\\(\d{5})[^\\]*$", "`$1"
move-item -path X:\"Property Files"\05165*.pdf -destination X:\"Property Files"\"* -- $ItemNumber"
}
如果你愿意,你可以通过管道来做,就像这样:

GCI "X:\Property Files\*.PDF"|%{move-item -path X:\"Property Files"\05165*.pdf -destination X:\"Property Files"\"* -- $($_.Fullname -replace ".+?\\(\d{5})[^\\]*$", "`$1")"}
但这会有点长,有些人真的不喜欢一行那么长


这个正则表达式可以测试,而且它可以把它全部分解。(链接到regex101.com解释)

作为一行程序,假设目标文件夹已经存在:

Get-ChildItem "X:\Property Files\*.PDF" | 
 ForEach { move -path $_ -destination ($_.directoryname +"\* -- "+ $_.Name.substring(0,5))}
仅使用文件名,您只需提取前五个字符(
子字符串(0,5)
),然后将其用作要匹配的文件夹的结尾。

$\目录名
假设目标文件夹是源路径的子文件夹。

非常感谢。它工作得很好。我用*前面的5位数字尝试了几个文件,然后在整个目录中使用它,当它被测试为有效时,我只把星号放回,然后按enter键。在我的机器上花了大约一个小时,但比以前快多了。感谢您节省的时间。:-)也感谢你的帮助。我正在寻找一个批处理过程,它不需要命名每个文件,所以这不完全符合要求。很高兴了解regex站点。这根本不应该重命名文件,只需移动它们,但我很高兴你找到了一个适合你的解决方案。