list files and sub directories in an ordered list?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
tlm2408
Posts: 31
Joined: 01 Aug 2017 01:19

list files and sub directories in an ordered list?

#1 Post by tlm2408 » 27 Nov 2017 06:34

Hello,

I'm trying to list all the mp3 and flac files in the directory the batch fles is in, but also any sub directories only one level deep. I need these to be put in to two html lists.

So for example I'm trying to get:

Code: Select all

<ul>
<li>Folder 1</li>
<li>Folder 2</li>
<li>Folder 3</li>
</ul>

<ol>
<li>song name 1.mp3</li>
<li>song name 2.mp3</li>
<li>song name 3.mp3</li>
<li>song name 21.flac</li>
<li>song name 32.flac</li>
<ol>
There could be any number of sub directories and files.
Hopefully this is easy to do and hopfully I've explained myself well enough.
Thank you for any help.

tlm2408
Posts: 31
Joined: 01 Aug 2017 01:19

Re: list files and sub directories in an ordered list?

#2 Post by tlm2408 » 27 Nov 2017 06:53

I've worked most of it out, but I can onlt get it to show the last sub directory. It doesn't show any of the others. This is what's giving me trouble now:

Code: Select all

>>"%file%" (
for /d %%d in (*) do set "dir=%%~nxd"
    setlocal EnableDelayedExpansion
<nul set /p =^<li^>^<a href="!dir!/index.html"^>!dir!^</a^>^</li^>
endlocal
)

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

Re: list files and sub directories in an ordered list?

#3 Post by aGerman » 27 Nov 2017 13:45

Code: Select all

>>"%file%" (
  for /d %%d in (*) do <nul set /p =^<li^>^<a href="%%~nxd.index.html"^>%%~nxd^</a^>^</li^>
)
Maybe?

Steffen

tlm2408
Posts: 31
Joined: 01 Aug 2017 01:19

Re: list files and sub directories in an ordered list?

#4 Post by tlm2408 » 27 Nov 2017 14:05

I got it working I forgot the brackets after the do. So it should have been:

Code: Select all

>>"%file%" (
for /d %%d in (*) do (
set "dir=%%~nxd"
    setlocal EnableDelayedExpansion
<nul set /p =^<li^>^<a href="!dir!/index.html"^>!dir!^</a^>^</li^>
endlocal
)
)

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

Re: list files and sub directories in an ordered list?

#5 Post by aGerman » 27 Nov 2017 14:13

Is there any reason why you need variable dir? Toggling between enabled and disabled delayed expansion is slow.

Steffen

tlm2408
Posts: 31
Joined: 01 Aug 2017 01:19

Re: list files and sub directories in an ordered list?

#6 Post by tlm2408 » 27 Nov 2017 23:10

Hi Steffen,
You're right. I just tried what you mentioned and it is quicker. Thank you very much.

tlm2408
Posts: 31
Joined: 01 Aug 2017 01:19

Re: list files and sub directories in an ordered list?

#7 Post by tlm2408 » 28 Nov 2017 03:02

I have another problem now. I have a batch file to copy my batch file to every sub directories (1700+). How would I get them all to run from one batch file.

I have:

Code: Select all

for /r "C:/Audio/" %%i in (.) do @copy "C:\Media\Audio\index.bat" "%%i"
To copy the batch file to the sub dirs. This works fine and is very quick.

I have:

Code: Select all

@echo off
for /r /d %%f in (*) do pushd "%%f" & Call "index.bat" & popd
To call the batch files in all the sub directories. It takes a long long time to run. Is there a way to make this quicker?
Hopefully it's something simple I'm doing wrong. I'm not real good at batch files, but I am trying to learn more.

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

Re: list files and sub directories in an ordered list?

#8 Post by aGerman » 28 Nov 2017 05:03

The batch files are called one by one. That means only if the script before was completed the next will be run. Of course it takes some time. But to be honest I don't recomment to run them asynchronously using START because your PC could freeze if thousands of processes are created. However you could try it. Depending how long it takes to process one script you should include a delay in your loop.
Try something like that:

Code: Select all

for /r /d %%f in (*) do (
  start "" /d "%%f" /b cmd /c "%%f\index.bat"
  >nul pathping 127.0.0.1 -n -q 1 -p 150
)
PATHPING can make things even worse if the delay (150ms in this case) isn't long enough.

Steffen

tlm2408
Posts: 31
Joined: 01 Aug 2017 01:19

Re: list files and sub directories in an ordered list?

#9 Post by tlm2408 » 28 Nov 2017 06:04

That works well and helped me find yet another problem. It won't list files and folders with & in it. I'm going to look at regex to allow all common filename safe characters. Thank you again for your time and help.

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

Re: list files and sub directories in an ordered list?

#10 Post by aGerman » 28 Nov 2017 08:20

Try to change "%%i" to "%%~i" and "%%f" to "%%~f" after the DO keyword in your loops. It's likely that the FOR variables already contain surrounding quotation marks in that case.

Steffen

tlm2408
Posts: 31
Joined: 01 Aug 2017 01:19

Re: list files and sub directories in an ordered list?

#11 Post by tlm2408 » 28 Nov 2017 13:51

Hi Steffen, That works much better. Thank you very much.

Post Reply