Moving files to Windows' Recycle Bin in LiveCode -- is it possible?

Deploying to Windows? Utilizing VB Script execution? This is the place to ask Windows-specific questions.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Martin Koob
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 256
Joined: Sun May 27, 2007 8:19 pm

Moving files to Windows' Recycle Bin in LiveCode -- is it possible?

Post by Martin Koob » Wed Nov 25, 2020 12:27 pm

Hi.

On the Mac I have a handler that can move a folder to the trash by using an AppleScript that I call with a shell command.

I have a custom property of the stack “Scripts" cDeleteMacFolderScript that contains a template script

Code: Select all

tell application "Finder"

	delete "**foldertodelete**"

end tell
Then I have the following handler

Code: Select all

on deletemacOSfolder folderMacpath

	put the cDeleteMacFolderScript  of stack  “Scripts" into thescript

	replace "**foldertodelete**" with folderMacpath in thescript

	do thescript as applescript

end deletemacOSfolder
It works a charm, run it and the folder ends up in the Trash Easy-peasy.


I have been trying to do the same thing with Windows and not having any luck.

First Challenge was finding the path to the recycle bin. Using SpecialfolderPath() to get the path for the Recycle Bin did not work. See the bug report here. https://quality.livecode.com/show_bug.cgi?id=22995 <https://quality.livecode.com/show_bug.cgi?id=22995>

I did manage write a script using the Windows shell commands to get a path to the user’s Recycle Bin and then move a directory to it using the LiveCode rename command. The folder is actually moved to the user's Recycle Bin but when you Open the Recycle Bin in the Windows Explorer GUI you don’t see the folder.
Here is the script I used.

Code: Select all

on mouseUp pButtonNumber

	answer folder "Select the folder to move to Recycle Bin folder."

	put it into tFoldertoRecycle

	if platform() is "win32" or platform() is "win64" then

		MoveFolderToRecycleBin tFolderToRecycle

	end if

end mouseUp


command MoveFolderToRecycleBin pFolderToMove

	put SpecialFolderPath_RecycleBinFolder() into tRecycleBinPath

	replace "/" with "\" in tRecycleBinPath

	set the itemDelimiter to "\"

	put the last item of pFolderToMove into tFolderName

	put tRecycleBinPath & "\" & tFolderName into tRecycleBinPath

	put pFolderToMove into line 1 of field 1

	put tRecycleBinPath &Cr &CR into line 2 of field 1

	rename folder pFolderToMove to tRecycleBinPath

end MoveFolderToRecycleBin


function SpecialFolderPath_RecycleBinFolder

	put shell ("whoami /user") into tUserInformation

	set the itemDelimiter to space

	put item 2 of line 7 of tUserInformation into tUserSID

	put "C:\$Recycle.Bin\" & tUserSID into tUserRecycleBinPath

	return tUserRecycleBinPath

end SpecialFolderPath_RecycleBinFolder


You can see that the folder was actually moved there by using the Windows Command Prompt program. If you type:

Code: Select all

C:\> cd \$Recycle.Bin
C: \$Recycle.Bin> dir /s /a
You will get a listing of the contents of the directory and the sub-directories including the one that contains the files in the logged in user’s recycle bin.
- files or folders the user dragged there in the GUI are there (but their file names are not the same as their actual names)
- folders I moved there using the script above are also there (their folder names are the same as they were in their original location).

I have read a lot of posts and documentation on the Recycle Bin and know far more than I ever wanted to know about it but it seems that simply moving a file to the users directory for the user’s Recycle Bin does not actually put it in the “RecycleBin" as seen in the Windows Explorer GUI. I found a post that says that here.

https://superuser.com/questions/1520290 ... -partition

This folder (Recycle Bin) is a special folder, because if you double-click the folder within Explorer, it will open the Recycle Bin program, rather than show the contents of that folder. As a result, it will show you all files deleted in your current Windows Installation, and not what is actually inside that folder.

