Search code examples
powershellbatch-filebatch-rename

Remove numbers from middle of filename


I have 400 tif files with names similar to:

  • 121941_006419_INDUNC.tif
  • 121948_007193_DRILLG.tif
  • 121950_007321_INDUNC.tif

that I need to have an underscore and 6 random numbers removed to look like:

  • 121941_INDUNC.tif
  • 121948_DRILLG.tif
  • 121950_INDUNC.tif

I have searched and the only solutions I have found involve downloading software which my company does not permit.

Is there a way to use a batch file to remove those seven characters from these files?


Solution

  • Since you are using the tag, here a solution:

    Use the Get-ChildItem cmdlet to retrieve your files filtered by .tif and rename them using the Rename-Item cmdlet with a simple regex replace:

    Get-ChildItem -Path c:\tmp -Filter '*.tif' | 
       Rename-Item -NewName { $_.Name -replace '_\d+' }