Deleting question

This is a discussion on Deleting question within the C++ Programming forums, part of the General Programming Boards category; I would like to know how you write a program that deletes the contents of a folder ohter than the ...

  1. #1
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052

    Thumbs up 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

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    >>> 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?
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    you are correct to say that I am using Windows and Dev C++

    So...what should i us instead of that example?

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    use the win32 api functions.....
    FindFirstFile()
    FindNextFile()
    DeleteFile()

    look them up in your compilers help files and/or msdn.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    does that work in DOS? (or Win Console Applications)

  6. #6
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    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!
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  7. #7
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    what include file are required for those functions?

  8. #8
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    ...and could somebody give me some example code explaining how to use those functions...please....

  9. #9
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    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
    	}
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  10. #10
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    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?

  11. #11
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    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.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  12. #12
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    How would I delete these files using the remove(); function in the code that you have posted here?

  13. #13
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    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);
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  14. #14
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,497
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  15. #15
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    >>> 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().
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. Hypothetical question about deleting from a file
    By sunburnbyRA in forum C++ Programming
    Replies: 6
    Last Post: 03-10-2003, 08:34 AM
  3. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM
  4. Question about linked lists.
    By cheeisme123 in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2003, 12:36 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21