I want to write a powershell script which should be able to remove searched string and everything after the searched string from a filename.
My code looks like this:
$path = "U:\PowerShell_Scripts\Test files"
$text1 = "_FULL.001_020_"
$text2 = "_HIGH.001_020_"
Get-ChildItem -Path $path -Filter "*$text1*" -Recurse |
Rename-Item -NewName {$_.Name -replace $text1, ''}
Get-ChildItem -Path $path -Filter "*$text2*" -Recurse |
Rename-Item -NewName {$_.Name -replace $text2, ''}
but it is not doing what I need of course. I have been looking the whole day via google, but this particular method is not possible to find, or I am using probably wrong search criteria.
Filenames which names should be manipulated:
SVT_ALL_HU__MGU_01__FULL.001_020_040.xml SVT_ALL_HU__MGU_01__FULL.001_020_080.xml SVT_ALL_HU__MGU_01__HIGH.001_020_249.xml
This is not a duplicate of "How to remove part of a string in powershell using Regular Expressions", because there is no multiple filename manipulation mentioned with starting fixed string.
Get-Help about_Regular_Expressionsmay be useful here - but remember that PowerShell is not a Linux shell, and requires a different mindset.SVT_ALL_HU__MGU_01_040.xml SVT_ALL_HU__MGU_01_080.xml SVT_ALL_HU__MGU_01_249.xmlin a test. So what's the problem?