如何在 Windows .bat 文件中递归列出 *.mp3 类型的所有文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2951063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I recursively list all files of type *.mp3 in a Windows .bat file?
提问by clamp
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.
我想递归列出所有mp3
以给定目录结尾的文件的绝对路径,该目录应该作为相对目录给出。
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?
然后,我还想从文件中删除目录,并且我已经阅读了for
-scope中的变量必须包含在!
s 中。那正确吗?
My current code looks like this:
我当前的代码如下所示:
for /r %%x in (*.mp3) do (
set di=%%x
echo directory !di!
C:\bla.exe %%x !di!
)
回答by Matthew Whited
Use the command DIR
:
使用命令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 /?
.
上面的命令将搜索当前路径及其所有子路径。要获取有关如何使用此命令的更多信息,请打开命令窗口并键入DIR /?
。
回答by yoyo
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".
下面的批处理文件演示了如何从for
循环中的变量中提取文件名的元素。将其另存为 file listfiles.bat
,然后运行“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.
您也可以运行“for /?” 在 cmd shell 中获取有关该for
命令的更多信息。
@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