4

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: enter image description here

Fnkraf
  • 103
  • 3
  • 12
  • Your regex doesn't seem right (^.*) will match any line that starts with an unlimited amount of any character... BTW I would rather not convert the pathes of `$allFiles` to string that early. If you keep the pathes as path objects you can use its members like `.FullName` and `.DirectoryName` later more easily. – T-Me Jan 22 '19 at 10:46
  • How about `[A-Za-z]:(\\[\w\s-]*)+(\.\w{3})` as regex for the path? You dont need to care for the numbers in the beginning of the row. – T-Me Jan 22 '19 at 11:52
  • Does this answer your question? [Extract the filename from a path](https://stackoverflow.com/questions/35813186/extract-the-filename-from-a-path) – cachius Apr 28 '22 at 23:24

2 Answers2

8

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)
marsze
  • 15,079
  • 5
  • 45
  • 61
4

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)
T-Me
  • 1,814
  • 1
  • 9
  • 22
  • He is searching a text for a filepath. The path changed, but the file is somewhere. So he wants to move the file to the position of the text-file. So yes, I assume the location of that text-file exists. – T-Me Jan 22 '19 at 11:30