Batch to rename index.html files to their subforders' names

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
terryhenderson
Posts: 16
Joined: 22 Jul 2018 04:59

Batch to rename index.html files to their subforders' names

#1 Post by terryhenderson » 22 Jul 2018 05:14

Hi,

I'm working on over 28,000 online tests. Each test is in one subfolder. Each subfolder consists of one index.html file in addition to 3 sub subfolders which contain the test data. I am in a dire need to rename all the index.html files to their subfolders name, so that when publishing the entire tests online, each test can be accessed through its very index.html file name. For example, if the subfolder's name is: TMC-WQ-AOP-T1, its index.html file would be renamed to TMC-WQ-AOP-T1.html instead.

Can I get some precious assistance with this request please?

Many thanks in advance.

T.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Batch to rename index.html files to their subforders' names

#2 Post by aGerman » 22 Jul 2018 07:43

Place that batch file in the parent folder and run it.

Code: Select all

@echo off &setlocal EnableExtensions DisableDelayedExpansion
for /f "delims=" %%i in ('dir /a-d /b /s "index.html"') do (
  for %%j in ("%%~dpi.") do ECHO ren "%%~i" "%%~nxj%%~xi"
)
PAUSE
Note: The code will only display the command lines without doing any renaming operation yet. If you are convinced it would do what you're looking for then remove ECHO and PAUSE from the code in order to perform the renaming if you run the updated code.

Steffen

terryhenderson
Posts: 16
Joined: 22 Jul 2018 04:59

Re: Batch to rename index.html files to their subforders' names

#3 Post by terryhenderson » 22 Jul 2018 09:36

Thank you very much aGerman for your prompt response to my question. In fact, I followed your instructions and placed the batch file in the parent folder and ran it after removing the echo and pause as advised, but nothing has been changed. I attempting placing the batch file down in the subdirectories themselves, but no index.html file has been affected to be renamed.

I may add that tests subdirectories with their index file and other sub subdirectories vary in their location in the parent folder. In other words, they may be in the first subdirectory from the parent directory or the ninth subdirectory from the parent. So I believe the batch should recursively search for the index.html file.


Once again, thank you very much.


T.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Batch to rename index.html files to their subforders' names

#4 Post by aGerman » 22 Jul 2018 10:41

The /S option of DIR means that it searches recursively. I already tested the script and everything worked out for me. By saying "parent folder" I meant the root folder in your folder structure that contains the index.html files (regardless of how many levels deep in the folder structure, because /S searches in all subsequent levels).

Steffen

terryhenderson
Posts: 16
Joined: 22 Jul 2018 04:59

Re: Batch to rename index.html files to their subforders' names

#5 Post by terryhenderson » 22 Jul 2018 14:42

Thank you very much Steffen for your genius code; it did run soothly and all the index.html files were renamed after their folders as exactly as desired.
Pardon me for a silly mistake that I made before running your code which made it not to run at first time.
Please accept my deep gratitude.

T.

Post Reply