|
Each contribution is licensed to you under a License Agreement by its owner, not Microsoft. Microsoft does not guarantee the contribution or purport to grant rights to it.
|
Categories |
Send a File to the Recycle Bin(Community)
Script Code
VBScript
' Discard files to the recycle bin
' Not intended for use on exe files
' Not intended for use on Win98 or earlier systems
' Steve Yandl
strFileToToss = "C:\Test\trash.txt"
Const HKEY_CURRENT_USER = &H80000001
strComputer = "."
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FileExists(strFileToToss) Then
WScript.Quit
End If
If fso.GetExtensionName(strFileToToss) = "exe" Then
WScript.Quit
End If
strFolderParent = fso.GetParentFolderName(strFileToToss)
strFileName = fso.GetFileName(strFileToToss)
' Make sure recycle bin properties are set to NOT display request for delete confirmation
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\default:StdRegProv")
strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Explorer"
strValueName = "ShellState"
oReg.GetBinaryValue HKEY_CURRENT_USER,strKeyPath, _
strValueName,strValue
strOrigBinSet = strValue(4)
strValue(4) = 39
errReturnA = oReg.SetBinaryValue _
(HKEY_CURRENT_USER, strKeyPath, strValueName, strValue)
' Use the Shell to send the file to the recycle bin
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(strFolderParent)
Set objFolderItem = objFolder.ParseName(strFileName)
objFolderItem.InvokeVerb("&Delete")
' Restore the User's Property settings for the Recycle Bin
strValue(4) = strOrigBinSet
errReturnB = oReg.SetBinaryValue _
(HKEY_CURRENT_USER, strKeyPath, strValueName, strValue)
Platforms
For online peer support, join
The Official Scripting Guys Forum!
To provide feedback or report bugs in sample scripts, please start a new discussion on the Discussions tab for this script.
Disclaimer
The sample scripts are not supported under any Microsoft standard support program or service. The sample scripts are provided AS IS without warranty of any kind. Microsoft further disclaims all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of the sample scripts and documentation remains with you. In no event shall Microsoft, its authors, or anyone else involved in the creation, production, or delivery of the scripts be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the sample scripts or documentation, even if Microsoft has been advised of the possibility of such damages.
Be the first to create a discussion.
|