i need to delete several files at one without knowing how many are there.
but they all start with :f ile
ex:
file001
file007
file010
etc
can i delete them all at once using ansi C? maybe with remove? somethig like remove file*?
tks
Printable View
i need to delete several files at one without knowing how many are there.
but they all start with :f ile
ex:
file001
file007
file010
etc
can i delete them all at once using ansi C? maybe with remove? somethig like remove file*?
tks
You could do this:
I don't think there is any other standard C way of doing it.Code:system( "rm -f file*" ); // on UNIX
// or
system( "del -f -q file*" ); // on Windows
There might be an OS API function that can do it, and it would be much more efficient than using system().
Otherwise, find each file yourself and call remove() on each one you find...
how about this one
OP:Code:[c_d@localhost c scratchpad]$ cat temp4.c
#include<stdio.h>
int main()
{
int status=0;
int i=1;
char buf[10];
while(status != -1)
{
sprintf(buf,"file%d",i++);
status=remove(buf);
}
return 0;
}
you get the basic idea right...:)Code:[c_d@localhost c scratchpad]$ gcc temp4.c
[c_d@localhost c scratchpad]$ touch file1 file2 file3 file4
[c_d@localhost c scratchpad]$ ls -1 | grep file
file1
file2
file3
file4
[c_d@localhost c scratchpad]$ ./a.out
[c_d@localhost c scratchpad]$ ls -1 | grep file
[c_d@localhost c scratchpad]$
pretty much as i am doing. just hoping to find a better way. tks anyway
ANSI and many operating systems in themselves (e.g. Linux/Unix) does not know how to perform multiple deletes. I'm not sure if Windows functions know how to delete multiple files or not - I think DOS and CP/M did know.
--
Mats
Perhaps the simplest in the way of how much code to write. But it certainly isn't the simplest for the machine to perform, since it involves starting a whole new process and expand the * into the valid filenames. Doing the same work within the application will be much lighter weight in terms of actual work in the machine.
[I'm not saying you are wrong - I'm just clarifying which form of "simplest" we are describing - as it can have several meanings].
--
Mats