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: Klaus, FourthWorld, heatherlaine, kevinmiller, robinmiller

Post Reply
Martin Koob
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 252
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: 614
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: 2555
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: 252
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: 2555
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: 252
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: 252
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?

Post Reply

Return to “Windows”