How do I recursively list all files of type *.mp3 in a Windows .bat file?
Solution 1
Use the command DIR
:
dir /s/b *.mp3
The above command will search the current path and all of its children. To get more information on how to use this command, open a command window and type DIR /?
.
Solution 2
The batch file below demonstrates how to extract elements of a filename from the variable in a for
loop. Save this as file listfiles.bat
, and run "listfiles some\folder *.mp3".
I set up the file finding as a subroutine in the batch file so you can insert it into your own scripts.
You can also run "for /?" in a cmd shell for more information on the for
command.
@echo off
setlocal enableextensions enabledelayedexpansion
call :find-files %1 %2
echo PATHS: %PATHS%
echo NAMES: %NAMES%
goto :eof
:find-files
set PATHS=
set NAMES=
for /r "%~1" %%P in ("%~2") do (
set PATHS=!PATHS! "%%~fP"
set NAMES=!NAMES! "%%~nP%%~xP"
)
goto :eof
Comments
-
clamp almost 2 years
I want to recursively list the absolute path to all files that end with
mp3
from a given directory which should be given as relative directory.I would then like to strip also the directory from the file and I have read that variables which are in a
for
-scope must be enclosed in!
s. Is that right?My current code looks like this:
for /r %%x in (*.mp3) do ( set di=%%x echo directory !di! C:\bla.exe %%x !di! )
-
Alexander Gelbukh over 6 yearsThis works with explicit directory, too:
dir /s/b "c:\My music\*.mp3"
.