Need help updating powershell code

This PowerShell loads a CSV with FILENAME and REVISION

Example filename 20000.40.29581_X_Support.dwg
PowerShell removes the entries at the beginning of the filename between the characters after the first period (“.”) and the second period (“.”) then
takes the entry from the REVISION column (in this case 1) and adds ","revision to the ending part of the filename
This code works for Example filename 20000.40.29581_X_Support.dwg it renames the file to 29581_X_Support,1.dwg

I have multiple instances in the csv file that already have the “,“revision in the ending part of the file name but still have the beginning part of the filename for example 20000.41.12345_Y_Support,2.dwg
For those cases with existing “,“revision, how can the code be updated to only remove the part at the beginning of the filename between the characters after the first period (”.”) and the second period (”.”) and leave the revision at the end part untouched? For example, filename 20000.41.12345_Y_Support,2.dwg needs to have the entry in the CSV and the filename renamed to 12345_Y_Support,2.dwg

# Define the path to the CSV file
$csvFile = "C:\data\Data.csv"

# Load the CSV file into a variable
$data = Import-Csv $csvFile

# Define the path to the log file
$logFile = "C:\data\log.txt"

# Loop through each row in the CSV file
foreach ($row in $data) {
  # Extract the parts of the file name
  $fileName = $row.FILE_NAME
  $fileDirectory = Split-Path $fileName -Parent
  $fileBaseName = [System.IO.Path]::GetFileNameWithoutExtension($fileName)
  $fileExtension = [System.IO.Path]::GetExtension($fileName)

  # Modify the file name based on the criteria
  $fileBaseName = $fileBaseName -replace "20000\..*?\.",""
  $newFileName = $fileDirectory + "\" + $fileBaseName + "," + $row.REVISION + $fileExtension

  # Write the file rename operation to the log file
  $logMessage = "$(Get-Date -Format "yyyy-MM-dd HH:mm:ss") - Renamed $fileName to $newFileName"
  Add-Content $logFile $logMessage

  # Rename the file
  Rename-Item $fileName -NewName $newFileName

  # Update the value in the CSV file
  $row.FILE_NAME = $newFileName
}

# Save the changes back to the CSV file
$data | Export-Csv $csvFile -NoTypeInformation

Are the filename formats consistent?

If the ones with a revision always end <revision>.dwg, then you can create a condition to match that, and just replace the first part of the name.

$filename = '20000.41.12345_Y_Support,1.dwg'
if ($filename -match '\d+.dwg$') {
    $filename = $filename -replace '^\d+\.\d+\.'
}
$filename

Hi Matt,
There are also pdf and doc file formats that have the “,revision.extension”

You can just modify the -match statement accordingly:

$filename = '20000.41.12345_Y_Support,1.pdf'
if ($filename -match '\d+.dwg$|\d+.pdf$|\d+.doc$') {
    $filename = $filename -replace '^\d+\.\d+\.'
}
$filename

Thanks for this Matt