how to remove the file to trash? Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
xin.songtao
Experienced Solver
Experienced Solver
Posts: 86
Joined: Wed Apr 18, 2007 6:10 am
Location: Shanghai China

how to remove the file to trash?

Post by xin.songtao » Fri Feb 05, 2010 3:26 am

Hi, guys!
Is there a way to remove the file to trash?(Windows && MacOS)

Waiting for your reply.....
from: Shanghai China
language: C++/C
platform:MSW\MacOS\Linux
Email: [email protected]

ninja9578
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 236
Joined: Thu Jan 29, 2009 3:33 pm

Post by ninja9578 » Fri Feb 05, 2010 4:03 am

I'm almost certain that both platforms simply have a folder for their trash. Simply move the file into the folder. It's in a weird place in Windows, I can get you the path if you'd like.

On OSX it's ~/.Trash

xin.songtao
Experienced Solver
Experienced Solver
Posts: 86
Joined: Wed Apr 18, 2007 6:10 am
Location: Shanghai China

Post by xin.songtao » Fri Feb 05, 2010 4:56 am

ninja9578 wrote:I'm almost certain that both platforms simply have a folder for their trash. Simply move the file into the folder. It's in a weird place in Windows, I can get you the path if you'd like.

On OSX it's ~/.Trash
Thanks for your reply!
ninja9578 wrote: It's in a weird place in Windows, I can get you the path if you'd like.
Is the same folder in windowsXP, windows vista and windows 7?
I am looking forward to know where is the place,and how to get it.
from: Shanghai China
language: C++/C
platform:MSW\MacOS\Linux
Email: [email protected]

JimFairway
wxWorld Domination!
wxWorld Domination!
Posts: 1059
Joined: Sun Dec 30, 2007 6:40 pm
Location: Canada

Post by JimFairway » Fri Feb 05, 2010 5:36 am

Hi,

Here's some code I found a while ago from here (see figure 3): http://msdn.microsoft.com/en-us/magazine/bb985590.aspx

It will move files to the recycle bin on windows if the FOF_ALLOWUNDO flag is set.

RecycleFile.h

Code: Select all

////////////////////////////////////////////////////////////////
// MSDN — April 2001
// If this code works, it was written by Paul DiLascia.
// If not, I don't know who wrote it.
// Compiles with Visual C++ 6.0. Runs on Windows 98 and probably Windows 
// 2000 too.
//
#include <shellapi.h>

//////////////////
// CRecycleFile — sends a file to the Recycle Bin.
// Note derived from SHFILEOPSTRUCT.
//
class CRecycleFile : public SHFILEOPSTRUCT {
protected:
public:
   CRecycleFile();
   ~CRecycleFile() { }
   int Recycle(LPCTSTR pszPath, BOOL bDelete=FALSE);
};
RecycleFile.cpp

Code: Select all

////////////////////////////////////////////////////////////////
// MSDN ? April 2001
// If this code works, it was written by Paul DiLascia.
// If not, I don't know who wrote it.
// Compiles with Visual C++ 6.0. Runs on Windows 98 and probably Windows 
// 2000 too.
//
#include <windows.h>
#include <tchar.h>
#include "RecycleFile.h"


//////////////////
// Constructor initializes SHFILEOPSTRUCT with reasonable
// defaults. You can override if you like. Go ahead, make my day.
//
CRecycleFile::CRecycleFile()
{
   memset((SHFILEOPSTRUCT*)this,0,sizeof(SHFILEOPSTRUCT));
   fFlags |= FOF_SILENT;                // don't report progress
   fFlags |= FOF_NOERRORUI;             // don't report errors
   fFlags |= FOF_NOCONFIRMATION;        // don't confirm delete
}

