繁体   English   中英

将文件从单个文件夹移动到具有文件名等名称的现有文件夹

[英]Move files from single folder to existing folders with names like file name

我有一个源文件夹,其中包含一堆 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

      }
      }

如果我的逻辑是正确的,我相信这应该可行。 注意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

我已经调整了 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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM