Jump to content

Move file to Recycle Bin ( not the easy way )


Terenz
 Share

Recommended Posts

Hello guys,

I need to put a file to the trash :D

This file is on a network path so if i'll delete the file was delete permenently, without going to the recycle. So i have say, "I'll do it in autoit, how hard it can be?" Well is hard, at least for me

What i have try:

a ) FileMove, not work. On XP ( also on 7, with a different name but for now i'll check on this os ) there is a folder for the recycle in:

C:RECYCLERMany number-letter

So i have try to move the file in this folder, nothing

b ) FileRecycle, not work

c ) SHFileOperationW, the same

Maybe someone has a better idea, my ( not working ) code:

Global Const $FO_DELETE = 0x0003
Global Const $FOF_ALLOWUNDO = 0x0040
Global Const $FOF_NOCONFIRMATION = 0x0010
Global Const $FOF_NOCONFIRMMKDIR = 0x0200
Global Const $FOF_NOERRORUI = 0x0400
Global Const $FOF_SILENT = 0x0004
Global Const $FOF_NO_UI = BitOR($FOF_NOCONFIRMATION, $FOF_NOCONFIRMMKDIR, $FOF_NOERRORUI, $FOF_SILENT)
Global Const $tagSHFILEOPSTRUCT = 'hwnd hWnd;uint Func;ptr From;ptr To;dword Flags;int fAnyOperationsAborted;ptr hNameMappings;ptr ProgressTitle;'

Local $Search, $RecycleBin

$MyFile = FileOpen(@ScriptDir & "\Test.txt", 2)
FileClose($MyFile)

If FileExists(@HomeDrive & "\RECYCLER") Then
    $Search = FileFindFirstFile(@HomeDrive & "\RECYCLER\*.*")
    If $Search <> -1 Then
        $RecycleBin = @HomeDrive & "\RECYCLER\" & FileFindNextFile($Search)
        ConsoleWrite("RECYCLER: " & $RecycleBin & @CRLF)
;~      FileRecycle(@ScriptDir & "\Test.txt")
;~      FileMove(@ScriptDir & "\Test.txt", $RecycleBin)
;~      _WinAPI_ShellFileOperation(@ScriptDir & "\Test.txt", '', $FO_DELETE, BitOR($FOF_ALLOWUNDO, $FOF_NO_UI))
    EndIf
EndIf

Func _WinAPI_ShellFileOperation($sFrom, $sTo, $iFunc, $iFlags, $sTitle = '', $hParent = 0)
    Local $tFrom, $tTo, $Data
    If Not IsArray($sFrom) Then
        $Data = $sFrom
        Dim $sFrom[1] = [$Data]
    EndIf
    $tFrom = _WinAPI_ArrayToStruct($sFrom)
    If @error Then
        Return SetError(-1, 0, 0)
    EndIf
    If Not IsArray($sTo) Then
        $Data = $sTo
        Dim $sTo[1] = [$Data]
    EndIf
    $tTo = _WinAPI_ArrayToStruct($sTo)
    If @error Then
        Return SetError(-1, 0, 0)
    EndIf
    Local $tSHFILEOPSTRUCT = DllStructCreate($tagSHFILEOPSTRUCT)
    DllStructSetData($tSHFILEOPSTRUCT, 'hWnd', $hParent)
    DllStructSetData($tSHFILEOPSTRUCT, 'Func', $iFunc)
    DllStructSetData($tSHFILEOPSTRUCT, 'From', DllStructGetPtr($tFrom))
    DllStructSetData($tSHFILEOPSTRUCT, 'To', DllStructGetPtr($tTo))
    DllStructSetData($tSHFILEOPSTRUCT, 'Flags', $iFlags)
    DllStructSetData($tSHFILEOPSTRUCT, 'ProgressTitle', $sTitle)
    Local $Ret = DllCall('shell32.dll', 'int', 'SHFileOperationW', 'ptr', DllStructGetPtr($tSHFILEOPSTRUCT))
    If @error Then
        Return SetError(-1, 0, 0)
    Else
        If $Ret[0] Then
            Return SetError($Ret[0], 0, 0)
        EndIf
    EndIf
    Return $tSHFILEOPSTRUCT
EndFunc   ;==>_WinAPI_ShellFileOperation

Func _WinAPI_ArrayToStruct(Const ByRef $aData, $iStart = 0, $iEnd = -1)
    If Not IsArray($aData) Then
        Return SetError(2, 0, 0)
    EndIf
    Local $tData, $Count, $Struct = ''
    If $iStart < 0 Then
        $iStart = 0
    EndIf
    If ($iEnd < 0) Or ($iEnd > UBound($aData) - 1) Then
        $iEnd = UBound($aData) - 1
    EndIf
    For $i = $iStart To $iEnd
        $Struct &= 'wchar[' & (StringLen($aData[$i]) + 1) & '];'
    Next
    $tData = DllStructCreate($Struct & 'wchar[1]')
    If @error Then
        Return SetError(1, 0, 0)
    EndIf
    $Count = 1
    For $i = $iStart To $iEnd
        DllStructSetData($tData, $Count, $aData[$i])
        $Count += 1
    Next
    DllStructSetData($tData, $Count, ChrW(0))
    Return $tData
EndFunc   ;==>_WinAPI_ArrayToStruct

Thanks for any help

Edited by Terenz

Nothing is so strong as gentleness. Nothing is so gentle as real strength

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...