//////////////////
// Send a file to the recycle bin. Args:
//  - full pathname of file.
//  - bDelete: if TRUE, really delete file (no recycle bin)
//
int CRecycleFile::Recycle(const char * pszPath, bool bDelete)
{
   // Copy pathname to double-NULL-terminated string.
   //
   TCHAR buf[_MAX_PATH + 1]; // allow one more character
   strcpy(buf,pszPath);
   buf[strlen(buf)+1]=0;    // need two NULLs at end
 //  _tcscpy(buf, pszPath);    // copy caller's path name
 //  buf[_tcslen(buf)+1]=0;    // need two NULLs at end
 
   // Set SHFILEOPSTRUCT params for delete operation
   //
   wFunc = FO_DELETE;                   // REQUIRED: delete operation
   pFrom = buf;                         // REQUIRED: which file(s)
   pTo = NULL;                          // MUST be NULL
   if (bDelete) {                       // if delete requested..
      fFlags &= ~FOF_ALLOWUNDO;         // ..don't use Recycle Bin
   } else {                             // otherwise..
      fFlags |= FOF_ALLOWUNDO;          // ..send to Recycle Bin
   }
   return SHFileOperation(this);        // do it!
}
To use it:

Code: Select all

wxString fileToRecycle;
CRecycleFile x;
x.Recycle(fileToRecycle.c_str());
Hope that helps,

Jim
OS: Vista SP1, wxWidgets 2.8.7.

xin.songtao
Experienced Solver
Experienced Solver
Posts: 86
Joined: Wed Apr 18, 2007 6:10 am
Location: Shanghai China

Post by xin.songtao » Fri Feb 05, 2010 9:10 am

Thanks for you give a great hand.
:D
from: Shanghai China
language: C++/C
platform:MSW\MacOS\Linux
Email: [email protected]

ninja9578
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 236
Joined: Thu Jan 29, 2009 3:33 pm

Post by ninja9578 » Fri Feb 05, 2010 2:57 pm

C:/RECYCLER

That's the most common one, but Windows has one on each hard drive, so if you have two hard drives, you have a C:/RECYCLER and a D:/RECYCLER

:)

They're hidden folders.

Most likely in Linux it's going to be in the same place as OSX, but it may depend on the distro.

mael15
Super wx Problem Solver
Super wx Problem Solver
Posts: 449
Joined: Fri May 22, 2009 8:52 am
Location: Bremen, Germany

Re: how to remove the file to trash?

Post by mael15 » Thu Jun 18, 2020 11:31 am

Is this still the same on Windows 10?

User avatar
doublemax
Moderator
Moderator
Posts: 15264
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: how to remove the file to trash?

Post by doublemax » Thu Jun 18, 2020 11:39 am

mael15 wrote:
Thu Jun 18, 2020 11:31 am
Is this still the same on Windows 10?
The api still exists under Windows 10 and is even used by wxFileName::Rmdir
https://github.com/wxWidgets/wxWidgets/ ... .cpp#L1312
Use the source, Luke!

PB
Part Of The Furniture
Part Of The Furniture
Posts: 2662
Joined: Sun Jan 03, 2010 5:45 pm

Re: how to remove the file to trash?

Post by PB » Thu Jun 18, 2020 11:43 am


mael15
Super wx Problem Solver
Super wx Problem Solver
Posts: 449
Joined: Fri May 22, 2009 8:52 am
Location: Bremen, Germany

Re: how to remove the file to trash?

Post by mael15 » Thu Jun 18, 2020 2:16 pm

perfect, thanx!

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 4529
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: how to remove the file to trash?

Post by ONEEYEMAN » Thu Jun 18, 2020 4:58 pm

Hi,
The pull request PB referenced takes care of all 3 major platforms.
Give it a shot and see what happens.

Thank you.

mael15
Super wx Problem Solver
Super wx Problem Solver
Posts: 449
Joined: Fri May 22, 2009 8:52 am
Location: Bremen, Germany

Re: how to remove the file to trash?

Post by mael15 » Thu Jun 18, 2020 5:08 pm

ONEEYEMAN wrote:
Thu Jun 18, 2020 4:58 pm
Hi,
The pull request PB referenced takes care of all 3 major platforms.
Give it a shot and see what happens.

Thank you.
thanx, I only need it for windows but hope it will be part of the next regular release. Very important function imho. I copy and pasted the msw part.

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 4529
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: how to remove the file to trash?

Post by ONEEYEMAN » Thu Jun 18, 2020 5:52 pm

Hi,
As an author I hope so too... :D

Thank you.

Post Reply