Saya memiliki folder sumber yang akan berisi banyak laporan pdf dengan konvensi penamaan

JOB XXXXXX (then some other not important stuff and a datestamp).pdf

Di mana XXXXXX adalah nomor referensi.

Di folder tujuan ada banyak folder bernama

JOB XXXXXX (other not important info)

Apa yang saya coba lakukan adalah memindahkan pdf apa pun di folder sumber di mana bagian JOB XXXXXX dari nama file cocok dengan bagian JOB XXXXXX dari folder di folder tujuan dan memindahkannya ke folder itu dengan nama yang cocok.

Inilah kode yang saya miliki sejauh ini, saya tidak dapat menemukan kesalahan saya:

$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

      }
      }
0
AntShadow 11 Mei 2021, 18:52

2 jawaban

Jawaban Terbaik

Saya yakin harus ini berfungsi jika logika saya benar. Catat -WhatIf pada Move-Item, uji dan jika menurut Anda berfungsi seperti yang diharapkan, hapus sakelar itu.

$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
}

Saya juga menambahkan ini untuk memastikan Anda cocok dengan apa yang diharapkan, meskipun ini dapat dihapus dengan aman setelah Anda mengonfirmasi itu berfungsi :)

"- Match found"
"- File: {0}" -f $file.Name
"- Destination Folder: {0}" -f $destFolder.Name
0
Santiago Squarzon 11 Mei 2021, 16:37

Saya telah mengubah kode Santiago dan menemukan solusi yang sekarang berfungsi:

$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
}
0
AntShadow 12 Mei 2021, 09:56