0

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.

Iftek Har
  • 161
  • 3
  • 5
  • 13
  • File name starting characters are not fixed to 16. – Iftek Har May 17 '20 at 17:24
  • The information at https://ss64.com/nt/syntax-substring.html will be helpful in extracting portions of the filename string. – lit May 17 '20 at 19:28
  • Technically you cannot do what your question asks because, `jweofimsdkfmoiwe-8592547472.html.tmp`, and `jweofimsdkfmoiwe-8592547472.html.readme`, would both need to be renamed to `8592547472.html`, and you cannot have two files with the same name in the same level of a directory. – Compo May 18 '20 at 18:10
  • All files have unique names, sorry for that confusion. – Iftek Har May 18 '20 at 19:36

2 Answers2

1

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 /?
Mofi
  • 46,139
  • 17
  • 80
  • 143
1

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.

Compo
  • 36,585
  • 5
  • 27
  • 39
  • It is whoing error "'%__AppDir' is not recognized as an internal or external command, operable program or batch file" – Iftek Har May 18 '20 at 19:39
  • I've checked the code above @IftekHar, assuming I'd made a typo, however, I haven't, please ensure that your pasted version always has `%__AppDir__%` not `%__AppDir__` – Compo May 18 '20 at 20:11
  • Additionally @IftekHar, if you really are 100% positive that your `%PATH%` variable will never be modified, _(deliberately or accidentally)_, to remove `X:\Windows\System32`, _(where `X:` is a placeholder for your `%SystemDrive%`)_, you can remove every single instance of `%__AppDir__%` without it affecting the code. – Compo May 18 '20 at 20:18
  • You @Compo have solved my problem. You are really an expert. Thanks – Iftek Har May 19 '20 at 22:06
  • this code is renaming 1000 files in a minutes and I want to rename millions of files. Is there any way to do it fast? – Iftek Har May 20 '20 at 19:14