0

I have files of random file names and extensions in a folder with a another random filename which contains an *.htm extension. I want to rename all the file names of each random file in the folder to match the one *.htm file while while retaining the correct extension for each random file.

Thus when the batch file is run, a file such as "My File Name.HTM" would be detected and the following files would be renamed to:

  • "test.doc" Renamed To "My File Name.doc"

  • "anyfile.jpg" Renamed To "My File Name.jpg"

  • "text.txt" Renamed To "My File Name.txt"

I have tried

Rename "*.*" "*.htm"

with no success. The *.HTM file could be any unknown filename, but will always contain the HTM extension, and it will always be the only file with an HTM extension in the folder. I'll then run the batch file from a command line.

Martin Brandl
  • 56,134
  • 13
  • 133
  • 172

2 Answers2

1

A concise batch file:

for %%f in (*.htm) do set name=%%~nf
ren * "%name%.*"

Same caveats as n01d's answer. Also, don't put your batch file in the same directory.

Edit:

Here is a sample session showing that the batch file renames all the fiiles to the name of the .htm file:

D:\tmp\kktmp>dir
 Volume in drive D has no label.
 Volume Serial Number is 4EDE-41E1

 Directory of D:\tmp\kktmp

14/07/2016  12:06    <DIR>          .
14/07/2016  12:06    <DIR>          ..
14/07/2016  12:05                 0 Author Name - Book Title - The Billionaires Revenge.htm
14/07/2016  12:06                 0 Book Title - The Billiona - Author Name.azw3
14/07/2016  12:06                 0 Book Title - The Billiona - Author Name.epub
14/07/2016  12:06                 0 Book Title - The Billiona - Author Name.mobi
14/07/2016  12:06                 0 Book Title - The Billiona - Author Name.pdf
               5 File(s)              0 bytes
               2 Dir(s)  52,725,227,520 bytes free

D:\tmp\kktmp>type ..\t.bat
for %%f in (*.htm) do set name=%%~nf
ren * "%name%.*"

D:\tmp\kktmp>..\t.bat

D:\tmp\kktmp>for %f in (*.htm) do set name=%~nf

D:\tmp\kktmp>set name=Author Name - Book Title - The Billionaires Revenge

D:\tmp\kktmp>ren * "Author Name - Book Title - The Billionaires Revenge.*"

D:\tmp\kktmp>dir
 Volume in drive D has no label.
 Volume Serial Number is 4EDE-41E1

 Directory of D:\tmp\kktmp

14/07/2016  12:37    <DIR>          .
14/07/2016  12:37    <DIR>          ..
14/07/2016  12:06                 0 Author Name - Book Title - The Billionaires Revenge.azw3
14/07/2016  12:06                 0 Author Name - Book Title - The Billionaires Revenge.epub
14/07/2016  12:05                 0 Author Name - Book Title - The Billionaires Revenge.htm
14/07/2016  12:06                 0 Author Name - Book Title - The Billionaires Revenge.mobi
14/07/2016  12:06                 0 Author Name - Book Title - The Billionaires Revenge.pdf
               5 File(s)              0 bytes
               2 Dir(s)  52,724,703,232 bytes free
