BradV
(Seasoned Scripter)
2009-06-24 01:59 PM
visual basic help

Does anyone know what:

 Code:
Const RECYCLE_BIN = &Ha&


is setting the constant to?


BradV
(Seasoned Scripter)
2009-06-24 02:09 PM
Re: visual basic help

It's setting it to 10 isn't it?

BradV
(Seasoned Scripter)
2009-06-24 02:37 PM
Re: visual basic help

OK, I was prompted by the empty recycle bin question in the basic forum. I didn't like that he was using a batch file to delete the entire bin and then remake it. So, I did a quick search and found:

 Code:
A simple script to clear the recycle bin
===
Set fso = CreateObject("Scripting.FileSystemObject")
Const RECYCLE_BIN = &Ha&
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.NameSpace(RECYCLE_BIN)
Set objFolderItems = objFolder.Items
For i = 0 to objFolderItems.Count - 1
    fso.DeleteFile objFolderItems.Item(i).Path, 1
Next
=== 


I translated that as:

 Code:
Dim $objFSO, $intRB, $objShell, $objFolder, $objFoldItm, $i
$objFSO = CreateObject("Scripting.FileSystemObject")
$intRB = 10
$objShell = CreateObject("Shell.Application")
$objFolder = $objShell.NameSpace($intRB)
$objFoldItm = $objFolder.Items
For $i = 0 To $objFoldItm.Count-1
   $objFSO.DeleteFile $objFoldItm.Item($i).Path,1
Next


Everything seems to work up until the delete line. Any thoughts?


Mart
(KiX Supporter)
2009-06-24 03:18 PM
Re: visual basic help

Made one small change to the delete line and now it works great. Tested it on WinXPPro SP3 and kix 4.60 and it works as expected.

Might be usefull as a UDF. EmtyRecycleBin() or something.

 Code:
Dim $objFSO, $intRB, $objShell, $objFolder, $objFoldItm, $i
$objFSO = CreateObject("Scripting.FileSystemObject")
$intRB = 10
$objShell = CreateObject("Shell.Application")
$objFolder = $objShell.NameSpace($intRB)
$objFoldItm = $objFolder.Items
For $i = 0 to $objFoldItm.Count - 1
	$objFSO.DeleteFile($objFoldItm.Item($i).Path, 1)
Next


JochenAdministrator
(KiX Supporter)
2009-06-24 03:28 PM
Re: visual basic help

doesn't delete Folders in the recycle bin...

this one does, but doesn't distinguish between files and folders.
Allthough it seems a bit brute it doesn't choke \:\)

 Code:
Dim $objFSO, $intRB, $objShell, $objFolder, $objFoldItm, $i
$objFSO = CreateObject("Scripting.FileSystemObject")
$intRB = 10
$objShell = CreateObject("Shell.Application")
$objFolder = $objShell.NameSpace($intRB)
$objFoldItm = $objFolder.Items
For $i = 0 to $objFoldItm.Count - 1
	$objFSO.DeleteFile($objFoldItm.Item($i).Path, 1)
	$objFSO.DeleteFolder($objFoldItm.Item($i).Path, 1)
Next


I may come up with a better way later (or may not ;\) )


JochenAdministrator
(KiX Supporter)
2009-06-24 03:31 PM
Re: visual basic help

.Type seemed to be promising, but I get Type results in German, so it is a nogo

BradV
(Seasoned Scripter)
2009-06-24 03:48 PM
Re: visual basic help

A UDF was what I was thinking about. I just think deleting the entire recycle bin and then re-creating it eventually is going to get you in trouble.

Mart
(KiX Supporter)
2009-06-24 04:09 PM
Re: visual basic help

 Originally Posted By: Jochen

doesn't delete Folders in the recycle bin...
....


D#mn , didn't test with folder just file.


Mart
(KiX Supporter)
2009-06-24 04:11 PM
Re: visual basic help

 Originally Posted By: BradV

....
I just think deleting the entire recycle bin and then re-creating it eventually is going to get you in trouble.


Agreed.


BradV
(Seasoned Scripter)
2009-06-24 04:40 PM
Re: visual basic help

OK, I think this is pretty close to a final solution:

 Code:
Dim $objFSO, $intRB, $objShell, $objFolder, $objFoldItm, $i
$objFSO = CreateObject("Scripting.FileSystemObject")
$intRB = 10
$objShell = CreateObject("Shell.Application")
$objFolder = $objShell.NameSpace($intRB)
$objFoldItm = $objFolder.Items
For $i = 0 to $objFoldItm.Count - 1
   If $objFSO.FileExists($objFoldItm.Item($i).Path)
      $objFSO.DeleteFile($objFoldItm.Item($i).Path, 1)
   Else
      If $objFSO.FolderExists($objFoldItm.Item($i).Path)
         $objFSO.DeleteFolder($objFoldItm.Item($i).Path, 1)
      Endif
   Endif
Next


JochenAdministrator
(KiX Supporter)
2009-06-24 04:51 PM
Re: visual basic help

Very nice!

the search for distinguishing files and folders based on item properties was a dead end \:\(

Here as a function a bit shorted (I think if it is not a file it must be a folder (at least in the bin) )

 Code:
function EmptyRecycleBin()

dim $objFSO, $objShell, $objFolder, $objFoldItm, $i
$objFSO = createobject("Scripting.FileSystemObject")
$objShell = createobject("Shell.Application")
$objFolder = $objShell.NameSpace(10)
$objFoldItm = $objFolder.Items
for $i = 0 to $objFoldItm.Count - 1
   if $objFSO.FileExists($objFoldItm.Item($i).Path)
      $objFSO.DeleteFile($objFoldItm.Item($i).Path, 1)
   else
      $objFSO.DeleteFolder($objFoldItm.Item($i).Path, 1)
   endif
next

endfunction


sufficient?


AllenAdministrator
(KiX Supporter)
2009-06-24 05:13 PM
Re: visual basic help

Amazing this has UDF has not existed before now.

I do see it has been discussed some 4 years ago with Jens coming up with a different solution.

http://www.kixtart.org/forums/ubbthreads...true#Post112135


BradV
(Seasoned Scripter)
2009-06-24 05:22 PM
Re: visual basic help

OK, put it in the UDF forum. Let me know if I made any typos!

Empty Recycle Bin UDF


Mart
(KiX Supporter)
2009-06-25 11:04 AM
Re: visual basic help

A bit late but an optional parameter to indicate what to delete would be nice.
Something like 1 for just folders and 2 for just file. If none is given then just delete everything (default).

I have no direct use for it but can see some in the future.


BradV
(Seasoned Scripter)
2009-06-25 12:45 PM
Re: visual basic help

Yes, I was also thinking someone might only want to delete things older than x (a parameter passed) days, or something else. The basis is there.