Windows scripting: list files not matching a pattern

感情迁移 提交于 2019-12-04 01:46:57

问题


In Windows 7 command prompt, I´d like to list all files of a folder which name does not start with abc. I have tried:

forfiles /P C:\myFolder\ /M ^[abc]* /S /C "CMD /C echo @file"

Where is my error?

Many thanks.


回答1:


Looking at forfiles /?:

/M    searchmask    Searches files according to a searchmask.
                    The default searchmask is '*' .

which strongly suggests forfiles doesn't support regular expressions, just normal Cmd/Windows wildcards.

On Windows 7 this can easily be achieved in PowerShell:

dir c:\myFolder | ?{ -not($_.Name -match '^abc') } | select Name

(That performs a case-insensitive regular expression match, which doesn't matter in the case of Windows filenames.)

NB. Assuming you want files not starting ABC, which isn't what your (attempted) regular expression says (any filename starting something that isn't a, b or c).




回答2:


Where is my error?

Your error is thinking that the forfiles command would support regular expressions.

It does not. It supports file name matching with * and ?.




回答3:


An alternative, in case of using a xcopy command instead of echo is using the option /exclude. For instance:

forfiles /P C:\myFolder\ /M ^[abc]* /S /C "CMD /C xcopy @path %myDestinationFolder% /exclude:abc*"

Also, if you´re using PowerShell, another option is the operator -match.



来源:https://stackoverflow.com/questions/14440692/windows-scripting-list-files-not-matching-a-pattern

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!