{"id":1183,"date":"2014-04-21T07:00:00","date_gmt":"2014-04-21T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2014\/04\/21\/how-can-i-get-information-about-the-items-in-the-recycle-bin-from-script\/"},"modified":"2014-04-21T07:00:00","modified_gmt":"2014-04-21T07:00:00","slug":"how-can-i-get-information-about-the-items-in-the-recycle-bin-from-script","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20140421-00\/?p=1183","title":{"rendered":"How can I get information about the items in the Recycle Bin from script?"},"content":{"rendered":"<p>\nToday we&#8217;ll do a scripting version of\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2011\/08\/30\/10202076.aspx\">\nan old C++ program<\/a>:\nPrinting information about the items in the Recycle Bin.\n(How you wish to act on the information is up to you.)\n<\/p>\n<p>\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2013\/01\/14\/10384593.aspx\">\nThis is a pattern we&#8217;ve seen a lot<\/a>.\nBind to a folder, enumerate its contents,\nextract properties.\n<\/p>\n<pre>\nvar shell = new ActiveXObject(\"<a HREF=\"http:\/\/msdn.microsoft.com\/library\/bb776890.aspx\">Shell.Application<\/a>\");\nvar recycleBin = shell.NameSpace(10); \/\/ CSIDL_BITBUCKET\nvar items = recycleBin.Items();\nfor (var i = 0; i &lt; items.Count; i++) {\n  var item = items.Item(i);\n  WScript.StdOut.WriteLine(item.Name);\n  WScript.StdOut.WriteLine(item.ExtendedProperty(\n                                \"System.Recycle.DeletedFrom\"));\n  WScript.StdOut.WriteLine(item.ExtendedProperty(\n                                \"System.Recycle.DateDeleted\"));\n  WScript.StdOut.WriteLine(item.Size);\n}\n<\/pre>\n<p>\nWow, that was way easier than doing it in C++!\n<\/p>\n<p>\nJust for fun, I&#8217;ll do it in C#, first as a straight port:\n<\/p>\n<pre>\n\/\/ add a reference to shell32.dll\nclass Program {\n  public static void Main()\n  {\n    var shell = new Shell32.Shell();\n    var recycleBin = shell.NameSpace(10); \/\/ CSIDL_BITBUCKET\n    var items = recycleBin.Items();\n    for (var i = 0; i &lt; items.Count; i++) {\n      var item = (Shell32.FolderItem2)items.Item(i);\n      System.Console.WriteLine(item.Name);\n      System.Console.WriteLine(item.ExtendedProperty(\n                                    \"System.Recycle.DeletedFrom\"));\n      System.Console.WriteLine(item.ExtendedProperty(\n                                    \"System.Recycle.DateDeleted\"));\n      System.Console.WriteLine(item.Size);\n    }\n  }\n}\n<\/pre>\n<p>\nWe have to cast to\n<code>Shell32.Folder&shy;Item2<\/code> because the default interface\nfor the <code>Item()<\/code> method is\n<code>Shell32.Folder&shy;Item<\/code>,\nbut\n<code>Extended&shy;Property<\/code> is a method on\n<code>Shell32.Folder&shy;Item2<\/code>.\nWe didn&#8217;t have to do this explicit cast in JavaScript\nbecause JavaScript is a dynamically-typed language.\n<\/p>\n<p>\nSo let&#8217;s use the <code>dynamic<\/code> keyword to mimic that\nin C#.\nNote, however, that if you use <code>dynamic<\/code>, then you\nmiss out on a lot of IntelliSense features.\n<\/p>\n<pre>\nclass Program {\n  public static void Main()\n  {\n    var shell = new Shell32.Shell();\n    var recycleBin = shell.NameSpace(10); \/\/ CSIDL_BITBUCKET\n    var items = recycleBin.Items();\n    foreach (dynamic item in items) {\n      System.Console.WriteLine(item.Name);\n      System.Console.WriteLine(item.ExtendedProperty(\n                                    \"System.Recycle.DeletedFrom\"));\n      System.Console.WriteLine(item.ExtendedProperty(\n                                    \"System.Recycle.DateDeleted\"));\n      System.Console.WriteLine(item.Size);\n    }\n  }\n}\n<\/pre>\n<p>\nNow you can do things like list all the files deleted today\n<\/p>\n<pre>\nclass Program {\n  public static void Main()\n  {\n    var today = DateTime.Today;\n    var shell = new Shell32.Shell();\n    var recycleBin = shell.NameSpace(10); \/\/ CSIDL_BITBUCKET\n    var items = recycleBin.Items();\n    foreach (dynamic item in items) {\n      if (item.ExtendedProperty(\"System.Recycle.DateDeleted\").Date\n                                                       == today) {\n        System.Console.WriteLine(item.name);\n      }\n    }\n  }\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Today we&#8217;ll do a scripting version of an old C++ program: Printing information about the items in the Recycle Bin. (How you wish to act on the information is up to you.) This is a pattern we&#8217;ve seen a lot. Bind to a folder, enumerate its contents, extract properties. var shell = new ActiveXObject(&#8220;Shell.Application&#8221;); var [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-1183","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Today we&#8217;ll do a scripting version of an old C++ program: Printing information about the items in the Recycle Bin. (How you wish to act on the information is up to you.) This is a pattern we&#8217;ve seen a lot. Bind to a folder, enumerate its contents, extract properties. var shell = new ActiveXObject(&#8220;Shell.Application&#8221;); var [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/1183","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=1183"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/1183\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=1183"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=1183"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=1183"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}