Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. deleting folder

deleting folder

Scheduled Pinned Locked Moved C / C++ / MFC
help
3 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    learningvisualc
    wrote on last edited by
    #1

    Hi all, I was trying to make self deleting executable. I found a article on code project [] my problem is i am stucked at one place my folder is not getting deleted.

    // temporary .bat file
    CString templ =
    "del \"%s\"\r\n"
    "taskkill /im selfdelete.exe\r\n"
    "del \"%s\"\r\n"
    "rmdir /S /Q \"%s\"\r\n"
    "del \"%s\"" ;

    //CString temppath;
    
    CString tempstr;
    CString modulename;
    
    LPTSTR pPath = tempstr.GetBufferSetLength(MAX\_PATH);
    GetTempPath ( MAX\_PATH, pPath );
    tempstr.ReleaseBuffer();
    tempstr = tempstr + tempbatname;
    
    LPTSTR modulenm = modulename.GetBufferSetLength(MAX\_PATH);
    GetModuleFileName(NULL,modulenm,MAX\_PATH) ;
    modulename.ReleaseBuffer();
    
    CString str = modulename;
    int k = str.Find('\\\\');
    while(k >= 0)
    {
    	str = str.Right(str.GetLength()-k-1);
    	k = str.Find('\\\\');
    }
    
    CString folder = modulename.Left(modulename.GetLength()-str.GetLength());
    
    CString iniFile = folder;
    CString file = "\\\\selfdeletepath.txt";
    iniFile = iniFile + file;
    
    HANDLE hf ;
    hf = CreateFile(tempstr, GENERIC\_WRITE, 0, NULL,CREATE\_ALWAYS, FILE\_ATTRIBUTE\_NORMAL, NULL) ;
    
    if (hf != INVALID\_HANDLE\_VALUE)
    {
    	DWORD len ;
    	CString bat ;
    	LPTSTR bBat = bat.GetBufferSetLength(MAX\_PATH);
    	bat = (char\*)alloca(strlen(templ) + strlen(modulename) \* 2 + strlen(tempstr) + 20) ;
    	wsprintf(bBat, templ,iniFile, modulename, folder, tempstr) ;
    	WriteFile(hf, bat, strlen(bat), &len, NULL) ;   
    	CloseHandle(hf) ;
    	ShellExecute(NULL, "open", tempstr, NULL, NULL, SW\_HIDE);  
    }
    

    }

    can anybody please tell me what i am doing wrong. thanks in advance

    M A 2 Replies Last reply
    0
    • L learningvisualc

      Hi all, I was trying to make self deleting executable. I found a article on code project [] my problem is i am stucked at one place my folder is not getting deleted.

      // temporary .bat file
      CString templ =
      "del \"%s\"\r\n"
      "taskkill /im selfdelete.exe\r\n"
      "del \"%s\"\r\n"
      "rmdir /S /Q \"%s\"\r\n"
      "del \"%s\"" ;

      //CString temppath;
      
      CString tempstr;
      CString modulename;
      
      LPTSTR pPath = tempstr.GetBufferSetLength(MAX\_PATH);
      GetTempPath ( MAX\_PATH, pPath );
      tempstr.ReleaseBuffer();
      tempstr = tempstr + tempbatname;
      
      LPTSTR modulenm = modulename.GetBufferSetLength(MAX\_PATH);
      GetModuleFileName(NULL,modulenm,MAX\_PATH) ;
      modulename.ReleaseBuffer();
      
      CString str = modulename;
      int k = str.Find('\\\\');
      while(k >= 0)
      {
      	str = str.Right(str.GetLength()-k-1);
      	k = str.Find('\\\\');
      }
      
      CString folder = modulename.Left(modulename.GetLength()-str.GetLength());
      
      CString iniFile = folder;
      CString file = "\\\\selfdeletepath.txt";
      iniFile = iniFile + file;
      
      HANDLE hf ;
      hf = CreateFile(tempstr, GENERIC\_WRITE, 0, NULL,CREATE\_ALWAYS, FILE\_ATTRIBUTE\_NORMAL, NULL) ;
      
      if (hf != INVALID\_HANDLE\_VALUE)
      {
      	DWORD len ;
      	CString bat ;
      	LPTSTR bBat = bat.GetBufferSetLength(MAX\_PATH);
      	bat = (char\*)alloca(strlen(templ) + strlen(modulename) \* 2 + strlen(tempstr) + 20) ;
      	wsprintf(bBat, templ,iniFile, modulename, folder, tempstr) ;
      	WriteFile(hf, bat, strlen(bat), &len, NULL) ;   
      	CloseHandle(hf) ;
      	ShellExecute(NULL, "open", tempstr, NULL, NULL, SW\_HIDE);  
      }
      

      }

      can anybody please tell me what i am doing wrong. thanks in advance

      M Offline
      M Offline
      m_mun
      wrote on last edited by
      #2

      You can try SHFILEOPSTRUCT Structure

      1 Reply Last reply
      0
      • L learningvisualc

        Hi all, I was trying to make self deleting executable. I found a article on code project [] my problem is i am stucked at one place my folder is not getting deleted.

        // temporary .bat file
        CString templ =
        "del \"%s\"\r\n"
        "taskkill /im selfdelete.exe\r\n"
        "del \"%s\"\r\n"
        "rmdir /S /Q \"%s\"\r\n"
        "del \"%s\"" ;

        //CString temppath;
        
        CString tempstr;
        CString modulename;
        
        LPTSTR pPath = tempstr.GetBufferSetLength(MAX\_PATH);
        GetTempPath ( MAX\_PATH, pPath );
        tempstr.ReleaseBuffer();
        tempstr = tempstr + tempbatname;
        
        LPTSTR modulenm = modulename.GetBufferSetLength(MAX\_PATH);
        GetModuleFileName(NULL,modulenm,MAX\_PATH) ;
        modulename.ReleaseBuffer();
        
        CString str = modulename;
        int k = str.Find('\\\\');
        while(k >= 0)
        {
        	str = str.Right(str.GetLength()-k-1);
        	k = str.Find('\\\\');
        }
        
        CString folder = modulename.Left(modulename.GetLength()-str.GetLength());
        
        CString iniFile = folder;
        CString file = "\\\\selfdeletepath.txt";
        iniFile = iniFile + file;
        
        HANDLE hf ;
        hf = CreateFile(tempstr, GENERIC\_WRITE, 0, NULL,CREATE\_ALWAYS, FILE\_ATTRIBUTE\_NORMAL, NULL) ;
        
        if (hf != INVALID\_HANDLE\_VALUE)
        {
        	DWORD len ;
        	CString bat ;
        	LPTSTR bBat = bat.GetBufferSetLength(MAX\_PATH);
        	bat = (char\*)alloca(strlen(templ) + strlen(modulename) \* 2 + strlen(tempstr) + 20) ;
        	wsprintf(bBat, templ,iniFile, modulename, folder, tempstr) ;
        	WriteFile(hf, bat, strlen(bat), &len, NULL) ;   
        	CloseHandle(hf) ;
        	ShellExecute(NULL, "open", tempstr, NULL, NULL, SW\_HIDE);  
        }
        

        }

        can anybody please tell me what i am doing wrong. thanks in advance

        A Offline
        A Offline
        Adam Roderick J
        wrote on last edited by
        #3

        I smell this exe itself to be like a virus Well i think you are doing something which is weird ;P ShellExecute itself, check the argument. Well i am doubted about your logic of deletion. I believe you can achieve the deletion in XP with a different logic. Create a remote thread and attach to some other process before exiting your native executable, and then delete from that remote thread. Just try that :)

        Величие не Бога может быть недооценена.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups