如何使用通配符删除C中的多个文件? [英] How to remove multiple files in C using wildcards?
问题描述
C中是否可以使用 *
(通配符)删除(使用 remove()
)多个文件?我有一组全部以索引开头的文件.例如: Index1.txt
, Index-39.txt
等.它们都以Index开头,但我不知道后面跟着什么.同一目录中还有其他文件,因此无法删除所有文件.
Is there any way in C to remove (using remove()
) multiple files using a *
(wildcards)?
I have a set of files that all start with Index. For example: Index1.txt
, Index-39.txt
etc.
They all start with Index but I don't know what text follows. There are also other files in the same directory so deleting all files won't work.
我知道您可以读取目录,循环访问每个文件名,读取前5个字符,进行比较,如果合适,则将其删除,但是,有没有更简单的方法(这是我目前正在做的事情)吗?
I know you can read the directory, iterate each file name, read the the first 5 chars, compare and if it fits then delete, but, is there an easier way (this is what I currently do by the way)?
这是标准的C语言,因为代码可以在Linux和Windows上运行.
推荐答案
如您所指出的,您可以使用diropen,dirread,dirclose来访问目录内容,自己的功能(或将通配符转换为regex并使用一个正则表达式库)匹配,然后取消链接以删除.
As you point out you could use diropen, dirread, dirclose to access the directory contents, a function of your own (or transform the wildcards into a regex and use a regex library) to match, and unlink to delete.
没有标准的方法可以更轻松地做到这一点.可能有图书馆,但它们不会比您正在做的事更有效率.通常,文件查找功能采用回调,您可以在其中提供代码的匹配和操作部分.您只需要保存循环即可.
There isn't a standard way to do this easier. There are likely to be libraries, but they won't be more efficient than what you're doing. Typically a file finding function takes a callback where you provide the matching and action part of the code. All you'd be saving is the loop.
这篇关于如何使用通配符删除C中的多个文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!