I don’t see any other way of Recycling a folder, or a file for that matter using the shell short of buying and installing one of a couple of utility programs that popped up in some of my searches. I can’t expect users to do that.

I know you can delete a folder and its contents using the revDeleteFolder Command. This removes the folder and files instantly. I was hoping there was a way to just move them the Recycle Bin but it does not appear to be possible from shell. However the fact that there are utilities that do it mean there must be some way programmatically.

Does anyone know of a way to accomplish this?

Thanks

Martin Koob
VideoLinkwell

mrcoollion
Posts: 709
Joined: Thu Sep 11, 2014 1:49 pm
Location: The Netherlands

Re: Moving files to Windows' Recycle Bin in LiveCode -- is it possible?

Post by mrcoollion » Wed Nov 25, 2020 9:54 pm

The Win32::FileOp module has a Recycle function.
Maybe use this within a shell command.

Regards,

Paul

SparkOut
Posts: 2834
Joined: Sun Sep 23, 2007 4:58 pm

Re: Moving files to Windows' Recycle Bin in LiveCode -- is it possible?

Post by SparkOut » Wed Nov 25, 2020 10:02 pm

You can get "do as vbscript" with the code here: https://www.robvanderwoude.com/sourceco ... ecycle_vbs

Martin Koob
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 256
Joined: Sun May 27, 2007 8:19 pm

Re: Moving files to Windows' Recycle Bin in LiveCode -- is it possible?

Post by Martin Koob » Fri Nov 27, 2020 6:19 pm

Thanks mrcoolion and SparkOut for your responses.

mrcoolion
It looks like the Win32::FileOp is something that would have to be installed on the user's PC in order for my LiveCode program to use it. Am I right about that? If that is the case I don't want to have to do that.

SparkOut
Think this solution is better but I have another question. From doing some research (and finding a post by you that I have copied below.) I gather that I can't use this script by storing that script as a custom property somewhere in my app and then run it with

Code: Select all

do "......" as "vbScript
since it has a WScript object in it which in your post below will cause the script to fail.

SparkOut wrote:
Thu Oct 16, 2008 10:35 am
The only way I know how you could do this would be with getting Rev to run some vbscript which you can call by running a file from the shell. I don't believe it would be possible to use the "do... as vbscript" method directly from Rev, as
Rev docs wrote:Any scripts on Windows which contain references to WScript will fail to run as WScript objects do not exist in the Revolution Environment
whereas the Rev shell command will receive the wscript.echo as a return value. In any event I'm unaware of how to get a "do..." statement list to parse "if..then" constructs.

The best way I've found (rather than interrogating the user groups, which may be nested very deeply) is as suggested near the end of this thread http://www.myitforum.com/forums/m_14241 ... htm#142413 which checks to see if the logged on user has access to the administrative share on the local drive.

So you could store this vbscript code file in the appropriate place, or write it out temporarily and delete it after running:

Code: Select all

