none
how can we delete a file from recycle bin using vc++? RRS feed

  • Question

  • Hi all

    using SHEmptyRecycleBin function is deleting all files from the drive where the file is presents on diver

    my question is

    How delete single file from recycle bin ?

     

    Anyone have anyidea?

    please share with me

    Thanks

     

     

     

     

    Tuesday, February 15, 2011 7:52 AM

All replies

  • "How to programmatically use the Recycle Bin"

    http://www.codeproject.com/kb/shell/recyclebin.aspx

    - Wayne
    Tuesday, February 15, 2011 8:11 AM
  • hi Wayne,

    Thanks for reply,

    I have seen that one,  there he is using os context menu for selected file. then every options is working like in recycle bin.

     

    but i need the way to delete the file without context menu and without confirmation for permanent deletion

     

    Anyone have anyidea?

    please share with me

    Thanks

    Tuesday, February 15, 2011 9:02 AM
  •  

    Hi pch,

     

    I think the easiest way to empty the RecycleBin is to use Windows API SHEmptyRecycleBin Function.

     

    For the last parameter, you can set SHERB_NOCONFIRMATION. So that there is no dialog confirming appears.

     

    I hope my suggestions can help you to solve this problem.

     

    Best regards,

    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, February 17, 2011 6:14 AM
    Moderator
  • Hi Jesse,

    thanks for your replay,

    deleting all files from recycle bin automatically, instead of deleting specific file if we used SHEmptyRecycleBin function

    that means, Recycle bin contains more than one file which all are from c: drive,

    suppose if we pass one file path to SHEmptyRecycleBin function then automatically deleting all files from recycle bin

     

    but i need the way to delete single file from recycle bin

     

    Anyone have anyidea?

    please share with me

    Thanks 

     

    Thursday, February 17, 2011 11:57 AM
  •  

    Hi,

     

    I'm so sorry for that, we can not use this function to delete a single file. we should think about other ways. But we can use this function to delete some files in a folder like "d:\\test", which been deleted.

     

    I hope my suggestions can help you to solve this problem.

     

    Regards,

    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, February 22, 2011 10:51 AM
    Moderator
  • Hi Jesse , 

    thanks for your replay,

     

    am not trying to delete a file from folder, i want to delete a file from recycle bin that to delete single file at time

     

    can any one tell me, how can we delete a file from recycle bin if there are more than one file in recycle bin

    that to its silent deletion, no confirmation

     

    Anyone have anyidea?

    please share with me

    Thanks 

    Tuesday, February 22, 2011 1:03 PM
  • What are you trying to do in the first place.

    The fact that you are trying to delete a single file from the recycle bin makes me think you are trying to do something in the wrong way.

    Are you deleting a file from your application and it is ending up in the recycle bin? If so then try using a different delete function.


    Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.
    Visit my (not very good) blog at
    http://ccprogramming.wordpress.com/
    Tuesday, February 22, 2011 8:40 PM
  • Hi Crescens

     

    Thanks for reply

     

    Are you deleting a file from your application and it is ending up in the recycle bin? If so then try using a different delete function.

     

    yea am following to delete a file from my application.

     

    so can you tell me the different delete function?

     

    Thanks


    Wednesday, February 23, 2011 1:28 PM
  • >can you tell me the different delete function?

    Different than *what*? You haven't said which
    operation you are using to delete the file in
    the first place from your program.

    I suspect Crescens is referring to the use of a
    Win32 API function which will delete a file and
    *not* send it to the recycle bin:

    SHFileOperation Function
    http://msdn.microsoft.com/en-us/library/bb762164(v=vs.85).aspx

    "When used to delete a file, SHFileOperation permanently
    deletes the file unless you set the FOF_ALLOWUNDO flag
    in the fFlags member of the SHFILEOPSTRUCT structure
    pointed to by lpFileOp. Setting that flag sends the file
    to the Recycle Bin. If you want to simply delete a file
    and guarantee that it is not placed in the Recycle Bin,
    use DeleteFile."

    - Wayne
    Wednesday, February 23, 2011 5:58 PM
  • hi Wayne

    thanks for reply, 

    am not trying to delete any file from normal location, 

    SHFileOperation Function is used to delete a file form normal location.

     


    am trying to delete a file permanently from Recycle Bin,

    loaded all the file names and its locations to my application from Recycle bin using IShellFolder2 interface,

    now i want to delete a file from Recycle Bin which is selected in my application?


    what is the procedure to delete a file from recycle bin?

     

    Anyone have anyIdea,

    Please share with me,

    Thanks

    Thursday, February 24, 2011 7:46 AM
  • Anyone have anyIdea,

    Please share with me,

    Thanks

    Monday, March 7, 2011 12:47 PM
  • please try this  (VB.net)

    My.Computer.FileSystem.DeleteFile("paht of the file",FileIO.UIOption.AllDialogs,FileIO.RecycleOption.DeletePermanently)

     



    Wednesday, July 6, 2011 3:10 PM
  • Hi, Pch System.

    I am not sure that this question is still relevant for you, however, I want to help those who still need a solution to this problem.

    I myself recently needed to work with the Windows recycle bin. Of course, I began to look for methods of working with her. However, here with removal there was a problem. This question is often asked, but everyone redirects each other to this project: How to programmatically use the Recycle Bin in C++.

    However, I will answer immediately: there is NO implementation of the removal. Yes, there is a context menu with such an item, however, it is a context menu from the shell wrapper (shell api), so you will not find an implementation of this method.

    Because of all the redirects and the lack of MSDN specific description of methods of working with recycle bin and similar virtual folders, I had to dig.

    So, for listing recycle bin code already laid out. But to remove you need to use interface IFileOperation.

    I provide a code that reflects the whole process of listing the recycle bin and removing from it:

    #include <shobjidl_core.h>
    #include <Shlobj.h>
    
    int main() {
    	CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
    
    	LPSHELLFOLDER pDesktop = NULL;
    	SHGetDesktopFolder(&pDesktop);
    
    	LPITEMIDLIST pidlRecycleBin = NULL;
    	SHGetSpecialFolderLocation(NULL, CSIDL_BITBUCKET, &pidlRecycleBin);
    
    	IShellFolder2 *pRecycleBin;
    	pDesktop->BindToObject(pidlRecycleBin, NULL, IID_IShellFolder2, (LPVOID*)&pRecycleBin);
    
    	pDesktop->Release();
    	CoTaskMemFree(pidlRecycleBin);
    
    	IEnumIDList* penumFiles;
    	pRecycleBin->EnumObjects(NULL, SHCONTF_FOLDERS | SHCONTF_NONFOLDERS, &penumFiles);
    	
    	STRRET sret; SHELLDETAILS shd;
    	LPMALLOC pMalloc = NULL;
    	SHGetMalloc(&pMalloc);
    
    	IFileOperation *pfo;
    	CoCreateInstance(CLSID_FileOperation, NULL, CLSCTX_ALL, IID_PPV_ARGS(&pfo));
    	pfo->SetOperationFlags(FOF_NO_UI);
    
    	IShellItem *shi;
    
    	LPITEMIDLIST pidl = NULL;
    	int i = 1;
    	while (penumFiles->Next(1, &pidl, NULL) != S_FALSE) {
    		pRecycleBin->GetDisplayNameOf(pidl, SHGDN_NORMAL, &sret);
    		switch (sret.uType) {
    			case STRRET_CSTR:
    				cout << sret.cStr << ' '; //free sret.cStr not need because stored in stack
    				break;
    			case STRRET_OFFSET:
    				break;
    			case STRRET_WSTR:
    				wcout << sret.pOleStr << L' ';
    				pMalloc->Free(sret.pOleStr);
    				break;
    		}
    
    		pRecycleBin->GetDetailsOf(pidl, 2, &shd); //return detete date; 2 is a index of deletetime feild
    		switch (shd.str.uType) {
    			case STRRET_CSTR:
    				cout << shd.str.cStr << endl; //free shd.str.cStr not need because stored in stack
    				break;
    			case STRRET_OFFSET:
    				cout << endl;
    				break;
    			case STRRET_WSTR:
    				//need to delete incorrect symbols in deletedate
    				wstring wsc(shd.str.pOleStr);
    				wsc.erase(15, 1); wsc.erase(14, 1);
    				wsc.erase(8, 1); wsc.erase(4, 1);
    				wsc.erase(0, 1);
    
    				unsigned char d, h, mo, mi; 
    				unsigned short y;
    				swscanf(wsc.c_str(), L"%u.%u.%u %u:%u", &d, &mo, &y, &h, &mi);
    
    				//output
    				wcout << wsc.c_str() << endl;
    				pMalloc->Free(shd.str.pOleStr);
    				break;
    		}
    
    		/*MARK ITEMS TO DELETE*/
    		SHCreateItemWithParent(NULL, pRecycleBin, pidl, IID_IShellItem, (void**)&shi);
    		pfo->DeleteItem(shi, NULL);
    		shi->Release();
    
    		
    		CoTaskMemFree(pidl);
    	}
    	pMalloc->Release();
    	penumFiles->Release();
    	pRecycleBin->Release();
    
    	/*DELETE MARKED ITEMS*/
    	pfo->PerformOperations();
    	pfo->Release();
    
    	//Update recycle bin icon [undocumented function]
    	(void (*)())GetProcAddress(GetModuleHandle(L"shell32.dll"), "SHUpdateRecycleBinIcon")();
    
    	CoUninitialize();
    
            return 0;
    }
    • Edited by Michail1778 Thursday, August 1, 2019 5:09 PM
    Thursday, August 1, 2019 5:05 PM