Deleting question

This is a discussion on Deleting question within the C++ Programming forums, part of the General Programming Boards category; i've been trying all night and I can't figure out how place this code: Code: strcpy (FilePath, DirPath); strcat (FilePath, ...

  1. #16
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    i've been trying all night and I can't figure out how place this code:
    Code:
    strcpy (FilePath, DirPath); 
    strcat (FilePath, FindData.cFileName); 
    DeleteFile(FilePath); // or remove(FilePath);
    into this code:
    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
    	}
    Could you please help me out here.
    Thanks

  2. #17
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    Put it where the comments are that tell you to do whatever you want with the first file, and the other where it says do whatever you want with the next!!!!!!!!!!!!!!

    I can't do more than this! I don't know how your code is set up, what your variables are called, or even what type they are! You have, here, a generic solution to your problem, AND, the information I am missing.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #18
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    I have no idea what I am doing here, so you will have to bare with me here . This is all I have so far and I have no idea how to get this to work...

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <windows.h>
    
    int main()
    {
      WIN32_FIND_DATA FindData;
    	HANDLE hFind;
    
    	hFind = FindFirstFile("C:\\My Documents\\Folder\\*.*", // 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
    	{
          cout << "The folder is empty"; // There wern't any files
    	}
    
    
    
    return 0;
    }
    Am I completely wrong? I have added the bits that the comments said, but for the others...I don't know where to go. Could you please help me out? I'm sorry for being so dumb, but I will learn from this, so it isn't a complete waste of time...I think...

    Thanks
    -Chris

  4. #19
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    Where the comments are that say do whatever, add the line...

    cout <<FindData.cFileName<<endl;

    I have created an empty project and put your code in it. I created a directory structure like yours, and copied some gash files into it. I added the lines above and now I get a list of files. Try this with your set up. If you get a list of files, then we'll go onto the next step.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  5. #20
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    ok...I have put together this code (thus taking another step) and I just need to know where to declare FileData and what to put in it...
    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <windows.h>
    #include <stdio.h>
    
    int main()
    {
    
    
        WIN32_FIND_DATA FindData;
    	HANDLE hFind;
    
        char DirPath[100] = "C:\\My Documents\\Chris\\Folder";
        char FilePath[100] = "C:\\My Documents\\Chris\\Folder\\stuff.NMW";
    
        strcpy (FilePath, DirPath); 
        strcat (FilePath, FileData.cFileName);
        remove(FilePath);
    
        hFind = FindFirstFile("C:\\My Documents\\Chris\\Folder\\*.*", // Search path is the directory/filemask you want
    		                  &FindData);
    	if (hFind != INVALID_HANDLE_VALUE)
    	{
               cout <<FindData.cFileName<< "\n";
    
    		while(FindNextFile(hFind,&FindData))
    		{
                cout <<FindData.cFileName<< "\n";
    		}
    		FindClose(hFind); // Close the find handle
    	}
    	else
    	{
          cout << "There were no files found";
    	}
    
      return 0;
    }

  6. #21
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    I'm assuming you can see the files in the directory. Okay, as you've worked out, the DeleteFile() (or remove()) API calls require a complete path to the file they are to delete, so we must build that. Your DirPath string does not have the final "\" on it. After executing your strcat, your FilePath will look like this...

    C:\\My Documents\\Chris\\FolderFileName

    ... you need...

    C:\\My Documents\\Chris\\Folder\\FileName

    ... so fix that first.

    Then you are trying to access something called FileData.cFileName. This does not exist. cFileName is a member of the WIN32_FIND_DATA structure, and in your program, you have called it FindData, (much more sensible actually!). So change your strcat from FileData to FindData.

    You then need to put the strcpy/strcat/DeleteFile() where the cout's are which are currently outputting the filenames.

    Try that.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  7. #22
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    brfore we go any furthur, I don't think you understand what I am trying to accomplish here. I am trying to get his to delete everything inside the folder, without knowing the actual filenames. Is this what is going on now?

  8. #23
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    YES! YES! YES!

    <fx head bumping on the desk> bump bump bump </fx>

    YES! YES! YES!

    YOU don't know what files are in the directory, but to delete a file, the DeleteFile() function must be told. You cannot say "delete a file - I don't know what it is called" can you? Something has to link the contents of the directory to the mechanism for deleting the file. THAT is why you must log the contents of the directory with FindFirstFile() and FindNextFile()!!!!! You then pass the name of each file that they find to the delete service.

    <fx> bump bump bump..... </fx> !!!
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

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

    ohhh, sorry

    oh ok. In the code that I gave you, should I have placed "C:\\My Documents\\Chris\\Folder\\stuff.NMW" into the string DirPath[100]?

  10. #25
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    No. DirPath is the path to the directory you are working with. Your initialisation of FilePath is meaningless, since you will overwrite it with the strcpy routine. FilePath is a dynamic entity, it will contain the directory path you do know, and the filenames you don't.

    Fix the faults I mentioned, and move the code to where I said. Try it and tell me if we are there yet!
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  11. #26
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    >>> Try it and tell me if we are there yet!

    And soon. I'm not going to be here for a week or two after Friday lunch time, (central European time)!

    Off to Greece for a bit of r 'n r!
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  12. #27
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    Thank you so much for helping me out...the code looks great and works well and I have also learned how to do all this (yay!). Just so you can see what the final product of all of these posts, here is the code:
    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <windows.h>
    #include <stdio.h>
    
    int main()
    {
    
    
        WIN32_FIND_DATA FindData;
    	HANDLE hFind;
    
        char DirPath[100] = "C:\\My Documents\\Chris\\Folder";
        char FilePath[100];
    
        hFind = FindFirstFile("C:\\My Documents\\Chris\\Folder\\*.*",
    		                  &FindData);
    	if (hFind != INVALID_HANDLE_VALUE)
    	{
               strcpy (FilePath, DirPath); 
               strcat (FilePath, "\\");
               strcat (FilePath, FindData.cFileName);
               remove(FilePath);
    
    		while(FindNextFile(hFind,&FindData))
    		{
                strcpy (FilePath, DirPath); 
                strcat (FilePath, "\\");
                strcat (FilePath, FindData.cFileName);
                remove(FilePath);
    		}
    		FindClose(hFind);
    	}
    	else
    	{
          cout << "There were no files found\n";
    	}
        system("pause");
    
      return 0;
    }
    Thanks again

  13. #28
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    Good. It's working. Now a few things you might want to think about.

    1. At the moment you have a program which will delete all files in that directory. The directory is hard coded, as is the mask. For the purpose of this job, that is good enough, but what you could do is change this such that the working part of the program was a function that you passed the directory and mask to. You would then have a more useful item to keep in your "toolbox". (All programmers have a toolbox of little functions that do useful things that they can drop into their code as required). Consider...

    ClearDirectory("C:\\Temp\\", "*.tmp"); // delete .tmp files
    ClearDirectory("C:\\Program Files\\", *.log"); // delete log files

    ... see what I mean, useful.

    2. You have used a string length of 100. Again, this is good enough for this job, but windows.h actually defines a value MAX_PATH which is the maximum allowable size of a path to a file. To make your general function, you should use this, (i.e. you should say char FilePath[MAX_PATH], rather than [100]). Currently the value assigned to MAX_PATH is 260, but you should use the name.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  14. #29
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    yeah, I know all about that *.* stuff.

  15. #30
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    oh yeah, and have a good time at Greece

Page 2 of 3 FirstFirst 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