How do I delete files from a directory?

S

Sonoman

Hi all:
I would like to find out how to write a small program that deletes files. I
want to be able to delete all files from a known directory (i.e. cookies,
temp files, etc.) regardless of type and name, "or" may be by extension.
This way I could have it run on start up and I will have my computer free of
junk every time I reboot. What should I use? Please advise.
 
R

Richard Herring

Sonoman said:
Hi all:
I would like to find out how to write a small program that deletes files. I
want to be able to delete all files from a known directory (i.e. cookies,
temp files, etc.) regardless of type and name, "or" may be by extension.
This way I could have it run on start up and I will have my computer free of
junk every time I reboot. What should I use?

You should use the scripting language provided by your operating system.
Please advise.

This is not something that can be done using standard C++.
 
A

Andreas Baumgart

I would like to find out how to write a small program that deletes
files. I
want to be able to delete all files from a known directory (i.e.
cookies,
temp files, etc.) regardless of type and name, "or" may be by
extension.

The way you delete files from a filesystem normally depends on the
syscalls your os provides. So there is no clear way to do this.

On Linux systems there is a syscall "unlink" which can do that. There
also exists an accordant function unlink() in the libc. I am quite sure
there exists a similar library for Windows, too.

Greets,
Andreas
 
J

Jeff Flinn

Sonoman,

Sonoman said:
Hi all:
I would like to find out how to write a small program that deletes files. I
want to be able to delete all files from a known directory (i.e. cookies,
temp files, etc.) regardless of type and name, "or" may be by extension.
This way I could have it run on start up and I will have my computer free of
junk every time I reboot. What should I use? Please advise.

See http://www.boost.org/libs/filesystem/doc/operations.htm#remove_all.

Jeff F
 
R

rossum

Hi all:
I would like to find out how to write a small program that deletes files. I
want to be able to delete all files from a known directory (i.e. cookies,
temp files, etc.) regardless of type and name, "or" may be by extension.
This way I could have it run on start up and I will have my computer free of
junk every time I reboot. What should I use? Please advise.

I suggest you look at the remove(const char *filename) function in
cstdio which might be what you want.

rossum
 
J

Jack Klein

The way you delete files from a filesystem normally depends on the
syscalls your os provides. So there is no clear way to do this.

Apparently you are unaware of the standard C++ library function
std::remove() prototyped in <cstdlib>, which may also be invoked as
::remove() by including <stdlib.h>.

These have been part of the ISO C standard for 15 years now, and part
of ISO C++ for nearly 6 years. They work on all conforming hosted
implementations, and that includes Linux and Windows.
 
R

Richard Herring

Jack Klein said:
Apparently you are unaware of the standard C++ library function
std::remove() prototyped in <cstdlib>, which may also be invoked as
::remove() by including <stdlib.h>.

Ahem. The OP wants to delete _all_ files, not a single named file. I
don't think you can use std::remove() to do that, without some
system-dependent support to enumerate the files.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top