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
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...
"I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008
"the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010
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]$
Code:printf("%c%c%c%c%c%c%c",0x68,0x68^0xd,0x68|0x4,0x68|0x4,0x68|0xf,0x68^0x49,0x68^0x62);
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
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
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
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.