简体   繁体   中英

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

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!
)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM