| View previous topic :: View next topic |
| Author |
Message |
matoos80@hotmail.com Guest
|
Posted: Tue Jun 07, 2005 10:55 pm Post subject: What is a good way to use c++ to delete all files in a direc |
|
|
I'm developing a command-line program that needs to clear its temporary
workspace and this involves deleting all previous files. Say the
current working directory has all the temp-files ... I'm currently
using:
system("del *.dat");
but I would like to avoid the error message that occurs if there are no
files of that type to delete. My problem is that I do not have advanced
knowledge of the file names. So i cannot loop over a list of filename
strings. Does anyone know of a better way of deleting all files in a
directory without using the "del" system function like in DOS?
Thanks,
Matoos
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
77123036@163.com Guest
|
Posted: Wed Jun 08, 2005 7:54 am Post subject: Re: What is a good way to use c++ to delete all files in a d |
|
|
if you have install "cygwin", you can use:
system("rm *.dat");
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
russelyang@gmail.com Guest
|
Posted: Wed Jun 08, 2005 7:55 am Post subject: Re: What is a good way to use c++ to delete all files in a d |
|
|
you can try system("del *.dat 1>NUL 2>NUL")
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Allan W Guest
|
Posted: Wed Jun 08, 2005 7:56 am Post subject: Re: What is a good way to use c++ to delete all files in a d |
|
|
[email]matoos80 (AT) hotmail (DOT) com[/email] wrote:
| Quote: | I'm currently using:
system("del *.dat");
but I would like to avoid the error message that occurs if there are no
files of that type to delete.
|
You're already non-portable, and that doesn't seem to bother you.
So the best answer might also be system-dependant.
Not sure what system you're on, but this might work on Windows
(untested):
system("del *.dat>nul");
Otherwise, you can just make sure that there is always at least one
file of that type to delete!
system("echo HELLO>killme.dat");
system("del *.dat");
The more-portable (but still not completely portable) way would be to
use functions that look up the names of files in the current directory.
Look at your system documentation for the names of functions and even
code samples.
One gotcha if you go this route: on some systems, if you look up the
names of the files and delete them as you go, you won't get them all.
Assume files are named FILE1 through FILE9 inclusive. You ask "what's
the first file name that matches?" and you get FILE1. Now you delete
FILE1. Next you ask "what's the second file name that matches?" But
now the first file name that matches is FILE2, and the second name
is FILE3. So you delete FILE3, but leave FILE2 alone.
The answer to this problem (which doesn't happen on all OS's) is to
first make a list of ALL files to delete, and THEN delete them.
Good luck.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Catalin Marinas Guest
|
Posted: Wed Jun 08, 2005 12:21 pm Post subject: Re: What is a good way to use c++ to delete all files in a |
|
|
[email]matoos80 (AT) hotmail (DOT) com[/email] wrote:
| Quote: | I'm developing a command-line program that needs to clear its temporary
workspace and this involves deleting all previous files. Say the
current working directory has all the temp-files ... I'm currently
using:
system("del *.dat");
|
Have a look at the boost fs library. Maybe something like this:
#include <boost/filesystem/operations.hpp>
namespace fs = boost::filesystem;
....
fs::remove_all(path)
--
Catalin
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
msalters Guest
|
Posted: Wed Jun 08, 2005 12:23 pm Post subject: Re: What is a good way to use c++ to delete all files in a d |
|
|
[email]matoos80 (AT) hotmail (DOT) com[/email] schreef:
| Quote: | I'm developing a command-line program that needs to clear its temporary
workspace and this involves deleting all previous files. ...
Does anyone know of a better way of deleting all files in a
directory without using the "del" system function like in DOS?
|
Have a look at boost(www.boost.org) FileSystem library.
HTH,
Michiel Salters
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Pete Becker Guest
|
Posted: Thu Jun 09, 2005 9:40 am Post subject: Re: What is a good way to use c++ to delete all files in a d |
|
|
[email]matoos80 (AT) hotmail (DOT) com[/email] wrote:
| Quote: |
system("del *.dat");
but I would like to avoid the error message that occurs if there are no
files of that type to delete.
|
Since nobody else has come up with the right answer <g>:
system("if exist *.dat del *.dat")
--
Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
matoos Guest
|
Posted: Thu Jun 09, 2005 9:43 am Post subject: Re: What is a good way to use c++ to delete all files in a d |
|
|
Thanks all for the amazing feedback!
For now, system("del *.dat 1>NUL 2>NUL") is working like a charm but I
am also taking a look at the boost libraries.
Portability isn't an issue for this design. I find the std library is
pretty limited in terms of functions that create or check for the
existence of files. You could use error flags to check for a file's
existence, but then you end up doing a brute-force search by generating
every possible string and checking error flags for those strings ...
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Anand Hariharan Guest
|
Posted: Fri Jun 10, 2005 10:28 am Post subject: Re: What is a good way to use c++ to delete all files in a d |
|
|
Pete Becker wrote:
| Quote: | matoos80 (AT) hotmail (DOT) com wrote:
system("del *.dat");
but I would like to avoid the error message that occurs if there are no
files of that type to delete.
Since nobody else has come up with the right answer <g>:
system("if exist *.dat del *.dat")
|
How about -
void main() {}
;-)
- Anand
[NB: Have seen the OP's most recent post]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|