Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remove Multiple Files in a Directory

Status
Not open for further replies.

CJason

Programmer
Oct 13, 2004
223
0
0
US
Is there a way to delete multiple files in a directory from C? I know there is the "remove" function...but, I think that is only to remove a single file, correct? What I'm looking for is something like "remove(*.dat)". Is that possible? If not, does someone have a simple algorithm that maybe loops though the *.dat files in a directory, and "removes" each one...one by one?

Thanks in advance!
 
No system independent library function(s) to scan directory in C, but there is such API in every system. Collect file names by "*.dat" template with this API (of your target system) then make loop with remove() function...
 
Apropos, you may use system("delete *.dat") call (correct cmd text for your target system too;).
 
What OS are you running this on? Some possibilities are MS Windows has
Code:
remove()
with multiple ways of moving through directories.

Unix has
Code:
remove()
opendir()
readdir()
unlink()
that can be used in conjunction with each other. I think the remove() function is ANSI standard, it's definetly K&R standard. Or as already mentioned use system() to call the command line (possibly a script that does the work). If you don't want to use a script then you will have to use some form of loop to cycle through the files one at a time, looking for the .dat extension and deleting.

Bertha
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top