【问题标题】:Move files from single folder to existing folders with names like file name将文件从单个文件夹移动到具有文件名等名称的现有文件夹
【发布时间】: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


【解决方案1】:

我已经调整了 Santiago 的代码并找到了现在可行的解决方案:

$source = "C:\Users\xxxxx\Desktop\PDF TEST"
$destination = "C:\Users\xxxxx\Desktop\PDF TEST FOLDERS MOVE"

$filesToSearch = Get-ChildItem $source -Filter *.pdf -Recurse
$destLocations = Get-ChildItem $destination -Directory

foreach($file in $filesToSearch)
{
$searchFolder = -join ($file.BaseName -split '(?=\s)')[0..1]
# Example of how this looks:
# -join ('JOB XXXXXX (then some other not important stuff and a datestamp).pdf' - 
split '(?=\s)')[0..1]
# Returns: JOB XXXXXX

$destFolder = $destLocations | Where-Object {$searchFolder -match 
$_.Name.Substring(0, 10)}
$moveto = $destination + '\' + $destFolder

if(-not $destFolder)
{
    "No folders found matching $searchFolder"
    continue
}

# Make sure here that there is only 1 Destination Folder    
if($destFolder.count -gt 1)
{
    Write-Warning "More than one folder found for $searchFolder"
    continue
}

"- Match found"
"- File: {0}" -f $file.Name
"- Destination Folder: {0}" -f $destFolder.Name
""

$file | Move-Item -Destination $moveto #-WhatIf
}

【讨论】:

    【解决方案2】:

    如果我的逻辑正确,我相信这应该起作用。注意Move-Item 上的-WhatIf,对其进行测试,如果您认为按预期工作,请移除该开关。

    $source = "C:\Users\xxxxx\Desktop\PDF TEST"
    $destination = "C:\Users\xxxxx\Desktop\PDF TEST FOLDERS MOVE"
    
    $filesToSearch = Get-ChildItem $source -Filter *.pdf -Recurse
    $destLocations = Get-ChildItem $destination -Directory
    
    foreach($file in $filesToSearch)
    {
        $searchFolder = -join ($file.BaseName -split '(?=\s)')[0..1]
        # Example of how this looks:
        # -join ('JOB XXXXXX (then some other not important stuff and a datestamp).pdf' -split '(?=\s)')[0..1]
        # Returns: JOB XXXXXX
    
        $destFolder = $destLocations | Where-Object {$searchFolder -match $_.Name}
    
        if(-not $destFolder)
        {
            "No folders found matching $searchFolder"
            continue
        }
    
        # Make sure here that there is only 1 Destination Folder    
        if($destFolder.count -gt 1)
        {
            Write-Warning "More than one folder found for $searchFolder"
            continue
        }
    
        "- Match found"
        "- File: {0}" -f $file.Name
        "- Destination Folder: {0}" -f $destFolder.Name
        ""
    
        $file | Move-Item -Destination $destFolder -WhatIf
    }
    

    我还添加了它以确保您符合预期,但一旦您确认它有效,可以安全地删除它:)

    "- Match found"
    "- File: {0}" -f $file.Name
    "- Destination Folder: {0}" -f $destFolder.Name
    

    【讨论】:

    • 对,我刚刚尝试过,它几乎可以工作 - 我可以看到的问题是文件夹名称包含 XXXXX 之后的文本,因此它与拆分文件名不完全匹配。结果:“找不到与作业 XXXXX 匹配的文件夹”等...
    猜你喜欢
    • 1970-01-01
    • 2022-11-28
    • 1970-01-01
    • 2019-11-07
    • 1970-01-01
    • 1970-01-01
    • 2020-05-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多