Set Shell = CreateObject("WScript.Shell") 
Set filesys = CreateObject("Scripting.FileSystemObject") 
computername = Shell.ExpandEnvironmentStrings("%computername%") 
if filesys.FolderExists("\" & computername & "\Admin$\System32") then 
wScript.echo ("Admin") 
else 
wScript.echo ("Not Admin") 
end if
set filesys = nothing
set Shell = nothing
So from your post here it seems I need to run it as a shell script. The one thing I am not sure about is how to pass the argument of the file I want to move to the recycle bin to the shell script when I run it in LiveCode. If someone has a quick answer to that it would save me a lot of time searching and hacking about.

Thanks.

Martin Koob

SparkOut
Posts: 2834
Joined: Sun Sep 23, 2007 4:58 pm

Re: Moving files to Windows' Recycle Bin in LiveCode -- is it possible?

Post by SparkOut » Fri Nov 27, 2020 7:51 pm

Oh yes, my bad sorry, I forgot. But you can do what you want with vbscript still, similar approaches here (ignore references to Lua) http://forums.livecode.com/viewtopic.ph ... 58&#p35666

Martin Koob
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 256
Joined: Sun May 27, 2007 8:19 pm

Re: Moving files to Windows' Recycle Bin in LiveCode -- is it possible?

Post by Martin Koob » Fri Nov 27, 2020 11:31 pm

Hi Sparkout

Thanks so much for your help. With it I think I am getting closer. I have done a little LiveCode script that I think should run the recycle.vbs file which is found here:
https://www.robvanderwoude.com/sourceco ... ecycle_vbs

The folder to be moved to the recycle bin is passed as the only parameter.

Here is the LiveCode script that I used to run the recycle.vbs script

Code: Select all

local sVBScriptFile  ## make the script file path a script local variable so I don't have to keep selecting it each time

on mouseup
   if the altkey is down then ## if you need to change the path then hold the alt key down while clicking mouseUp
      put empty into sVBScriptFile  
   end if
   if sVBScriptFile is empty then
      answer file "select the vbs file" with type ".vbs"   ## I just select the "recycle.vbs " file here
      put it into sVBScriptFile
   end if
   answer folder "select the folder you a to move to trash."
   put it into tFilePath
   get shell("cscript.exe //nologo" && quote & sVBScriptFile & quote && quote & tFilePath  & quote)
   Put "it" & it & CR into message box
   put "the result:" && the result after message box
end mouseup
When I first ran this I get an error in the result

Code: Select all

...\recycle.vbs(60, 1) Microsoft VBScript runtime error: Object required
I realized that I needed to provide a path with back slashes "\" rather than forward slashes "/" so I added a line to acheve this. See the third linein the code below.

Code: Select all

   answer folder "select the folder you a to move to trash."
   put it into tFilePath
  replace "/" with "\" in tFilePath  ## This ensures that the file path is correct.

Once I did that it seems the code from the syntax routine ran since it output that info.
itRecycle.vbs, Version 1.10
Move the contents of a folder to the Recycle Bin

Usage: RECYCLE.VBS folder_path

Where: folder_path specifies the folder to be purged

Note: Deleting root directories, profile folders, the Program Files or
Windows folder or My Documents is not allowed.

Written by Rob van der Woude
http://www.robvanderwoude.com

the result:
There was no error but neither the folder nor its contents were moved into the recycle bin. What I want is both the folder with its contents moved to the recycle bin moved there.

I tried using both cscript.exe and wScript.exe
I also tried the command in the command prompt and I got the same result.

So I am getting a few steps forward but not there yet. Any help would be greatly appreciated.

Thanks

Martin

Martin Koob
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 256
Joined: Sun May 27, 2007 8:19 pm

Re: Moving files to Windows' Recycle Bin in LiveCode -- is it possible?

Post by Martin Koob » Sat Nov 28, 2020 11:41 pm

After some stepping through the code inserting wscript.echo commands at various points I found that it would kick it out to the syntax subroutine if the file I wanted to be moved was in the Documents directory or in my home folder

I had to comment out the lines that checked if the path to be deleted strFolder contained the path to Documents objMyDocsFolder.Path or the path to my home directory.
Here is the code for Documents where I commented out that line

Code: Select all

' Skip My Documents
Set objMyDocuments  = objShell.Namespace( MY_DOCUMENTS )
Set objMyDocsFolder = objMyDocuments.Self
' Script runs fine up to here.  It exits at the next step to syntax
strDebugMsg ="strFolder: " & strFolder & vbCrLf & vbCrLf & "objMyDocsFolder.Path: " & objMyDocsFolder.Path
WScript.Echo strDebugMsg
'If InStr( 1, strFolder, objMyDocsFolder.Path, vbTextCompare ) Then Syntax
Set objMyDocsFolder = Nothing
Set objMyDocuments  = Nothing
Not sure why the script is done that way. I can see wanting to prevent the Documents folder from being deleted but then should have some thing to check if the strFolder = objMyDocsFolder.path.

This is also done for what it calls Profiles

Code: Select all

' Skip My Documents
Set objMyDocuments  = objShell.Namespace( MY_DOCUMENTS )
Set objMyDocsFolder = objMyDocuments.Self
' Script runs fine up to here.  It exits at the next step to syntax
strDebugMsg ="strFolder: " & strFolder & vbCrLf & vbCrLf & "objMyDocsFolder.Path: " & objMyDocsFolder.Path
WScript.Echo strDebugMsg
'If InStr( 1, strFolder, objMyDocsFolder.Path, vbTextCompare ) Then Syntax
Set objMyDocsFolder = Nothing
Set objMyDocuments  = Nothing
Once I commented those out then the script would run and the documents and Directories(and documents within those directories) in the Selected folder I passed to this command would be moved to the Recycle Bin YAAAY! But the folder that contains them did not get moved to the Recycle Bin. I want them moved as a unit. Odd it does it this way and I can't find much online about this.

If there is not a way to have the folder and its contents moved to the recycle bin with this or another command - the one work around solution I can think of is I could select the containing folder of the folder I want to move to Recycle Bin pass that to the Recycle.vbs script as the filePath parameter and then go through the contents of that folder and remove the Folders and DocumentsI want to keep and then let it go ahead and move the remaining items to the bin.

Any Thoughts?

Zax
Posts: 414
Joined: Mon May 28, 2007 10:12 am
Location: France

Re: Moving files to Windows' Recycle Bin in LiveCode -- is it possible?

Post by Zax » Wed May 11, 2022 7:00 pm

2 years later...
I would like to move a single file (not a folder) to the windows 10-11 recycle bin. Are now the things more simple than in 2020, using LC 9.6.7?

Thank you.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9801
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Moving files to Windows' Recycle Bin in LiveCode -- is it possible?

Post by FourthWorld » Wed May 11, 2022 8:38 pm

Zax wrote:
Wed May 11, 2022 7:00 pm
I would like to move a single file (not a folder) to the windows 10-11 recycle bin. Are now the things more simple than in 2020, using LC 9.6.7?
Not at a Win box at the moment so I can't test it, but if you can get the path to the Recycle folder (isn't it "C:\$Recycle.Bin"?) you should be able to use the rename command to rename the path, effectively moving the file, eg.:

