简体   繁体   中英

How to move files from folder to folder based on name and Basename

I am trying to move files that have the same basename from a failed folder to a good folder. The code at the moment is moving the FailedDocs folder to the GoodDocs, but I would only like the files within.

Example files below;

  • test.pdf (name)
  • test.pdf.xml (basename)

Files to be moved when test.pdf.xml (when basename is used becomes test.pdf) and equals test.pdf within the Good Folder.

$sourceFiles = @("C:\Temp\FailedDocs")
$destinationFiles = @("C:\Temp\GoodDocs")

function Perform-File-Move {
    for ($i = 0; $i -lt $sourceFiles.Count; $i++) {
        Get-ChildItem $($sourceFiles[$i]) -Recurse | Where-Object {
            $destinationFiles.Name -eq $sourceFiles.BaseName
        } | ForEach-Object {
            if ($destinationFiles.Name -eq $sourceFiles.BaseName) {
                Move-Item -Path $($sourceFiles[$i]) -Destination
                $($destinationFiles[$i]) -Force
            } else {
                Write-Host "Nothing to Move"
            }
        }
    }
}

Perform-File-Move

Thought I would post the answer I came up with for moving the files from the source folder to destination folder based upon basename matching name.

$sourceFiles = "C:\Temp\FailedDocs"
$destinationFiles = "C:\Temp\GoodDocs"


function Perform-Compare-And-Move{
   foreach ($XmlFileName in @(Get-ChildItem -Path $sourceFiles)){
   :outer
   foreach ($PdfFileName in @(Get-ChildItem -Path $destinationFiles -Filter *.pdf)){
       if ($XmlFileName.BaseName -eq $PdfFileName) {
       Move-Item  -Path $sourceFiles\$XmlFileName -Destination $destinationFiles -force
       break :outer
       }
       else{}
      }
   }
}

Perform-Compare-And-Move

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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