【发布时间】:2021-08-02 00:39:45
【问题描述】:
我有一个源文件夹,其中包含一堆带有命名约定的 pdf 报告
JOB XXXXXX (then some other not important stuff and a datestamp).pdf
其中 XXXXXX 是参考号。
在一个目标文件夹中,有许多名为
的文件夹JOB XXXXXX (other not important info)
我要做的是移动源文件夹中的任何 pdf,其中文件名的 JOB XXXXXX 部分与目标文件夹中文件夹的 JOB XXXXXX 部分匹配,并将其移动到具有匹配名称的文件夹。
这是我到目前为止的代码,我只是不知道哪里出错了:
$source = "C:\Users\xxxxx\Desktop\PDF TEST"
$destination = "C:\Users\xxxxx\Desktop\PDF TEST FOLDERS MOVE"
$filesToSearch = (Get-ChildItem $source -Filter *.pdf -Recurse) # | % {($_.name.split('')[0..1] -join ' ')})
$destLocations = Get-ChildItem $destination #| where-object {($_.name.split('')[0..1] -join ' ')}
Get-ChildItem $source -Filter *.pdf -Recurse | ForEach-Object {
if (($destLocations.name.split('')[0..1] -join ' ') -match ($filesToSearch.name.split('')[0..1] -join ' '))
{
Move-Item ($filestosearch.fullname | Out-String) -Destination ($destlocations | out-string) -Force
}
}
【问题讨论】:
-
XXXXXX总是像123456这样的数字吗?在下一个不重要的信息之前,是否总是跟一些字符,如空格、连字符或下划线?XXXXXX是否总是 6 个字符(或数字)? -
嗨 Theo,是的,XXXXXX 总是数字,总是 6 位数字,后跟一个空格
标签: powershell