Code: Select all

on MoveToRecycleBin pFilePath
   set the itemdel to "/"
   put last item of pPath into tFileName
   put "C:/"& $Recycle.Bin &"/"& tFileName into tDestPath
   rename pFilePath to tDestPath
   if the result is not empty then -- always check for errors with file ops:
      answer "Couldn't move file: "& the result &" ("& sysError() &")"
      exit to top
   end if
end MoveToRecycleBin
This is off the cuff and completely untested. If it doesn't work let's explore how to make it work. But I believe the core idea here of using the path to the Recycle bin with LC's rename command is the key to simple trashing.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

SparkOut
Posts: 2834
Joined: Sun Sep 23, 2007 4:58 pm

Re: Moving files to Windows' Recycle Bin in LiveCode -- is it possible?

Post by SparkOut » Wed May 11, 2022 11:59 pm

It's not as simple as that unfortunately. C:\$Recycle.Bin gives the root of a virtualised folder location I believe. It certainly fails when trying to rename the folder path from LC.

However, you can still do it with VBScript and I have pared down what is needed to:

Code: Select all

on MoveToRecycleBin pPathToFileForDeletion
   replace "/" with "\" in pPathToFileForDeletion
   --change to Windows format for VBScript
   put "on error resume next" & cr into tVBS
   put "Set app = CreateObject(~Shell.Application~)" & cr after tVBS
   put "Set fso = CreateObject(~Scripting.FileSystemObject~)" & cr after tVBS
   put "If fso.FileExists(~[[pPathToFileForDeletion]]~) Then" & cr after tVBS
   put "   Set file = fso.GetFile(~[[pPathToFileForDeletion]]~)" & cr after tVBS
   put "   Set folderItem = app.Namespace(0).ParseName(file.Path)" & cr after tVBS
   put "   folderItem.InvokeVerb(~delete~)" & cr after tVBS
   put "   If( Err.number <> 0 ) Then" & cr after tVBS
   put "      result = ~Error in deletion~" & cr after tVBS
   put "   Else" & cr after tVBS
   put "      result = ~Delete invoked~" & cr after tVBS
   put "   End If" & cr after tVBS
   put "Else" & cr after tVBS
   put "   result = ~File not found: [[pPathToFileForDeletion]]~" & cr after tVBS
   put "End If" & cr after tVBS
   put "Set fso = nothing" & cr after tVBS
   put "Set app = nothing" after tVBS
   replace "~" with quote in tVBS
   put merge(tVBS) into tVBS  --merge will replace the placeholders with the values
   put tVBS -- so you can see what the script looks like after replacing quotes and filename
   do tVBS as "vbscript"
   put the result into tResult
   put "The result" && tResult
