开发者

How do I recursively list all files of type *.mp3 in a Windows .bat file?

开发者 https://www.devze.com 2023-01-01 14:32 出处:网络
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 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开发者_StackOverflow中文版 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!
)


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 /?.


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
0

精彩评论

暂无评论...
验证码 换一张
取 消