Reputation: 134
Using rename
command in a batch file in Windows is there a clear way for remove the First File Extension from all files in a directory. For example
1.txt.png => 1.png
2.txt.png => 2.png
Now I use two renaming methods that work. But in my opinion, these methods look "dirty".
1 method. I don't like the 1st method because I exec rename
twice.
rename *.txt.png *.
rename *.txt *.png
2 method. I don't like the 2nd method because the number of characters "?" can theoretically be smaller than the basic name (without extension) of the file.
rename *.txt.png ??????????????????????????????.png
? Clear method
I would like to run a command like rename *.txt.png *.png
but it doesn't work.
Upvotes: 0
Views: 123
Reputation: 782
@echo off
for %%A in ("*.txt.*") do call :SubRen "%%A"
pause
exit/b
:SubRen
set "Name=%~1"
if /I not "%Name%" == "%Name:.txt.=.%" ECHO ren "%Name%" "%Name:.txt.=.%"
if /I not "%Name%" == "%Name:.txt.=.%" ren "%Name%" "%Name:.txt.=.%"
exit/b
Upvotes: 0
Reputation: 16236
Here is a cmd
command that will rename the file as described. When you are confident that the file(s) will be renamed correctly, remove -WhatIf
from the Rename-Item
command.
If you are using the older Windows PowerShell instead of PowerShell Core, change pwsh
to powershell
.
pwsh -NoLogo -NoProfile -Command ^
"Get-Childitem -Filter '*.*.*' |" ^
"ForEach-Object {" ^
"if ($_.Name -match '(.*)\..*(\..*)') {" ^
"Rename-Item -Path $_.FullName -NewName ($($Matches[1])+$($Matches[2])) -WhatIf" ^
"}" ^
"}"
Upvotes: 1