-1

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.

5
  • 1
    Have you looked at PowerShell's own help on regex? Get-Help about_Regular_Expressions may be useful here - but remember that PowerShell is not a Linux shell, and requires a different mindset. Commented Aug 9, 2017 at 14:07
  • Possible duplicate of How to remove part of a string in powershell using Regular Expressions Commented Aug 9, 2017 at 14:08
  • Please read up on regular expressions and how they differ from globbing expressions. Commented Aug 9, 2017 at 14:13
  • Aside from the dot matching any character as a RegEx I don't see a problem with your code. Here the files got renamed to SVT_ALL_HU__MGU_01_040.xml SVT_ALL_HU__MGU_01_080.xml SVT_ALL_HU__MGU_01_249.xml in a test. So what's the problem? Commented Aug 9, 2017 at 18:02
  • The problem is the rest is still there in the filename. I want to find that specific string and then delete this string and everything what comes after that string. Beside that I need to make parallel process with same file, because the files are coming from developers with some funny stuff at start of filename (like: [{[]}]) which I need to delete manually and thats very annoying if I need to process over hundred files. Thatswhy I am asking for help, because I am not working with Regexp on everyday basis. Commented Aug 9, 2017 at 18:44

1 Answer 1

-2

This should do what you're requesting:

$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 '__F.*\.','.')
Get-ChildItem -Path $path -Filter "*$text2*" -Recurse |
  Rename-Item -NewName ($_.Name -replace '__H.*\.','.')

It will take SVT_ALL_HU__MGU_01__FULL.001_020_080.xml and make it SVT_ALL_HU__MGU_01.xml

Sign up to request clarification or add additional context in comments.

4 Comments

This is not working. I've tried it at home, even moved the directory outside from "C:\Users" to enhance naming compatibility to C:\Test\Files, but nothing happens with files when I run the code.
@PavolDurica Edited to remove script blocks. Those shouldn't be there.
Rename-Item : Cannot bind argument to parameter 'NewName' because it is an empty string. At C:\Test\File_rename2.ps1:4 char:78 + ... t1*" -Recurse | Rename-Item -NewName ($_.Name -replace '__F.*\.','.') + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidData: (:) [Rename-Item], ParameterBindingValidationExcept ion + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft. PowerShell.Commands.RenameItemCommand
You MUST use scriptblocks there. Parentheses won't work, because with them PowerShell won't defer the expansion of $_ (i.e. $_ is expanded when the Get-ChildItem ... | Rename-Item ... statement is parsed rather than when Rename-Item is invoked).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.