0

我想做一些类似的事情:

for %%i not in (*.xml *.doc) do ....

显然,这是行不通的。有没有办法可以在批处理文件中做类似的事情?

4

2 回答 2

2
for /f "eol=: delims=" %%i in ('dir /b /a-d *^|findstr /live ".bat .txt"') do ...

附录

如果目录列表变得很大,此解决方案将以非线性方式减慢速度。这不太可能是单个目录的问题,但如果使用 DIR /S 选项,则很容易成为问题。通过使用临时文件,可以将解决方案恢复为相对于列表大小具有线性响应时间。

set temp1="%temp%\dirExclude1_%random%.txt"
set temp2="%temp%\dirExclude2_%random%.txt"
>%temp1% dir /s /b /a-d *
>%temp2% findstr /live ".bat .txt" %temp1%
for /f "usebackq eol=: delims=" %%i in (%temp2%) do ...
del %temp1%
del %temp2%

这是 Windows 管道的一般限制。对于大量数据,它们变得非常低效。在处理大量数据时,使用临时文件而不是管道总是要快得多。

于 2012-04-12T02:05:01.407 回答
1
@echo off
setlocal EnableDelayedExpansion
rem Build a list of files to exclude
set exclude=
for %%i in (*.xml *.doc) do set "exclude=!exclude!%%i*"
rem Process all but excluded files
for %%i in (*.*) do (
   if "!exclude!" equ "!exclude:%%i*=!" echo Process this one: %%i
)
于 2012-04-12T00:05:20.850 回答