Split a Path and take out only the last part (filename) Powershell

故事扮演 提交于 2021-02-10 19:58:55

问题


I'm very new to powershell and im currently trying to write a script that finds a referenced filepath in a file, takes out only the last part of the path (The filename) and moves it to the same destination like the folder containing it.

I have a functional script that does what i want, the only thing left is that its not supposed to look for the whole path of the referenced file. Because the path isnt correct anymore. It should just look for the filename and find and move it.

This is my current script:

    $source      = 'Z:\Documents\16_Med._App\Aufträge\RuheEKG_24HBP_Skript\Ursprung_test'
$destination = 'Z:\Documents\16_Med._App\Aufträge\RuheEKG_24HBP_Skript\24BHD'
$toDelete    = 'Z:\Documents\16_Med._App\Aufträge\RuheEKG_24HBP_Skript\ToDelete'
$pattern1    = 'AmbulatoryBloodPressure'
$pattern2    = 'RuheEKG'


# Erstellt Array mit pfad und filename
$allFiles = @(Get-ChildItem $source -File | Select-Object -ExpandProperty FullName)

foreach($file in $allFiles) {
    # Dateinhalt als Array
    $content = Get-Content -Path $file

    # Wählt Destinationspfad
        if ($content | Select-String -Pattern $pattern1 -SimpleMatch -Quiet)  
         {
        $dest = $destination
    }
    else {
        $dest = $toDelete
    }

    # Prüft ob Datei einen Pfad enthält
    $refCount = 0
    $content | Select-String -Pattern '(^.*)([A-Z]:\\.+$)' -AllMatches | ForEach-Object {


        $prefix  = $_.Matches[0].Groups[1].Value   
        $refPath = $_.Matches[0].Groups[2].Value   # Bitmap file Path wird geholt

        if (Test-Path -Path $refPath -PathType Leaf) {
            Write-Host "Moving referenced file '$refPath' to '$dest'"
            Move-Item -Path $refPath -Destination $dest -Force


        }
        else {
            Write-Warning "Referenced file '$refPath' not found"
        }
    }
    $refPath -split "\"
       $refPath[4]
    write-host $refpath

    # Bewegt die Files an die vorher festgelegte Destination.
    Write-Host "Moving file '$file' to '$dest'"
    Move-Item -Path $file -Destination $dest -Force
}

This is the referenced bitmap file:


回答1:


You have several options to savely work with file-paths and -names in PowerShell.

Built-in cmdlets

# Get the file name
$fileName = Split-Path $refPath -Leaf

# Get the directory path
$dirPath = Split-Path $refPath -Parent

.NET methods

# Get the file name
$fileName = [System.IO.Path]::GetFileName($refPath)

# Get the directory path
$dirPath = [System.IO.Path]::GetDirectoryName($refPath)

If you want to look for the filename in another directory, you can build a new path like this:

# built-in version
$otherPath = Join-Path $otherDir $fileName

# .NET version
$otherPath = [System.IO.Path]::Combine($otherDir, $fileName)



回答2:


To get the filename of the file you want to move, use the Split-Path as marsze suggests.

$FileName = Split-Path $refPath -Leaf

To locate the file use Get-ChildItem. ($SearchBase is the path where you want to search)

$FileLocation = (Get-ChildItem -Path $SearchBase -Filter $filename -Recurse -File).FullName

Now to move the file use Move-Item and again use Split-Path to find the destination.

Move-Item -Path $FileLocation -Destination $(Split-Path $file -Parent)


来源:https://stackoverflow.com/questions/54305545/split-a-path-and-take-out-only-the-last-part-filename-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!