删除与正则表达式匹配的文件
在 Windows 上从与 Perl 中的某些正则表达式匹配的目录中删除所有文件的最短和最好的方法。
我的示例:从目录中删除所有 *.txt
文件,但保留 tmp.txt
?视窗。
Shortest and nicest way to delete all files from directory that match certain regex in Perl on Windows.
My example: delete all *.txt
files from directory, but leave tmp.txt
? Windows.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
虽然并不完美,但也许一些冗长的内容会有所帮助。
While not perfect, maybe something long winded will help.
如果目录中有很多文件(10,000 个或更多),上述所有解决方案都可能存在问题,因为它们会同时读取整个文件列表。安全的做法是遍历目录,而不是一次读取全部内容。 “readdir”可以仅返回标量上下文中的下一个条目。我建议使用 while 循环更新 @hpvac 的答案,一次遍历目录一个条目。
(如果您确定永远不会有大量文件,则上述解决方案是可行的。)
All of the solutions above are potentially problematic if there a lot files in the directory (10,000 or more), because they all read in the entire list of files at once. The safe thing to do is iterate through the directory, rather than read it all at once. "readdir" can return just the next entry in scalar context. I recommend updating @hpvac's answer with a while loop that goes through the directory one entry at a time.
( If you are certain there will never be a huge number of files, the solutions above are workable. )
有一个 perl 实用程序:
prename
(在大多数 Linux 上为rename
),只需将其与 rm 结合使用:输入答案后,我看到您要求使用 Windows 解决方案。我认为你最好的选择是下载(Perl 源代码)作为名称,并对其进行一些编辑,以便它可以在 Windows 上运行。
There is a perl utility:
prename
(rename
on most linuxes), just use that in combination with rm:After typing the answer I saw you asked for a Windows solution. I think your best bet is downloading the (Perl source) for prename, and editting that a little, so that it works with Windows.