how to catch the last level name folder ?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
budhax
Posts: 63
Joined: 09 Oct 2006 12:25

how to catch the last level name folder ?

#1 Post by budhax » 26 Jun 2007 10:36

Hello,
Last level folder= folder that have no sub folder.

Here is the script putting links of .URL files (web page shortcut)
in a links.html file,
from the current folder and sub folders.
Url2Htm.bat

Code: Select all

@ECHO OFF
SET z=%~PD0Links.hTM
TYPE NUL>"%z%"
(FOR /F "Tokens=1,2,* Delims=:=" %%a IN ('FINDSTR /s /i /b /c:"URL=" "*.url"') DO (
        ECHO.^&bull; ^<a href="%%c"^>%%~na^</a^> ^<br^>
))>Links.html
Pause



I would add the "last level name folder" before each link.
How to catch the last level name folder in the FOR loop?
Thanks.

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

#2 Post by DosItHelp » 03 Jul 2007 23:56

budhax,

Dependent on where your .URL file is there might be multiple last level folders under it, not sure if I understand correctly.

Having delayed variable expansion turned on the following code lists all last level folders under folder f:

Code: Select all

SETLOCAL ENABLEDELAYEDEXPANSION 
...
for /f "delims=" %%a in ('"dir /ad /s /b "%f%"|sort /r"') do (
    if "!merker!" NEQ "%%a\" echo."+%%a"
    set "merker=%%~dpa"
)


DOS IT HELP?

budhax
Posts: 63
Joined: 09 Oct 2006 12:25

#3 Post by budhax » 05 Jul 2007 10:43

Sorry DosItHelp,
In fact, my question was "wrong".
I just would create an HTML file with links based on URL files in the tree folder.
Where the BAT is in the root of this tree.

AND the parent folder name containing a URL file, is added just befor the HTML link.
I found a solution:

Code: Select all

SET z=%~PD0LiNKS.hTM
TYPE NUL>"%z%"
ECHO.^<h1^>Fichiers .URL vers liens HTM^</h1^>>>"%z%"
(
   FOR /F "Tokens=*" %%i IN ('DIR /ad /s /b') DO (
      CD "%%i"
      FOR /F "Tokens=1,2,* Delims=:=" %%a IN ('FINDSTR /i /b /c:"URL=" "*.url"') DO (
         ECHO.%%~ni ^&bull; ^<a href="%%c"^>%%~na^</a^> ^<br^>
      )
      ECHO.^<br/^>
   )
)>>"%z%"


Put this script in the root tree folder structure containing URL files (Web page short cut), and launch it.

The next step to improve this srcipt is:
how to avoid empty HTML line (<br>) for folder that has no URL file?

I tested your script, it works fine (of course ;)) !!

Thanks a lot.

A SPECIAL THANK FOR ALL DOS EXPERTS ON THIS FORUM

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

#4 Post by DosItHelp » 06 Jul 2007 00:01

budax,

Interesting script you have there :D. It creates some pretty index.
"if exist" should avoid the empty lines. How about this:

Code: Select all

@echo off
SET z=%~PD0LiNKS.hTM
TYPE NUL>"%z%"
ECHO.^<h1^>Fichiers .URL vers liens HTM^</h1^>>>"%z%"
pushd .
(
   FOR /F "Tokens=*" %%i IN ('DIR /ad /s /b') DO (
      if exist "%%i\*.url" (
         CD "%%i"
         ECHO.^<a href="file://%%i"^>%%i^</a^> ^<br^>
         FOR /F "Tokens=1,2,* Delims=:=" %%a IN ('FINDSTR /i /b /c:"URL=" "*.url"') DO (
            ECHO.%%~ni ^&bull; ^<a href="%%c"^>%%~na^</a^> ^<br^>
         )
         ECHO.^<br/^>
      )
   )
)>>"%z%"
popd


or similar:

Code: Select all

@echo off
SET z=%~PD0LiNKS.hTM
TYPE NUL>"%z%"
ECHO.^<h1^>Fichiers .URL vers liens HTM^</h1^>>>"%z%"
(
   FOR /F "Tokens=*" %%i IN ('DIR /ad /s /b') DO (
      if exist "%%i\*.url" (
         ECHO.^<a href="file://%%i"^>%%i^</a^> ^<br^>
         FOR /F "Tokens=2,3,* Delims=:=" %%a IN ('FINDSTR /i /b /c:"URL=" "%%i\*.url"') DO (
            ECHO.%%~ni ^&bull; ^<a href="%%c"^>%%~na^</a^> ^<br^>
         )
         ECHO.^<br/^>
      )
   )
)>>"%z%"

Post Reply