Thread: delteing a file...I got it just need help...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    731

    delteing a file...I got it just need help...

    Alright I have delted a single file but I want to try and delete a whole directory with files in it.

    Here is the code I got so far...

    Code:
    //Lots o headers please don't comment on them
    
    #include <iostream.h>
    #include <conio.h>
    #include <windows.h>
    #include <iomanip>
    #include <fstream.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <math.h>
    #include <shellapi.h>
    
    using namespace std;
    
    
    
    int main()
    {    
        typedef struct _SHFILEOPSTRUCT    //SHFileOperation structure?
        {
            HWND hwnd;
            UINT wFunc;
            LPCTSTR pFrom;
            LPCTSTR pTo;
            FILEOP_FLAGS fFlags;
            BOOL fAnyOperationsAborted;
            LPVOID hNameMappings;
            LPCTSTR lpszProgressTitle;
        } SHFILEOPSTRUCT, *LPSHFILEOPSTRUCT;
    
        
        _SHFileOperation(_SHFILEOPSTRUCT);    //deletes file
    }
    yes yes I know tons of headers not sure wich ones I need. (Basicly my test headers)

    ok so I got alot of non filled out junk. Can someone help me with that?


    Edit: I really don't need them filled. I just need maybe 1 or 2 filled out to see how to properly fill them out.
    Last edited by Rune Hunter; 09-17-2004 at 04:33 PM.

  2. #2
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    First, don't put the leading underscore. The function is called SHFileOperation, according to MSDN and Google. Second, you shouldn't need to define the struct yourself.

    Ok. According to MSDN (found by searching on Google), what you want is:

    wFunc = FO_DELETE
    (?)hwnd = NuLL
    pFrom = "path\filename.ext\0path2\filename2.ext\0path3\*.* \0";
    pTo = NULL
    etc.

    It's the same as filling out any struct; read which member does what, fill it with what you need, and pass it to the function.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> please don't comment on them
    What if I just want to comment on the fact that you're mixing standard and non-standard C++ headers and that it's sure to cause you problems?

    Ok, I won't.

    gg

  4. #4
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    ok alright first time I'm doing something like this.

    So far this is what I got...


    Code:
    //Lots o headers please don't comment on them
    
    #include <iostream.h>
    #include <conio.h>
    #include <windows.h>
    #include <iomanip>
    #include <fstream.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <math.h>
    #include <shellapi.h>
    
    using namespace std;
    
    
    
    int main()
    {    
        typedef struct SHFILEOPSTRUCT    //SHFileOperation structure?
        {
            UINT wFunc = FO_DELETE
            (?)hwnd = NuLL
            LPCTSTR pFrom = "path\filename.ext\0path2\filename2.ext\0path3\*.*\0"
            LPCTSTR pTo = NULL
            FILEOP_FLAGS fFlags;
            BOOL fAnyOperationsAborted;
            LPVOID hNameMappings;
            LPCTSTR lpszProgressTitle;
        } SHFILEOPSTRUCT, *LPSHFILEOPSTRUCT;
    
        
        SHFileOperation(SHFILEOPSTRUCT);    //deletes file
    }

    the headers never caused probeblems for me yet when I do the testing stage. But anways. Can I leave some of the things just like they are.

    Like one of them only returns true if so and so happens. Do I do anything with them?


    and here is my error log. (I get 2 errors)


    C:\Documents and Settings\Alex\Desktop\test.cpp In function `int main()':
    22 C:\Documents and Settings\Alex\Desktop\test.cpp syntax error before `?' token
    25 C:\Documents and Settings\Alex\Desktop\test.cpp:23 [Warning] unknown escape sequence '\*'
    32 C:\Documents and Settings\Alex\Desktop\test.cpp syntax error before `)' token


    Edit: the error before `)' is in this line: SHFileOperation(SHFILEOPSTRUCT); //deletes file


    Thank you for all of your help.
    Last edited by Rune Hunter; 09-17-2004 at 06:10 PM.

  5. #5
    you have more then 2 errors if you look at it this way
    treat warnings as if they were errors and get rid of them

    sorry wasted post but its a good habit.

  6. #6
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    lol no prob but the warning as I can I see is not fixable sence that is how I am suposed to put the lines like that. But the other 2 I need fixed.

  7. #7
    actually from what i see the warning is the path up there, and
    instead of \ you should have \\

  8. #8
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Rune: My post was intended to let you know what goes in what member, i.e. how to fill the struct in. You weren't supposed to copy and paste, as you can probably tell by now. In fact, if you just look at what I posted, the (?) means "I'm not sure about this one", you'll notice that I made a typo and put "NuLL" instead of "NULL", didn't put semicolons on the ends of the lines.

    And, looking at your 'updated' code again, I see that you're redefining the structure still, and also you're trying to initialize the structure members in the prototype, which you can't do (you can only do it in a constructor). And you still shouldn't be redefining the structure, because then you're essentially giving the function something that it wasn't meant to take. What you want is something more like this:
    Code:
    SHFILEOPSTRUCT sfos;
    sfos.wFunc = FO_DELETE;
    sfos.hwnd = NULL; //(?)
    etc.
     
    SHFileOperation(&sfos);
    [edit]And yes, the \ should be \\. Oversight on my part.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  9. #9
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    ok but first I did copy and paste. But I modified. Becuase when I put the semicolens in it has that error were I tried to initialize. That is why they are not in. And yes I do look and feel like an idoit but I gotta start somewere. I might start looking for a nice book on amazon.com or something today so that shoudl help me lesson the questions and stupidity.

  10. #10
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Have you learned about structures yet?
    The tutorial has a section on them, if you haven't read about them yet. 'sfos' is short for SHFileOpStruct, and is an object of type SHFileOpStruct, which is already defined in another header - which is why I said you shouldn't be defining it yourself.

    >>error were I tried to initialize
    Once you've read about structures, you'll (probably) know why you got these errors. In case you don't, it's because you can't initialize variables in a structure definition - either make a constructor for the structure and do initialization in that, or manually fill the members in each object you create. In this case, it's the latter, since you're not defining the structure yourself.
    Last edited by Hunter2; 09-18-2004 at 09:36 AM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  11. #11
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    Well yes I have read all the tuts on this site. Well the bsaics anyways. (ok not the last 2 or so but that has notin to do with this yet).

  12. #12
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    Ok I am really close now!


    First off here is my updated code tat compilers and "works"...


    Code:
    #include <iostream.h>
    #include <conio.h>
    #include <windows.h>
    #include <iomanip>
    #include <fstream.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <math.h>
    #include <shellapi.h>
    
    using namespace std;
    
    
    
    int main()
    {    
        SHFILEOPSTRUCT sfos;
        sfos.hwnd = NULL;
        sfos.wFunc = FO_DELETE;
        sfos.pFrom = "C:\\test\\*.*";
        sfos.pTo = NULL;
        sfos.fFlags = NULL;
        sfos.fAnyOperationsAborted = NULL;
        sfos.hNameMappings = NULL;
        sfos.lpszProgressTitle = NULL;
         
        SHFileOperation(&sfos);
    
    }

    Now the only error I get is when I run the program it gives an error saying it can read file or folder from disk. I know why that happens: they folders are read only.

    And when I un check the read only button -> hit apply -> hit ok, it stays as read only. I can't remember my trick on how to make it stay that way but do anyone else know?

  13. #13
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    I've never had problems with folders making themselves read-only automatically...
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  14. #14
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    oh never mind that. But yes that is crazy but then again our computer does some wierd things. But anyways my probeblem was the NULL turminating character wich I need after the path I deinated.

    sfos.pFrom = "C:\\test\\*.*"; should be sfos.pFrom = "C:\\test\\*.*\0";


    It works now and I finnaly learned how to use files and folders now.

  15. #15
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    alright sence it is working now I am actually makeing a really program. All this does is delete the cookies and stuff. But I want to be able to exclude a file extension but still be able to use the * for a wild card. Is this possible?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  4. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM