Iftek Har
Iftek Har

Reputation: 161

Rename files by CMD keep last 10 characters and remove all starting characters

I have thousands of HTML files names like "jweofimsdkfmoiwe-8592547472.html.tmp" and "jweofimsdkfmoiwe-8592547472.html.readme" I want to rename all files by command and just want last 10 characters and .html like 8592547472.html. I am not expert but by searching I found follwoing command to remove all .tmp and .readme

for /f "delims=." %a in ('dir /b *.html.readme') do ren "%~a.html.readme" "%~a.html"
for /f "delims=." %a in ('dir /b *.html.tmp') do ren "%~a.html.tmp" "%~a.html"

I searched a lot and found many solutions but only for removing starting characters.

REN *.* " *.*" 
FOR %v IN (*.*) DO REN "%v" %v

I want a simple code which rename files and keep last 10 characters and .html will be very nice if it includes removing last .tmp and .readme

How can convert it to use in CMD?
::Extract only the last 10 characters
SET _result=%_test:~-10%
ECHO %_result%          =8592547472

Thanks in advance.

Upvotes: 0

Views: 4412

Answers (2)

Mofi
Mofi

Reputation: 49086

The following batch file can be used if there is always just one hyphen between the file name part to remove and the file name part to keep.

@echo off
setlocal EnableExtensions DisableDelayedExpansion
for /F "eol=| delims=" %%I in ('dir *.html.readme *.html.tmp /A-D /B 2^>nul') do for /F "eol=| tokens=1* delims=-" %%J in ("%%~nI") do ren "%%I" "%%K"
endlocal

The inner FOR processes the file name string without extension .readme or .tmp and assigns everything left to first (series of) - to specified loop variable J (not further used) and everything after first (series of) - to next but one loop variable K according to the ASCII table. Hyphens at beginning of a file name are removed before assigning first hyphen delimited substring to not used loop variable J.

Otherwise the following batch file could be used for this file renaming task with file names containing inside more than one hyphen:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
for /F "eol=| delims=" %%I in ('dir *.html.readme *.html.tmp /A-D /B 2^>nul') do (
    set "SourceFileName=%%I"
    for %%J in ("%%~nI") do set "TargetFileName=%%~nJ"
    setlocal EnableDelayedExpansion
    set "TargetFileName=!TargetFileName:~-10!"
    ren "!SourceFileName!" "!TargetFileName!.html"
    endlocal
)
endlocal

Please read this answer for details about the commands SETLOCAL and ENDLOCAL. It is necessary to enable/disable delayed expansion inside the FOR loop as otherwise it would not be possible to process file names correct containing one or more exclamation marks.

This batch file really uses always the last 10 characters of the source file name for the target file name.

The following batch file can be used if there is guaranteed that no file to rename contains one or more ! in its name.

@echo off
setlocal EnableExtensions EnableDelayedExpansion
for /F "eol=| delims=" %%I in ('dir *.html.readme *.html.tmp /A-D /B 2^>nul') do (
    for %%J in ("%%~nI") do set "TargetFileName=%%~nJ"
    ren "%%I" "!TargetFileName:~-10!.html"
)
endlocal

One more solution similar to above working for file names with one or more exclamation marks by not using delayed expansion, but use command CALL to double parse the command line with command REN before execution of the rename command.

@echo off
setlocal EnableExtensions DisableDelayedExpansion
for /F "eol=| delims=" %%I in ('dir *.html.readme *.html.tmp /A-D /B 2^>nul') do (
    for %%J in ("%%~nI") do set "TargetFileName=%%~nJ"
    call ren "%%I" "%%TargetFileName:~-10%%.html"
)
endlocal

To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.

  • call /?
  • dir /?
  • echo /?
  • endlocal /?
  • for /?
  • ren /?
  • set /?
  • setlocal /?

Upvotes: 1

Compo
Compo

Reputation: 38613

For your provided examples, if you're using Windows Vista or later, (and as long as you don't have any files which have more than those two levels of extension e.g. *.html.readme.ext). The following should rename your *.html files and *.html.ext files, (where .ext is any other valid extension), which are immediately preceded by a string consisting of a hyphen, -, then 10 digits.

For /F Delims^= %G In ('%__AppDir__%where.exe .:*-??????????.html* 2^>NUL^|%__AppDir__%findstr.exe "[-][0123456789][0123456789][0123456789][0123456789][0123456789][0123456789][0123456789][0123456789][0123456789][0123456789]\.html"') Do @Echo "%~nxG"|%__AppDir__%findstr.exe "\".*[-][0123456789][0123456789][0123456789][0123456789][0123456789][0123456789][0123456789][0123456789][0123456789][0123456789]\.html\"$">NUL&&(Set "_=%~nxG")||(For /F "EOL=?Delims=" %H In ("%~nG")Do @Echo "%~nxH"|%__AppDir__%findstr.exe "\".*[-][0123456789][0123456789][0123456789][0123456789][0123456789][0123456789][0123456789][0123456789][0123456789][0123456789]\.html\"$">NUL&&Set "_=%~nxH")&%__AppDir__%cmd.exe /V/Q/D/C "Ren "%G" "!_:~-15!""

I'm not going to work through it to explain what is happening and how it works, because trying to type all of this in on my mobile has about exhausted my reserves.

This task has been made more difficult by stipulating "Rename files by CMD" and "rename all files by command" as opposed to a , a potential solution for which, has already been posted.

Upvotes: 1

Related Questions