0

I'm trying to write a script that will go through a director structure like:

school/admission/english->index.html

It should rename index.html to english.html which is it's parent folder and move it a level up so that the result would be like

school/admission/english.html

This same process should get repeated for all the files by the name index.html in that folder structure. Any help about how to approach this problem would be appreciated.

To find all the index.html files I have:

   @echo off
for /r %%i in (index.html) do echo %%~ti %%~zi %%i

To move .html files one level up:

for /r %x in (*.html) do move "%x" "%x"/../..
Vinit
  • 1,815
  • 17
  • 38

1 Answers1

2

Here is a fairly simple native solution that can be run from the command line

for /r %F in (index.html) do @for /f "eol=: delims=" %A in ("%F\..") do @if exist "%F" move "%F" "%F\..\..\%~nA%~xF"


EDIT in response to additional question in comment

I think it best to use a batch script if you want to only process leaf nodes of the folder hierarchy:

@echo off
for /r %%F in (index.html) do (
  if exist "%%F" (
    set "subFolder="
    for /d %%A in ("%%~dpF\*") do set subFolder=1
    if not defined subFolder for /f "eol=: delims=" %%A in ("%%F\..") do move "%%F" "%%F\..\..\%%~nA%%~xF"
  )
)
dbenham
  • 127,446
  • 28
  • 251
  • 390
  • This one works well...Can we add a condition to this so that it only does the process for index.html without sub-folders? I mean only rename and move index.html which does not have any subfolder in it (like only the last ones in the folder hierarchy) ? – Vinit Sep 20 '12 at 16:40