我希望能够提供一个使用正则表达式查找文件名中包含数字或特定数字范围的批处理文件示例。请问是否有这样的方法?能否提供一个简单的示例呢?
其中一部分功劳归于Y.A.P的回答。
以下代码将获取目录中任何文件名至少包含一个数字的所有文件:
@Echo Off
CD C:\Folder\To\Process
Dir /B>Dir.temp
FindStr /R "[0-9]" "Dir.temp">FindStr.temp
Del Dir.temp
For /F "tokens=*" %%a In (FindStr.temp) Do Call :WorkIt "%%a"
Del FindStr.temp
Exit /B
:WorkIt
:: Insert code here. Use %1 to get the file name with quotes. For example:
Echo Processing %1...
Exit /B
FindStr
行包含正则表达式。命令行版本的正则表达式是有限制的。您需要查找哪个确切的范围并且文件名是什么格式?
例如,如果您知道所有文件都有3位数字,您可以使用表达式[0-2][0-9][0-9]
将其限制为从000到299的所有项。
dir /B
的输出导入到 findstr
中来摆脱中间文件 Dir.temp
。像这样 dir /B | findstr /R "[0-9]" > FindStr.temp
。否则,非常好的帖子,对我帮助很大。干杯! - Jagger * = match all
并且 ? = 匹配 1
所以:
mytune*.mp?
mytune01.mp3, mytune01.mpg, mytune-the-best.mpe
http://powershell.com/cs/blogs/ebook/archive/2009/03/30/chapter-13-text-and-regular-expressions.aspx
我建议先花一个小时左右学习基础知识。在findstr中有一个功能。 请查看这里
findstr
搜索它。 - Hand-E-Food