-
Deleting question
I would like to know how you write a program that deletes the contents of a folder ohter than the actual folder itself. I posted the same question before, but I can't find it on this board...
Here is the code that I tried (taken from an answer to one of my posts)
Code:
system("rm -rf folderName");
This, however, did not work. Has anybody got any ideas?
Thanks
-Chris
-
>>> system("rm -rf folderName");
The reasons you should not use system() are numerous, and in the FAQ. Anyway, the command you are issuing, (rm), is a unix command, I thought you were using some version of Windows and Dev-C?
-
you are correct to say that I am using Windows and Dev C++
So...what should i us instead of that example?
-
use the win32 api functions.....
FindFirstFile()
FindNextFile()
DeleteFile()
look them up in your compilers help files and/or msdn.
-
does that work in DOS? (or Win Console Applications)
-
Certainly in a console. A console is only a special type of Window, the entire API is still available for you to call, (of course some of the calls are meaningless in a console situation).
S_C's suggestion is correct - this is how you should be working these days. One reason you should not use system() that is not in the FAQ is that it is so damned old fashioned!
-
what include file are required for those functions?
-
...and could somebody give me some example code explaining how to use those functions...please....
-
The functions, like most basic API functions, are in winbase.h which is included in windows.h, so you've almost certainly got them already.
Code:
WIN32_FIND_DATA FindData;
HANDLE hFind;
hFind = FindFirstFile(SearchPath, // Search path is the directory/filemask you want
&FindData);
if (hFind != INVALID_HANDLE_VALUE)
{
// Do whatever you want with the first file
while(FindNextFile(hFind,&FindData))
{
// Do whatever with the next.. and so on
}
FindClose(hFind); // Close the find handle
}
else
{
// There wern't any files
}
-
First question: in line >>> while(FindNextFile(hFind,&FindData)) does this line check every file in the folder?
Second question: is this possible in a DOS/Windows Console Window too? If so, how is this done?
-
When you call FindFirstFile(), you supply it with a path which includes a filename mask. If no files match that path/mask combination, the function returns INVALID_HANDLE_VALUE and you know there are no files of that type. If there is one, it retuns a valid find handle and the file details in the WIN32_FIND_DATA structure. You can now call FindNextFile() using the same handle to continue searching the same directory for files matching the mask you used above. It will return the details of each file it finds in the structure as before, and when there are no more files returns NULL. To answer your first question then, the construct will check every file, and return all of those which match the mask. If you specify a general mask, say "*.*" it will return all files in the directory.
Yes you can do it in a console app. As I said before, the console app is just a special window. You do it in exactly the same way.
-
How would I delete these files using the remove(); function in the code that you have posted here?
-
Presumably you have the path to the directory in a string, so concatenate the filename to the path and pass that to remove() or DeleteFile().
Assuming your path is in a string called "DirPath", you don't want to modify that, and that FilePath is another string, and your find data structure is called FindData...
strcpy (FilePath, DirPath);
strcat (FilePath, FindData.cFileName);
DeleteFile(FilePath); // or remove(FilePath);
-
In M$ operating systems, try
system("deltree /y folderName");
But read the warning!!!
Deletes a directory and all the subdirectories and files in it.
To delete one or more files and directories:
DELTREE [/Y] [drive:]path [[drive:]path[...]]
/Y Suppresses prompting to confirm you want to delete
the subdirectory.
[drive:]path Specifies the name of the directory you want to delete.
Note: Use DELTREE cautiously. Every file and subdirectory within the
specified directory will be deleted.
-
>>> In M$ operating systems, try system("deltree /y folderName");
And then, as I have said before, read the FAQ to find out why you should avoid using system().