0

OK so I have a folder full of files. Each of these files have a 12 character extension (date time stamp) added to them (gets generated each day as the files are delivered to another system).

So I have files

abcd.txt.161205042500

efgh.txt.112845035688

etc.

I want PS to mass rename these files. Basically trim the last 13 characters from the filename for each file in the folder.

Everything I've found has been where you can replace a string, but I don't have anything to search by here.

Any help is appreciated.

  • What if the name isn't unique after stripping the 13 char extension? –  Dec 05 '16 at 22:24
  • Not a concern in this case. We get 1 file per day, and the date is in the file name. It gets picked up, and a date/time extension added to it. – uneasyrider Dec 05 '16 at 22:28
  • Possible duplicate of [How do I get a file path without extension in PowerShell?](http://stackoverflow.com/questions/14770587/how-do-i-get-a-file-path-without-extension-in-powershell) and [removing-path-and-extension-from-filename-in-powershell](http://stackoverflow.com/questions/12503871/) and [file-output-in-powershell-without-extension](http://stackoverflow.com/questions/13370055/) – TessellatingHeckler Dec 05 '16 at 22:39

2 Answers2

1

If the output looks right, remove the -whatif

Get-ChildItem *.txt.* -file |Foreach { if ($_.Extension.length -eq 13) {
    rename-item $_.FullName -NewName $_.Basename -whatif
  }
}
0
Get-ChildItem  "c:\temp" -file  | where name -match ".\.txt\.[0-9]{12}$" | %{rename-item $_.FullName $_.Basename}

short version

gci "c:\temp" -file  | ? name -m ".\.txt\.[0-9]{12}$" | %{rni $_.Fullname $_.Basename}
Esperento57
  • 16,521
  • 3
  • 39
  • 45