Page 1 of 1

list files and sub directories in an ordered list?

Posted: 27 Nov 2017 06:34
by tlm2408
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.

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

Posted: 27 Nov 2017 06:53
by tlm2408
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
)

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

Posted: 27 Nov 2017 13:45
by aGerman

Code: Select all

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

Steffen

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

Posted: 27 Nov 2017 14:05
by tlm2408
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
)
)

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

Posted: 27 Nov 2017 14:13
by aGerman
Is there any reason why you need variable dir? Toggling between enabled and disabled delayed expansion is slow.

Steffen

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

Posted: 27 Nov 2017 23:10
by tlm2408
Hi Steffen,
You're right. I just tried what you mentioned and it is quicker. Thank you very much.

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

Posted: 28 Nov 2017 03:02
by tlm2408
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.

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

Posted: 28 Nov 2017 05:03
by aGerman
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

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

Posted: 28 Nov 2017 06:04
by tlm2408
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.

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

Posted: 28 Nov 2017 08:20
by aGerman
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

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

Posted: 28 Nov 2017 13:51
by tlm2408
Hi Steffen, That works much better. Thank you very much.