[Closed] Deleting to Recycle Bin
Hello, could anyone help me with finding a way to delete a folder and its contents to the recycle bin? I’ve searched online and it seems that it can only be done through visual basic with this line:
FileIO.FileSystem.DeleteDirectory (file, FileIO.UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin)
however I don’t know how to execute vb code through maxscript.
In general, could you point me to a tutorial / example of how to execute compiled code in c# & vb because I want to learn how to do it. Thanks in advance!
(
dotnet.loadAssembly "Microsoft.VisualBasic.dll"
FileIO = dotnetclass "Microsoft.VisualBasic.FileIO.FileSystem"
UIOption = (dotnetclass "Microsoft.VisualBasic.FileIO.UIOption").OnlyErrorDialogs
RecycleOption = (dotnetclass "Microsoft.VisualBasic.FileIO.RecycleOption").SendToRecycleBin
Directory = "C:\THE_FOLDER_TO_DELETE"
if (FileIO.DirectoryExists Directory) then
(
FileIO.DeleteDirectory Directory UIOption RecycleOption
)else(
messagebox "Directory not found."
)
)
Hi, I am not an expert in scripting/coding… Still I would like share my thoughts…
- the best way to go is to find a dos command line equivalent for the same…
Checkout http://www.maddogsw.com/cmdutils/
If this is feasible for you, you could easily call shellexecute and run this recycle from maxscript.
- If you have provision to access socket from maxscript, which I think there is…
You can write an application which could listen and execute the same for you outside max.
Again, I have very limited knowledge of maxscript, so forgive if this is not feasible.
deleteFile(), HiddenDOSCommand()
are these not helpful?
deleteFile can’t delete folders and its contents. It works fine with single files but not directories.
The hidden dos commands have a delete function, but it delete things permanently, not send it to the recycle bin, which I want to do because I want to be able to recover in case of a mistake.
sioDir = dotNetClass "System.IO.Directory"
if (SIODir.Exists folder) do SIODir.Delete (folder) true
also deletes things directly.
I’d like to not depend on external programs to delete things, because I’d like to keep my script self-contained. I’ll consider it once I’ve run out of options
From some of the research I’ve done you can delete to recycle bin through shell with some command called SHFileOperation, or you can delete with the vb code I posted above. There may be more, but I would like to see how the vb code way would work preferably, so that I can learn how to do that, but whatever works.
here is a good explanations how to move into recycling bin, and the working c# solution.
http://stackoverflow.com/questions/3282418/send-a-file-to-the-recycle-bin
i’ve just adapted it to mxs:
global FileOperationAPIWrapper
fn CreateFileOperationAPIWrapperAssembly =
(
source = ""
source += "using System;
"
source += "using System.IO;
"
source += "using System.Runtime.InteropServices;
"
source += "public class FileOperationAPIWrapper
"
source += " {
"
source += " /// <summary>
"
source += " /// Possible flags for the SHFileOperation method.
"
source += " /// </summary>
"
source += " [Flags]
"
source += " public enum FileOperationFlags : ushort
"
source += " {
"
source += " /// <summary>
"
source += " /// Do not show a dialog during the process
"
source += " /// </summary>
"
source += " FOF_SILENT = 0x0004,
"
source += " /// <summary>
"
source += " /// Do not ask the user to confirm selection
"
source += " /// </summary>
"
source += " FOF_NOCONFIRMATION = 0x0010,
"
source += " /// <summary>
"
source += " /// Delete the file to the recycle bin. (Required flag to send a file to the bin
"
source += " /// </summary>
"
source += " FOF_ALLOWUNDO = 0x0040,
"
source += " /// <summary>
"
source += " /// Do not show the names of the files or folders that are being recycled.
"
source += " /// </summary>
"
source += " FOF_SIMPLEPROGRESS = 0x0100,
"
source += " /// <summary>
"
source += " /// Surpress errors, if any occur during the process.
"
source += " /// </summary>
"
source += " FOF_NOERRORUI = 0x0400,
"
source += " /// <summary>
"
source += " /// Warn if files are too big to fit in the recycle bin and will need
"
source += " /// to be deleted completely.
"
source += " /// </summary>
"
source += " FOF_WANTNUKEWARNING = 0x4000,
"
source += " }
"
source += " /// <summary>
"
source += " /// File Operation Function Type for SHFileOperation
"
source += " /// </summary>
"
source += " public enum FileOperationType : uint
"
source += " {
"
source += " /// <summary>
"
source += " /// Move the objects
"
source += " /// </summary>
"
source += " FO_MOVE = 0x0001,
"
source += " /// <summary>
"
source += " /// Copy the objects
"
source += " /// </summary>
"
source += " FO_COPY = 0x0002,
"
source += " /// <summary>
"
source += " /// Delete (or recycle) the objects
"
source += " /// </summary>
"
source += " FO_DELETE = 0x0003,
"
source += " /// <summary>
"
source += " /// Rename the object(s)
"
source += " /// </summary>
"
source += " FO_RENAME = 0x0004,
"
source += " }
"
source += " /// <summary>
"
source += " /// SHFILEOPSTRUCT for SHFileOperation from COM
"
source += " /// </summary>
"
source += " [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
"
source += " private struct SHFILEOPSTRUCT
"
source += " {
"
source += " public IntPtr hwnd;
"
source += " [MarshalAs(UnmanagedType.U4)]
"
source += " public FileOperationType wFunc;
"
source += " public string pFrom;
"
source += " public string pTo;
"
source += " public FileOperationFlags fFlags;
"
source += " [MarshalAs(UnmanagedType.Bool)]
"
source += " public bool fAnyOperationsAborted;
"
source += " public IntPtr hNameMappings;
"
source += " public string lpszProgressTitle;
"
source += " }
"
source += " [DllImport(\"shell32.dll\", CharSet = CharSet.Auto)]
"
source += " private static extern int SHFileOperation(ref SHFILEOPSTRUCT FileOp);
"
source += " /// <summary>
"
source += " /// Send file to recycle bin
"
source += " /// </summary>
"
source += " /// <param name=\"path\">Location of directory or file to recycle</param>
"
source += " /// <param name=\"flags\">FileOperationFlags to add in addition to FOF_ALLOWUNDO</param>
"
source += " public static bool Send(string path, FileOperationFlags flags)
"
source += " {
"
source += " try
"
source += " {
"
source += " var fs = new SHFILEOPSTRUCT
"
source += " {
"
source += " wFunc = FileOperationType.FO_DELETE,
"
source += " pFrom = path + '\\0' + '\\0',
"
source += " fFlags = FileOperationFlags.FOF_ALLOWUNDO | flags
"
source += " };
"
source += " SHFileOperation(ref fs);
"
source += " return true;
"
source += " }
"
source += " catch (Exception)
"
source += " {
"
source += " return false;
"
source += " }
"
source += " }
"
source += " /// <summary>
"
source += " /// Send file to recycle bin. Display dialog, display warning if files are too big to fit (FOF_WANTNUKEWARNING)
"
source += " /// </summary>
"
source += " /// <param name=\"path\">Location of directory or file to recycle</param>
"
source += " public static bool Send(string path)
"
source += " {
"
source += " return Send(path, FileOperationFlags.FOF_NOCONFIRMATION | FileOperationFlags.FOF_WANTNUKEWARNING);
"
source += " }
"
source += " /// <summary>
"
source += " /// Send file silently to recycle bin. Surpress dialog, surpress errors, delete if too large.
"
source += " /// </summary>
"
source += " /// <param name=\"path\">Location of directory or file to recycle</param>
"
source += " public static bool MoveToRecycleBin(string path)
"
source += " {
"
source += " return Send(path, FileOperationFlags.FOF_NOCONFIRMATION | FileOperationFlags.FOF_NOERRORUI | FileOperationFlags.FOF_SILENT);
"
source += " }
"
source += " private static bool deleteFile(string path, FileOperationFlags flags)
"
source += " {
"
source += " try
"
source += " {
"
source += " var fs = new SHFILEOPSTRUCT
"
source += " {
"
source += " wFunc = FileOperationType.FO_DELETE,
"
source += " pFrom = path + '\\0' + '\\0',
"
source += " fFlags = flags
"
source += " };
"
source += " SHFileOperation(ref fs);
"
source += " return true;
"
source += " }
"
source += " catch (Exception)
"
source += " {
"
source += " return false;
"
source += " }
"
source += " }
"
source += " public static bool DeleteCompletelySilent(string path)
"
source += " {
"
source += " return deleteFile(path,
"
source += " FileOperationFlags.FOF_NOCONFIRMATION | FileOperationFlags.FOF_NOERRORUI |
"
source += " FileOperationFlags.FOF_SILENT);
"
source += " }
"
source += " }
"
csharpProvider = dotnetobject "Microsoft.CSharp.CSharpCodeProvider"
compilerParams = dotnetobject "System.CodeDom.Compiler.CompilerParameters"
compilerParams.ReferencedAssemblies.Add("System.dll");
compilerParams.GenerateInMemory = true
compilerResults = csharpProvider.CompileAssemblyFromSource compilerParams #(source)
if (compilerResults.Errors.Count > 0 ) then
(
errs = stringstream ""
for i = 0 to (compilerResults.Errors.Count-1) do
(
err = compilerResults.Errors.Item[i]
format "Error:% Line:% Column:% %
" err.ErrorNumber err.Line err.Column err.ErrorText to:errs
)
MessageBox (errs as string) title: "Errors encountered while compiling C# code"
format "%
" errs
undefined
)
else
(
assembly = compilerResults.CompiledAssembly
assembly.CreateInstance "FileOperationAPIWrapper"
)
)
FileOperationAPIWrapper = CreateFileOperationAPIWrapperAssembly()
/*
FileOperationAPIWrapper.MoveToRecycleBin <file>
FileOperationAPIWrapper.DeleteCompletelySilent <file>
*/
clearlistener()
source_string = "using Microsoft.VisualBasic.FileIO;class mmm { public static void DeleteDirectory( string directoryPath ){Microsoft.VisualBasic.FileIO.FileSystem.DeleteDirectory( directoryPath, Microsoft.VisualBasic.FileIO.UIOption.OnlyErrorDialogs, Microsoft.VisualBasic.FileIO.RecycleOption.SendToRecycleBin, Microsoft.VisualBasic.FileIO.UICancelOption.ThrowException);}}"
fn dll =
(
local source = StringStream ( source_string )
compilerParams = dotnetobject "System.CodeDom.Compiler.CompilerParameters"
compilerParams.ReferencedAssemblies.Add("System.dll");
compilerParams.ReferencedAssemblies.Add("Microsoft.VisualBasic.dll");
compilerParams.ReferencedAssemblies.Add(@"C:\Program Files\Autodesk\3ds Max 2014\Autodesk.Max.dll");
compilerParams.GenerateInMemory = on
csharpProvider = dotnetobject "Microsoft.CSharp.CSharpCodeProvider"
compilerResults = csharpProvider.CompileAssemblyFromSource compilerParams #(source as String)
flush source
close source
if (compilerResults.Errors.Count > 0 ) then
(
local errs = stringstream ""
for i = 0 to (compilerResults.Errors.Count-1) do
(
local err = compilerResults.Errors.Item[i]
format "Error:% Line:% Column:% %
" err.ErrorNumber err.Line err.Column err.ErrorText to:errs
)
format "%
" errs
undefined
)
else
(
compilerResults.CompiledAssembly.CreateInstance "mmm"
)
)
a = dll()
a.deletedirectory "C:\asd"
vb style
So many different ways to do the same thing! Thank you everyone for the help!