My guess is that after moving the files and associated bitmap files, you would also want to have the file contain the new filepaths for these referenced files.
Also, since your patterns aren't really regular expressions, I used the -SimpleMatch parameter on Select-String.
This code should do just that.
$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 = '24BHD'
$pattern2 = 'RuheEKG'
# create the destination paths if they do not exist
if (!(Test-Path -Path $destination -PathType Container)) {
Write-Host "Creating folder '$destination'"
New-Item -Path $destination -ItemType 'Directory' -Force | Out-Null
}
if (!(Test-Path -Path $toDelete -PathType Container)) {
Write-Host "Creating folder '$toDelete'"
New-Item -Path $toDelete -ItemType 'Directory' -Force | Out-Null
}
# get an array of full path and filenames.
# if they all have the same extension, it would be wise to add the '-Filter' parameter..
$allFiles = @(Get-ChildItem $source -File | Select-Object -ExpandProperty FullName)
foreach($file in $allFiles) {
# get the file content as array so we can reuse it and update the line(s) with the new bitmap path(s)
$content = Get-Content -Path $file
# first decide on the destination. '-Quit' returns $true or $false
# if both $pattern1 AND $pattern2 are present, move stuff to $destination
if (($content | Select-String -Pattern $pattern1 -SimpleMatch -Quiet) -and
($content | Select-String -Pattern $pattern2 -SimpleMatch -Quiet)) {
$dest = $destination
}
else {
$dest = $toDelete
}
# next check if the file contains path(s) for referenced (bitmap) file((s)
$refCount = 0
$content | Select-String -Pattern '(^.*)([A-Z]:\\.+$)' -AllMatches | ForEach-Object {
# each '$_' automatic variable in here holds a MatchInfo object.
# see: https://learn.microsoft.com/en-us/dotnet/api/microsoft.powershell.commands.matchinfo?view=pscore-6.0.0
$prefix = $_.Matches[0].Groups[1].Value # get the prefix of the line (something like '0619154')
$refPath = $_.Matches[0].Groups[2].Value # get the bitmap file path
if (Test-Path -Path $refPath -PathType Leaf) {
Write-Host "Moving referenced file '$refPath' to '$dest'"
Move-Item -Path $refPath -Destination $dest -Force
# recreate the line to match the new location of the bitmap file
Write-Host "Updating path in '$file' to '$refPath'"
$refFile = Split-Path $refPath -Leaf
$refPath = Join-Path -Path $dest -ChildPath $refFile
$content[$_.LineNumber -1] = $prefix + $refPath
$refCount++
}
else {
Write-Warning "Referenced file '$refPath' not found"
}
if ($refCount) {
# we handled referenced files, so write the new content back to the original file
Set-Content -Path $file -Value $content -Force
}
}
# finally move the file to its new destination
Write-Host "Moving file '$file' to '$dest'"
Move-Item -Path $file -Destination $dest -Force
}
EDIT
As per your comments:
I have tested this like below.
I created a couple of folders on my D: drive and put files in there:
+---Fnkraf
\---Bitmaps
| PIC0053.BMP
| PIC0057.BMP
| PIC0571.BMP
| PIC0572.BMP
|
\---MasterFiles
File1.txt
File2.txt
File3.txt
The Bitmaps folder contains the referenced bitmap files.
In the MasterFiles folder, I put these files:
File1.txt
This file is valid, because it contains both keyword patterns and has two referenced bitmap files. Both referenced files are present. These will go to the 24BHD folder.
24BHD
RuheEKG
01091521
0249153EKG 10 Sekunden
0619154D:\Fnkraf\Bitmaps\PIC0053.BMP
0619155D:\Fnkraf\Bitmaps\PIC0057.BMP
0118410HF
File2.txt
This file is valid, because it contains both keyword patterns and has two referenced bitmap files. One of which will spit out a warning because it cannot be found. These will go to the 24BHD folder.
24BHD
RuheEKG
01091521
0249153EKG 15 Sekunden
0719154D:\Fnkraf\Bitmaps\PIC0571.BMP
0719157D:\Fnkraf\Bitmaps\DOESNOTEXIST.BMP
0118410HG
File3.txt
This file is not valid, because it contains only one keyword pattern. It does have a findable referenced bitmap file. These should go to the toDelete folder
25BHD
RuheEKG
01091521
0249153EKG 17 Sekunden
0619154D:\Fnkraf\Bitmaps\PIC0572.BMP
0118410HG
After running the script, this is the result:
+---Fnkraf
\---24BHD
| File1.txt
| File2.txt
| PIC0053.BMP
| PIC0057.BMP
| PIC0571.BMP
|
+---Bitmaps
+---MasterFiles
\---ToDelete
File3.txt
PIC0572.BMP
You can see both the destination 24BHD and the toDelete folder are created and master files File1.txt and File2.txt ended up in the destination, along with their referenced bitmap files.
File3.txt failed the pattern test as expected and was moved to the toDelete folder, again with the referenced bitmap file.
Now, if you open up the moved text files, you can see the referenced file paths have been updated to match the new location of the bitmaps.
File1.txt
24BHD
RuheEKG
01091521
0249153EKG 10 Sekunden
0619154D:\Fnkraf\24BHD\PIC0053.BMP
0619155D:\Fnkraf\24BHD\PIC0057.BMP
0118410HF
The same was done for the other files. The only reference that was NOT updated is the bitmap file that could not be found in File2.txt:
24BHD
RuheEKG
01091521
0249153EKG 15 Sekunden
0719154D:\Fnkraf\24BHD\PIC0571.BMP
0719157D:\Fnkraf\Bitmaps\DOESNOTEXIST.BMP
0118410HG
Hope that explains it all.