扫描文件夹内容时排除某些子文件夹
Exclude certain subfolders when scanning the contents of a folder
我有一个批处理文件,用于使用 for 循环扫描 Windows 7 框中的 c:\ 文件夹(及其各自的子文件夹)。我在排除某些文件夹时遇到问题,例如用户、Windows、LocalAppData 等
我需要扫描 C:\ 中的文件夹以找到特定的命名文件夹(即 "myFolder")而不扫描上述文件夹,以便增加在C: 文件夹.
代码:
@echo off
for /d /r "c:\" %%a in (*) do (
findstr /v "Users" "Windows"
)
使用 dir
command to return the paths of all wanted directories myFolder
, filter them by the findstr
command, then capture the result by a for /F
loop,像这样:
for /F "delims=" %%D in ('dir /B /S /A:D "C:\myFolder" ^| findstr /I /V /C:"\Users\" /C:"\Windows\"') do echo/%%D
当然你可以指定更多排除项。
如果您想将例外情况放在文本文件中 C:\exclude.txt
:
Users Windows
改变方法如下:
for /F "delims=" %%D in ('dir /B /S /A:D "C:\myFolder" ^| findstr /I /V /L /G:"C:\exclude.txt"') do echo/%%D