end MoveToRecycleBin
Most of the script is just constructing the VBS template so you can certainly create it once, and then put that into a custom property ready for the placeholder to be replaced and then the script executed:

Code: Select all

on error resume next
Set app = CreateObject("Shell.Application")
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists("[[pPathToFileForDeletion]]") Then
   Set file = fso.GetFile("[[pPathToFileForDeletion]]")
   Set folderItem = app.Namespace(0).ParseName(file.Path)
   folderItem.InvokeVerb("delete")
   If( Err.number <> 0 ) Then
      result = "Error in deletion"
   Else
      result = "Delete invoked"
   End If
Else
   result = "File not found: [[pPathToFileForDeletion]]"
End If
Set fso = nothing
Set app = nothing

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9801
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Moving files to Windows' Recycle Bin in LiveCode -- is it possible?

Post by FourthWorld » Thu May 12, 2022 12:34 am

Serious question: why does Microsoft provide a path to the Recycle Bin if it can't be used?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

stam
Posts: 2599
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Moving files to Windows' Recycle Bin in LiveCode -- is it possible?

Post by stam » Thu May 12, 2022 2:04 am

Nice one SparkOut!
I have no immediate use for this just yet but will likely need soon... stored for future use!

SparkOut
Posts: 2834
Joined: Sun Sep 23, 2007 4:58 pm

Re: Moving files to Windows' Recycle Bin in LiveCode -- is it possible?

Post by SparkOut » Thu May 12, 2022 7:20 am

FourthWorld wrote:
Thu May 12, 2022 12:34 am
Serious question: why does Microsoft provide a path to the Recycle Bin if it can't be used?
I don't know.
I wouldn't be surprised if the "justification" was on the grounds of "security" because
"You wouldn't want a malicious batch script to run that could recycle files, now would you?"
- "but what about the fact you can permanently del..."
"Security!"
- "or that you can recycle files by powershell, or vbscript?"
"Talk to the palm"

But really each logged in user has a distinct set of recycled files in a folder relating to the user's SID. So there's definitely some jiggery-pokery needed to identify user account and permissions. Or let vbscript do it.

LCMark
Livecode Staff Member
Livecode Staff Member
Posts: 1206
Joined: Thu Apr 11, 2013 11:27 am

Re: Moving files to Windows' Recycle Bin in LiveCode -- is it possible?

Post by LCMark » Thu May 12, 2022 9:25 am

Serious question: why does Microsoft provide a path to the Recycle Bin if it can't be used?
macOS does too - its at ~/.Trash (I think there are also folders related to Trash on each read-write volume as well).

The files-which-you-might-want-to-recover have to be stored somewhere!

Zax
Posts: 414
Joined: Mon May 28, 2007 10:12 am
Location: France

Re: Moving files to Windows' Recycle Bin in LiveCode -- is it possible?

Post by Zax » Thu May 12, 2022 11:54 am

Thank you SparkOut :)
As I am not at home this week, I only tested your script on Win 7 emulated by WMware, and it works.

Would it be possible for you to post a version of your script to handle folder? I know I was only talking about a file in my previous post... I had to change my standalone script.

Post Reply

Return to “Windows”