简体   繁体   中英

Batch command to ignore specific files after dir command?

How do I set a batch script to ignore certain files? Like, not filter, but exact files. Like

dir /b *.zip

Results in the file list

file1.zip
file2.zip
file3.zip
file4.zip
file5.zip

And I want to ignore the specific files

file3.zip
file4.zip

And end up with the file list result

file1.zip
file2.zip
file5.zip

How do I do this?

dir /b *.zip|findstr /v /i /g:"filecontainingexcludednamesonetoaline.txt"

should handle this task quite adequately.


On more information provided (best to provide relevant information at the start to prevent an endless revision cycle)

...
dir /b *.zip|findstr /x /v /i /g:"~f0"
....
goto :eof
---- this info at end-of-file. just plain text
---- files-to-exclude-from-listing
---- these last 3 lines are just comments playing no part in the script
exclude me.zip
and_me.zip
dont_show me.zip

Nearly the same as Magoo's answer, but without the extra file, and also with the addition of the /L option to force a literal match:

dir /b *.zip|findstr /vilx /c:"file1.zip" /c:"file2.zip"

This is a bit overkill for this problem, but you could also use my JREN.BAT utility . It is primarily intended for renaming files, but it does have an option to list files instead. The advantage of this utility is it allows you to specify very precise regular expression terms to match, as well as to specify files to exclude.

In this case, I use the /FM and /FX options to specify masks with normal Windows wildcard rules instead of using regular expressions.

jrepl "^" "" /list /fm *.zip /fx "file1.zip|file2.zip"

If you have complicated matching rules, then /RFM and /RFX could be used to specify regular expression masks instead.

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