Klitos Kyriacou
  • 10,634
  • 2
  • 38
  • 70
  • An elegant solution in concept. But does the opposite of what I need. This solution turns the HTM file into the file name of whatever else is in the folder, I need to do the reverse and change all the other files into the same file name as the HTM file. Batch file is not in the same folder, and only one file exists with the same extension. Any chance of getting this to rename all the other files instead of the htm file? Thanks in advance. Phaedrus Thanks on – Phaedrus T. Wolfe Jul 13 '16 at 16:22
  • Just to explain how it works: the first line sets the variable 'name' to the name of the .htm file. The second line renames all files to have the name 'name' and their original extension. It 'renames' the .htm file as well, but that's totally benign as it renames it with exactly the same name. – Klitos Kyriacou Jul 13 '16 at 17:59
  • It is strange. Your suggestion makes total sense, but alas, it is not working in my bat file. I have no logical explanation. Windows 8.1 Thanks for helping – Phaedrus T. Wolfe Jul 13 '16 at 20:10
  • I don't know if this helps, but here are the exact file names I am working with: Author Name - Book Title - The Billionaires Revenge.htm Book Title - The Billiona - Author Name.azw3 Book Title - The Billiona - Author Name.epub Book Title - The Billiona - Author Name.mobi Book Title - The Billiona - Author Name.pdf When I try this solution, the htm file is renamed to match the other files. What I am trying to achieve is for all the other files to match the htm file. I hope this helps. Thanks – Phaedrus T. Wolfe Jul 13 '16 at 20:57
  • Can you make sure you haven't accidentally put a space between * and .htm in 'for %%f in (*.htm)' ? – Klitos Kyriacou Jul 13 '16 at 23:33
  • Confirming that no space exists in the "*.htm" area. Thanks. – Phaedrus T. Wolfe Jul 14 '16 at 07:59
  • See my edit. The batch file renames all the files to the name of the .htm file. If this is not what you are getting, check that your batch file is exactly as shown when I typed `type ..\t.bat` above. If you are still not getting these results, post a session log like mine. – Klitos Kyriacou Jul 14 '16 at 12:51
  • Now I am extremely confused, but extremely happy! I ran the same bat file one more time today, and it works perfectly now! No changes to it whatsoever. The only difference is a Microsoft update on the machine with framework 3.5 and 4.0 updates -- and a reboot.Incredible. I am the happiest man in this city today. Thanks for your assistance and patience. It works perfectly. – Phaedrus T. Wolfe Jul 15 '16 at 12:15
  • Crazy. Used it all morning, then this afternoon it stopped working and went back to the old way of reversing the file names. Groan, If I can get it working all the time, this will save me about 15 hours time per month, Looking forward to figuring this one out. Thanks for your help. – Phaedrus T. Wolfe Jul 15 '16 at 17:13
  • Do you by any chance sometimes have a file named "Book Title - The Billiona - Author Name.htm" ? I.e. the same name as all the other files that you want to rename, but with a .htm extension? – Klitos Kyriacou Jul 15 '16 at 19:57
  • No, there is always only one htm file in the folder. Never is there more than one. – Phaedrus T. Wolfe Jul 15 '16 at 22:05
  • Do you have any files that have an extension with more than three characters but that begins with ".htm"? Such as .html or .htm123? – Klitos Kyriacou Jul 15 '16 at 22:13
  • The reason I asked is that files with long names have in addition an equivalent 8.3 name (which you can see if you type DIR /X). So if you have a file named x.html123abc it will match the FOR %f in (*.htm) command because its 8.3 filename will have a .htm extension. And because it matches, its name will be assigned to the variable 'name' which is then used to rename all the other files including the "good" .htm file. – Klitos Kyriacou Jul 15 '16 at 22:22
  • If that is the case, or even a rare possibility, then replace the first line with `for %%f in (*.htm) do if "%%~xf"==".htm" set name=%%~nf` – Klitos Kyriacou Jul 15 '16 at 22:24
  • That was exactly the solution. In some of the folders a file sometimes exists with an extension of *.htmlz which is what was causing the problem. This last line replacing the first works perfectly. In total, the batch file now saves me 15 hours per month in time, You can imagine how pleased I am. Thanks. – Phaedrus T. Wolfe Jul 16 '16 at 14:34
0

Something like this:

$files = Get-ChildItem -Path D:\TEST_123
$htmBaseName = $files | where {$_.Extension -eq '.htm'} | select -ExpandProperty BaseName

foreach ($f in $files) {
    if ($f.Extension -ne '.htm') {
        Rename-Item -Path $f.FullName -NewName ($htmFileName + $f.Extension)
    }
}

But be careful: if there's more than one file with the same ext. you'll get an error Cannot create a file when that file already exists.

n01d
  • 1,047
  • 8
  • 22