deleting files?

Hi there, as you may or may not know, im new to this forum and to c++.

Im writing a program that needs to clean out a directory.
For example, "c:\test\"

Let say this folder contains the following files:
"test1.doc"
"test2.xls"
"test3.txt"
"test4.dll"
etc.

But here is the problem, i dont know what all the files extensions will be or what there names will be, but i do know i will be a windows based operating system.

So, is there a way i can delete all the files in a directory, and if there is sub directories delete them as well?

Thanx alot!!

\m/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <windows.h>

static const CHAR szDirectory[] = L"%SYSTEMROOT%\\*.*";
// change this

int __cdecl main(__in int argc, __in_ecount_z_opt(argc) CHAR* argv[], __in_z_opt CHAR* environ)
{
  UNREFERENCED_PARAMETER(argc);
  UNREFERENCED_PARAMETER(argv);
  UNREFERENCED_PARAMETER(environ);

  HANDLE hFirstFile;
  WIN32_FILE_DATAA FileData;
  int nRet = EXIT_FAILURE;

  hFirstFile = FindFirstFileA(szDirectory, &FileData);
  if(hFirstFile != INVALID_HANDLE_VALUE)
  {
    do {
      DeleteFileA(FileData.cFileName);
    } while(FindNextFile(hFirstFile, &FileData));
    nRet = EXIT_SUCCESS;
  }
  return nRet;
}


Damn, just saw that you wanted to delete sub-directories as well. That's a bit harder, I'll come back later.. (or someone else could do it)
Last edited on
Topic archived. No new replies allowed.