1

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?

aschipfl
  • 33,626
  • 12
  • 54
  • 99
Thomas Ward
  • 13
  • 1
  • 3
  • 1
    I somehow doubt that this question is about DOS (as in Disk Operating System). Neither the Windows Command Prompt nor PowerShell have anything to do with that. – Ansgar Wiechers Oct 18 '16 at 17:57
  • DOS and Command Prompt are often used interchangeably. I think most folks realize what's being talked about. – Nasir Oct 18 '16 at 17:58
  • Ansgar sorry for the lack of proper vocabulary about DOS. I did mean the Command Prompt. As for the duplicate, that post did not answer my question because it searched for the same character in each file, and my files had 6 random numbers in each file bracketed by underscores. – Thomas Ward Oct 18 '16 at 18:36

1 Answers1

3

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+' }
Martin Brandl
  • 56,134
  • 13
  • 133
  • 172
  • Thank you Martin. Your code worked perfectly and solved my problem. I do not understand what _\d+ means. Any chance you could explain that? – Thomas Ward Oct 18 '16 at 18:32
  • gci -Path c:\temp -File -Filter "*.tif" | foreach{$arr=$_.name -split "_"; Rename-Item $_.FullName ($_.DirectoryName + "\" + $arr[0] + "_" + $arr[2]) } – Esperento57 Oct 18 '16 at 18:33
  • 2
    @ThomasWard `_\d+` is a regular expression matching "*an underscore, followed by one or more digits*". See [here](https://regex101.com/r/5LQIVY/1) how it matches against your filenames. – TessellatingHeckler Oct 18 '16 at 18:39