将具有多个扩展名的所有文件列入文本文件。
下面的代码工作正常:
dir /s/b *.jpg /s *.png > temp.txt
但是,如果pwd是桌面版,我需要查找特定用户目录及其子目录(例如:C:\ Users \ user_name)中的所有文件,并且应该在我的pwd即桌面中创建文本文件。我尝试了以下代码,但它包含所有存在的文件而不考虑扩展名。
dir C:\Users\<user_name>\ /s/b *.jpg /s *.png > temp.txt
答案 0 :(得分:1)
dir
的开关是“全局”的,因此多次添加开关不会改变任何内容。
dir /s /b C:\Users\<user_name>\*.jpg C:\Users\<user_name>\*.png >temp.txt
或
pushd C:\Users\<user_name>\
dir /s /b *.jpg *.png>%userprofile%\desktop\temp.txt
popd
或更优雅:
( pushd C:\Users\<user_name>\
dir /s /b *.jpg *.png
popd ) >temp.txt
如果您真的想要{* 1}}用于* .jpg且仅/b/s
用于* .png,则必须使用两个/s
命令。
编辑从简单的文本文件中获取扩展名:
dir
pushd C:\Users\<user_name>\
(for /f %%e in (extensions.txt) do (
dir /s /b *%%e
)) >temp.txt
popd
应如下所示:
extensions.txt
如果文本文件看起来不同,则必须相应地调整此代码。
答案 1 :(得分:0)
虽然晚了几年,但我想分享我通常用于同一任务的脚本。它将接收目录和扩展名列表(表示为通配符:*.jpg
)。
就您而言,使用示例是 this_script.bat . *.jpg *.png > files.txt
@echo off
setlocal
rem %1 Directory to list
rem %2 - %9 extensions
if "%2"=="" (
echo Usage %0 ^<directory^> ^<extensions separated by space^>
echo Example: %0 . *.jpg *.jpeg *.png *.bmp *.gif
exit /b 1
)
set EXTENSIONS=%2 %3 %4 %5 %6 %7 %8 %9
pushd %1
rem List files in base directory
call :list_dir %cd%
rem List subdirectories
for /F "delims=" %%D in ('dir /ad /on /b /s') do (
call :list_dir %%D
)
popd
exit /b 0
rem Lists all files in a directory that match the extensions
:list_dir
pushd %1
set DIR=%1
for %%S in (%EXTENSIONS%) do (
echo %DIR%\%%S
)
popd
exit /b 0
如果您需要变量中的列表,只需通过您需要的表达式更改 echo
中的 :list_dir
。