Jump to content

Open windows explorer with items already highlighted


Go to solution Solved by ahmet,

Recommended Posts

Hello all.  So, I am modifying a script I wrote a long time ago which cleans up duplicate files and assists in removing out of date files in a user's downloads folder (determines if a file is a duplicate or different version of same file based on whether "(n)" is in the file name where "n" is a digit).  The script has a gui that populates a list view control with the duplicate items in the users downloads folder, and one of the features I am trying to implement is to integrate a context menu item for the list view items which would open windows explorer with the list view item already highlighted in windows explorer.  I did some research and found this method on how to accomplish that, it does work when running the command from the windows run prompt.  It also works when used in a simple autoit script:

$text = FileOpenDialog ( "Select a file", "", "All (*.*)", 3 )
If StringIsSpace ( $text ) Then
    Exit
EndIf

Run ( 'explorer.exe /select,"' & $text & '"' )

giphy.gif

However, for the life of me, I can not get the above code to work when it is run in response to a GUI event.  When I attempt to run the above code in response to a context menu control event, it crashes windows explorer:

giphy.gif

Below is the code for the script that I am working on:

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_UseX64=y
#AutoIt3Wrapper_Res_SaveSource=y
#AutoIt3Wrapper_Res_Language=1033

#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
; *** Start added by AutoIt3Wrapper ***

; *** End added by AutoIt3Wrapper ***
#cs ----------------------------------------------------------------------------

    AutoIt Version: 3.3.15.0 (Beta)
    Author:         myName

    Script Function:
    Template AutoIt script.

#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here
#include <File.au3>
#include <Date.au3>
#include <Misc.au3>
#include <Array.au3>
#include <Constants.au3>
#include <Date.au3>
#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <WindowsConstants.au3>
#include <ButtonConstants.au3>
#include <FileConstants.au3>
#include <GuiListView.au3>
#include <_FileGetProperty.au3>
#include <Crypt.au3>
#include <GuiListViewex.au3>
#include <WinAPIConv.au3>
#RequireAdmin
Opt("GUIResizeMode", $GUI_DOCKAUTO)
Local $hDLL = DllOpen("user32.dll")
Global $vData, $sMsg, $aLV_List_Left, $aRet, $iEditMode = 0

;First, loop through all files in Downloads folder and automatically remove any files with (n) in the file name whose calculated hash matches the original file (without "(n)"

;Exit script if no duplicate items found
$files = _FileListToArrayRec(@UserProfileDir & "\Downloads", "*(?)*", $FLTAR_FILES)

If @error Then
    SetError(0)
    ProgressOff()
    DllClose($hDLL)
    MsgBox($MB_OK + $MB_ICONHAND, "No duplicate files", 'There were no files in your downloads folder which appear to be duplicates (determined by the presence of "(n)" at the end of the file name, where "n" is a digit).  Try running this tool again when there are duplicate files in your downloads folder.')
    Exit
Else
    _Crypt_Startup()
    For $i = 1 To $files[0] Step 1
        ProgressSet ( ( $i / $files[0] ) * 100, $files[$i], "Removing Duplioates" )
        $newhash = _Crypt_HashFile(@UserProfileDir & "\Downloads\" & $files[$i], $CALG_SHA_256)
        $spli1 = StringSplit($files[$i], "(")
        If $spli1[0] > 2 Then
            SetError(3)
        EndIf
        ;If statement contains My current workaround for this functionality.  Would like to replace: not effective
        If @error Then
            DllClose($hDLL)
            MsgBox($MB_OK + $MB_ICONHAND, "error", "There is a file with multiple sets of parentheses." & @CRLF & $files[$i])
            ShellExecute("explorer.exe", @UserProfileDir & "\Downloads", @SystemDir)
            Sleep(4000)
            Send($files[$i], 0)
            Exit
        EndIf
        ;$spli1 = StringSplit ( _GetFilename(@UserProfileDir & "\Downloads\" & $files[$i]) & "." & $ext, "(" )
        $spli2 = StringSplit($spli1[2], ")")
        $newname = StringStripWS($spli1[1], 3) & StringStripWS($spli2[2], 3)
        If FileExists(@UserProfileDir & "\Downloads\" & $newname) Then
            $oldhash = _Crypt_HashFile(@UserProfileDir & "\Downloads\" & $newname, $CALG_SHA_256)
            If $newhash = $oldhash Then
                $newtime = FileGetTime(@UserProfileDir & "\Downloads\" & $files[$i])
                $oldtime = FileGetTime(@UserProfileDir & "\Downloads\" & $newname)
                $encnewtime = _Date_Time_EncodeFileTime($newtime[1], $newtime[2], $newtime[0], $newtime[3], $newtime[4], $newtime[5])
                $encoldtime = _Date_Time_EncodeFileTime($oldtime[1], $oldtime[2], $oldtime[0], $oldtime[3], $oldtime[4], $oldtime[5])
                $res = _Date_Time_CompareFileTime($encnewtime, $encoldtime)
                If $res = 1 Then
                    Do
                        FileDelete(@UserProfileDir & "\Downloads\" & $newname)
                    Until Not FileExists(@UserProfileDir & "\Downloads\" & $newname)
                    Do
                        FileMove(@UserProfileDir & "\Downloads\" & $files[$i], @UserProfileDir & "\Downloads\" & $newname)
                    Until Not FileExists(@UserProfileDir & "\Downloads\" & $files[$i])
                Else
                    Do
                        FileDelete(@UserProfileDir & "\Downloads\" & $files[$i])
                    Until Not FileExists(@UserProfileDir & "\Downloads\" & $files[$i])
                EndIf
                ContinueLoop
            EndIf
        EndIf
    Next
    ProgressOff ()
    _Crypt_Shutdown()
    ;Now that all the duplicates whose hash values match the original file's hash are gone, now we loop through all files in downloads again to grab all which have
    ;the same name as an existing file (resulting in the "(n)", but whose file hash does not match the original file.  We will populate a list view with both duplicate
    ;and original files and allow user verify each of the files, manipulate the files, and batch process.
    ProgressOn("Gathering file list", "Please Wait", "Please Wait", Default, Default, 18)
    $files = _FileListToArrayRec(@UserProfileDir & "\Downloads", "*(?)*", $FLTAR_FILES)
    _ArrayDelete($files, 0)
    Global $arr[UBound($files)][6]
    For $i = 0 To UBound($files) - 1 Step 1
        ProgressSet ( ( ( $i + 1 ) / UBound ( $files ) ) * 100, $files[$i], "Gathering file info" )
        ;$props = _FileGetProperty ( @UserProfileDir & "\Downloads\" & $files[$i] )
        $arr[$i][0] = $files[$i]
        $arr[$i][1] = _FileGetProperty(@UserProfileDir & "\Downloads\" & $files[$i], "Size")
        ;$thesize = FileGetSize(@UserProfileDir & "\Downloads\" & $files[$i])




        $oldsize = FileGetSize(@UserProfileDir & "\Downloads\" & $files[$i])
        $ext = _GetFilenameExt(@UserProfileDir & "\Downloads\" & $files[$i])
        ConsoleWrite(_GetFilename(@UserProfileDir & "\Downloads\" & $files[$i]) & "." & $ext & @CRLF)
        $spli1 = StringSplit($files[$i], "(")
        If $spli1[0] > 2 Then
            SetError(3)
        EndIf
        If @error Then
            DllClose($hDLL)
            MsgBox($MB_OK + $MB_ICONHAND, "error", "There is a file with multiple sets of parentheses." & @CRLF & $files[$i])
            ShellExecute("explorer.exe", @UserProfileDir & "\Downloads", @SystemDir)
            Sleep(4000)
            Send($files[$i], 0)
            Exit
        EndIf
        ;$spli1 = StringSplit ( _GetFilename(@UserProfileDir & "\Downloads\" & $files[$i]) & "." & $ext, "(" )
        $spli2 = StringSplit($spli1[2], ")")
        $newname = StringStripWS($spli1[1], 3) & StringStripWS($spli2[2], 3)
        $sea = _ArraySearch($arr, $newname, 0, 0, 0, 0, 1, 1)
        If @error Then
            SetError(0)
            If FileExists(@UserProfileDir & "\Downloads\" & $newname) Then


                $newsize = FileGetSize(@UserProfileDir & "\Downloads\" & $newname)
                $diff = Null
                If $oldsize > $newsize Then
                    $diff = $oldsize - $newsize
                Else
                    $diff = $newsize - $oldsize
                EndIf
                $text = Null
                Switch $diff
                    Case 0
                        $text = "0"
                    Case 1 To 1023
                        $text = $diff & " bytes"
                    Case 1024 To 1048575
                        $divide = Round($diff / 1024, 2)
                        $text = $divide & " KB"
                    Case Else
                        $divide = Round($diff / 1048576, 2)
                        $text = $divide & " MB"
                EndSwitch
                $arr[$i][2] = $text
                $arr[$i][3] = _FileGetProperty(@UserProfileDir & "\Downloads\" & $files[$i], "File description")
                $arr[$i][4] = _FileGetProperty(@UserProfileDir & "\Downloads\" & $files[$i], "Product name")
                $arr[$i][5] = _FileGetProperty(@UserProfileDir & "\Downloads\" & $files[$i], "Product version")
                If @error Then
                    SetError(0)
                    $arr[$i][5] = _FileGetProperty(@UserProfileDir & "\Downloads\" & $files[$i], "File version")
                EndIf
                $newver = _FileGetProperty(@UserProfileDir & "\Downloads\" & $newname, "Product version")
                If @error Then
                    SetError(0)
                    $newver = _FileGetProperty(@UserProfileDir & "\Downloads\" & $newname, "File version")
                EndIf
                _ArrayAdd($arr, $newname & "|" & _FileGetProperty(@UserProfileDir & "\Downloads\" & $newname, "Size") & "|" & $text & "|" & _FileGetProperty(@UserProfileDir & "\Downloads\" & $newname, "File description") & "|" & _FileGetProperty(@UserProfileDir & "\Downloads\" & $newname, "Product name") & "|" & $newver)
            Else
                ContinueLoop
            EndIf
        Else
            ContinueLoop
        EndIf
    Next

    _ArraySort($arr)
    ProgressOff()
    $Form1 = GUICreate("Duplicate Downloads", 800, 407, 190, 141, BitOR($GUI_SS_DEFAULT_GUI, $WS_SIZEBOX, $WS_THICKFRAME, $DS_SETFOREGROUND))
    $ListView1 = GUICtrlCreateListView("File Name|Size|Size Difference|Description|Product Name|Version", 26, 32, 752, 329, BitOR($LVS_REPORT, $LVS_SHOWSELALWAYS), BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT))
    GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 160)
    GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 1, 70)
    GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 2, 70)
    GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 3, 200)
    GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 4, 160)
    _GUICtrlListView_AddArray($ListView1, $arr)
    $iLV_Left_Index = _GUIListViewEx_Init($ListView1) ;, $listarr )
    _GUIListViewEx_SetEditStatus($iLV_Left_Index, 0)
    $ListView1context = GUICtrlCreateContextMenu($ListView1)
    $MenuItem1 = GUICtrlCreateMenuItem("Open/launch file(s)", $ListView1context)
    $MenuItem2 = GUICtrlCreateMenuItem("Open in explorer", $ListView1context)
    $MenuItem3 = GUICtrlCreateMenuItem("Delete item(s)", $ListView1context)
    $Button1 = GUICtrlCreateButton("Process", 325, 368, 89, 33)
    GUICtrlSetCursor(-1, 0)
    GUISetState()

    _GUIListViewEx_MsgRegister()

    Local $nMsg
    While 1
        If _IsPressed("2E", $hDLL) Then
            While _IsPressed("2E", $hDLL)
                Sleep(50)
            WEnd
            $stri = "Are you sure you want to delete the following files?" & @CRLF
            $in2 = _GUICtrlListView_GetSelectedIndices($ListView1, True)
            Local $filearray[UBound($in2) - 1]
            For $ty = 1 To $in2[0] Step 1
                $filearray[$ty - 1] = _GUICtrlListView_GetItemText($ListView1, $in2[$ty])
                $stri = $stri & _GUICtrlListView_GetItemText($ListView1, $in2[$ty]) & @CRLF
            Next
            If Not IsDeclared("iMsgBoxAnswer") Then Local $iMsgBoxAnswer
            $iMsgBoxAnswer = MsgBox($MB_YESNO + $MB_DEFBUTTON2 + $MB_ICONQUESTION, "Confirm", $stri)
            If $iMsgBoxAnswer = $IDYES Then
                _GUICtrlListView_BeginUpdate($ListView1)
                $filearray = _ArrayUnique($filearray)
                For $ty = 0 To UBound($filearray) - 1 Step 1
                    Do
                        FileDelete(@UserProfileDir & "\Downloads\" & $filearray[$ty])
                    Until Not FileExists(@UserProfileDir & "\Downloads\" & $filearray[$ty])
                    $temp = ""
                    For $gg = 0 To UBound($arr) - 1 Step 1
                        If $arr[$gg][0] = $filearray[$ty] Then
                            $temp = $temp & $gg & ";"
                        Else
                            ContinueLoop
                        EndIf
                    Next
                    _ArrayDelete($arr, StringTrimRight($temp, 1))
                Next


                _GUICtrlListView_DeleteAllItems($ListView1)
                _GUICtrlListView_AddArray($ListView1, $arr)
                _GUICtrlListView_EndUpdate($ListView1)
            EndIf
        EndIf



        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_PRIMARYDOWN
                ToolTip("")
            Case $GUI_EVENT_CLOSE
                DllClose($hDLL)
                Exit


            Case $MenuItem1
                $index = _GUICtrlListView_GetSelectedIndices($ListView1, True)
                For $i = 1 To $index[0] Step 1
                    $filename = _GUICtrlListView_GetItemText($ListView1, $index[$i])
                    If StringRight($filename, 3) = "exe" Then
                        ShellExecute("Start.exe", '/wait "' & @UserProfileDir & '\Downloads\' & $filename & '"', "C:\Program Files\Sandboxie")
                    ElseIf StringRight($filename, 3) = "msi" Then
                        ShellExecute("Start.exe", '/wait msiexec.exe /i "' & @UserProfileDir & '\Downloads\' & $filename & '"', "C:\Program Files\Sandboxie")
                    Else
                        ShellExecute($filename, "", @UserProfileDir & "\Downloads")
                    EndIf
                Next
;The below menu item contains the code which is not working
            Case $MenuItem2
                If _GUICtrlListView_GetSelectedCount($ListView1) > 1 Then
                    $pos = MouseGetPos()
                    If Not IsDeclared("sToolTipAnswer") Then Local $sToolTipAnswer
                    $sToolTipAnswer = ToolTip("Only one item can be selected when using this function.", $pos[0], $pos[1], "error", 3, 1)
                Else
                    $index = _GUICtrlListView_GetSelectedIndices($ListView1, True)
                    $text = _GUICtrlListView_GetItemText($ListView1, $index[1])
                    MsgBox ( 1, "", 'explorer.exe /select,"' & @UserProfileDir & '\Downloads\' & $text & '"' )
                    ; Right below here.  And I have tried leaving out the "@Comspec & ' /c " part as well.
                    Run ( @ComSpec & ' /c explorer.exe /select,"' & @UserProfileDir & '\Downloads\' & $text & '"' )
                EndIf

            Case $MenuItem3
                $stri = "Are you sure you want to delete the following files?" & @CRLF
                $in2 = _GUICtrlListView_GetSelectedIndices($ListView1, True)
                Local $filearray[UBound($in2) - 1]
                For $ty = 1 To $in2[0] Step 1
                    $filearray[$ty - 1] = _GUICtrlListView_GetItemText($ListView1, $in2[$ty])
                    $stri = $stri & _GUICtrlListView_GetItemText($ListView1, $in2[$ty]) & @CRLF
                Next
                If Not IsDeclared("iMsgBoxAnswer") Then Local $iMsgBoxAnswer
                $iMsgBoxAnswer = MsgBox($MB_YESNO + $MB_DEFBUTTON2 + $MB_ICONQUESTION, "Confirm", $stri)
                If $iMsgBoxAnswer = $IDYES Then
                    _GUICtrlListView_BeginUpdate($ListView1)
                    $filearray = _ArrayUnique($filearray)
                    For $ty = 0 To UBound($filearray) - 1 Step 1
                        Do
                            FileDelete(@UserProfileDir & "\Downloads\" & $filearray[$ty])
                        Until Not FileExists(@UserProfileDir & "\Downloads\" & $filearray[$ty])
                        $temp = ""
                        For $gg = 0 To UBound($arr) - 1 Step 1
                            If $arr[$gg][0] = $filearray[$ty] Then
                                $temp = $temp & $gg & ";"
                            Else
                                ContinueLoop
                            EndIf
                        Next
                        _ArrayDelete($arr, StringTrimRight($temp, 1))
                    Next


                    _GUICtrlListView_DeleteAllItems($ListView1)
                    _GUICtrlListView_AddArray($ListView1, $arr)
                    _GUICtrlListView_EndUpdate($ListView1)
                EndIf
            Case $Button1
                $st = 0
                GUISetState(@SW_HIDE, $Form1)
                ProgressOn("Gathering file list", "Please Wait", "Please Wait", Default, Default, 18)
                For $i = 0 To UBound($arr) - 1 Step 1
                    ProgressSet(($i / UBound($arr)) * 100, _GetFilename(@UserProfileDir & "\Downloads\" & $arr[$i][0]), "Moving Files")
                    $ext = _GetFilenameExt(@UserProfileDir & "\Downloads\" & $arr[$i][0])
                    $pa = StringInStr($arr[$i][0], "(")
                    If $pa = 0 Then
                        ContinueLoop
                    Else
                        $spli1 = StringSplit(_GetFilename(@UserProfileDir & "\Downloads\" & $arr[$i][0]) & "." & $ext, "(")
                        If @error Then
                            SetError(0)
                            ContinueLoop
                        Else
                            $spli2 = StringSplit($spli1[2], ")")
                            $newname = StringStripWS($spli1[1], 3) & StringStripWS($spli2[2], 3)
                        EndIf
                        If $arr[$i][0] = $newname Then
                            ContinueLoop
                        EndIf

                        If Not FileExists(@UserProfileDir & "\Downloads\" & $newname) Then
                            Do
                                FileMove(@UserProfileDir & "\Downloads\" & $arr[$i][0], @UserProfileDir & "\Downloads\" & $newname)
                            Until FileExists(@UserProfileDir & "\Downloads\" & $newname)

                        Else
                            $dup = FileGetTime(@UserProfileDir & "\Downloads\" & $arr[$i][0])
                            $one = $dup[0] & "/" & $dup[1] & "/" & $dup[2] & " " & $dup[3] & ":" & $dup[4] & ":" & $dup[5]
                            $ol = FileGetTime(@UserProfileDir & "\Downloads\" & $newname)
                            $two = $ol[0] & "/" & $ol[1] & "/" & $ol[2] & " " & $ol[3] & ":" & $ol[4] & ":" & $ol[5]
                            $comp1 = _DateDiff("n", $one, _NowCalc())
                            $comp2 = _DateDiff("n", $two, _NowCalc())
                            If $comp1 > $comp2 Then

                                Do
                                    FileDelete(@UserProfileDir & "\Downloads\" & $arr[$i][0])
                                Until Not FileExists(@UserProfileDir & "\Downloads\" & $arr[$i][0])

                                ;MsgBox ( 1, "", $arr[$i][0] & " is older than " & $newname)
                            Else
                                FileMove(@UserProfileDir & "\Downloads\" & $arr[$i][0], @UserProfileDir & "\Downloads\" & $newname, 9)
                                ;MsgBox ( 1, "", $newname & " is older than " & $arr[$i][0])
                            EndIf
                        EndIf

                    EndIf
                Next
                DllClose($hDLL)
                Exit

        EndSwitch
        $vRet = _GUIListViewEx_EventMonitor()
        If @error Then
            MsgBox($MB_SYSTEMMODAL, "Error", "Event error: " & @error)
        EndIf
        Switch @extended
            Case 1
                If $vRet <> "" Then
                    $before = $vRet[1][2]
                    $after = $vRet[1][3]
                    If FileExists(@UserProfileDir & "\Downloads\" & $after) Then
                        If Not IsDeclared("iMsgBoxAnswer") Then Local $iMsgBoxAnswer
                        $iMsgBoxAnswer = MsgBox($MB_YESNO + $MB_ICONQUESTION, "Continue?", "The file name you have selected already exists.  Do you want to overwrite this file?")
                        If $iMsgBoxAnswer = $IDYES Then
                            Local $holarr[UBound($arr) - 1][3]
                            ;$sub = 0
                            For $n = 0 To UBound($arr) - 1 Step 1
                                If $n < $vRet[1][0] Then
                                    ;If $arr[$n][0] = $after Then

                                    $holarr[$n][0] = $arr[$n][0]
                                    $holarr[$n][1] = $arr[$n][1]
                                    $holarr[$n][2] = $arr[$n][2]
                                ElseIf $n > $vRet[1][0] Then
                                    $holarr[$n - 1][0] = $arr[$n][0]
                                    $holarr[$n - 1][1] = $arr[$n][1]
                                    $holarr[$n - 1][2] = $arr[$n][2]
                                Else
                                    ContinueLoop
                                EndIf
                            Next
                            $arr = $holarr
                            _GUICtrlListView_BeginUpdate($ListView1)
                            _GUICtrlListView_DeleteAllItems($ListView1)
                            _GUICtrlListView_AddArray($ListView1, $arr)
                            _GUICtrlListView_EndUpdate($ListView1)
                            If FileExists(@UserProfileDir & "\Downloads\" & $before) Then
                                Do
                                    FileMove(@UserProfileDir & "\Downloads\" & $before, @UserProfileDir & "\Downloads\" & $after, $FC_OVERWRITE)
                                Until FileExists(@UserProfileDir & "\Downloads\" & $after) And Not FileExists(@UserProfileDir & "\Downloads\" & $before)
                            EndIf
                        EndIf
                    Else
                        Local $holarr[UBound($arr) - 1][3]
                        ;$sub = 0
                        For $n = 0 To UBound($arr) - 1 Step 1
                            If $n < $vRet[1][0] Then
                                ;If $arr[$n][0] = $after Then

                                $holarr[$n][0] = $arr[$n][0]
                                $holarr[$n][1] = $arr[$n][1]
                                $holarr[$n][2] = $arr[$n][2]
                            ElseIf $n > $vRet[1][0] Then
                                $holarr[$n - 1][0] = $arr[$n][0]
                                $holarr[$n - 1][1] = $arr[$n][1]
                                $holarr[$n - 1][2] = $arr[$n][2]
                            Else
                                ContinueLoop
                            EndIf
                        Next
                        $arr = $holarr
                        _GUICtrlListView_BeginUpdate($ListView1)
                        _GUICtrlListView_DeleteAllItems($ListView1)
                        _GUICtrlListView_AddArray($ListView1, $arr)
                        _GUICtrlListView_EndUpdate($ListView1)
                        If FileExists(@UserProfileDir & "\Downloads\" & $before) Then
                            Do
                                FileMove(@UserProfileDir & "\Downloads\" & $before, @UserProfileDir & "\Downloads\" & $after, $FC_OVERWRITE)
                            Until FileExists(@UserProfileDir & "\Downloads\" & $after) And Not FileExists(@UserProfileDir & "\Downloads\" & $before)
                        EndIf
                    EndIf


                EndIf


        EndSwitch

    WEnd




EndIf

Func _GetFilename($sFilePath)
    Local $oWMIService = ObjGet("winmgmts:{impersonationLevel = impersonate}!\\" & "." & "\root\cimv2")
    Local $oColFiles = $oWMIService.ExecQuery("Select * From CIM_Datafile Where Name = '" & StringReplace ( StringReplace ( $sFilePath, "\", "\\"), "'", "\'" ) & "'")
    If IsObj($oColFiles) Then
        For $oObjectFile In $oColFiles
            Return $oObjectFile.FileName
        Next
    EndIf
    Return SetError(1, 1, 0)
EndFunc   ;==>_GetFilename

Func _GetFilenameExt($sFilePath)
    Local $oWMIService = ObjGet("winmgmts:{impersonationLevel = impersonate}!\\" & "." & "\root\cimv2")
    Local $oColFiles = $oWMIService.ExecQuery("Select * From CIM_Datafile Where Name = '" & StringReplace ( StringReplace ( $sFilePath, "\", "\\"), "'", "\'" ) & "'")
    If IsObj($oColFiles) Then
        For $oObjectFile In $oColFiles
            Return $oObjectFile.Extension
        Next
    EndIf
    Return SetError(1, 1, 0)
EndFunc   ;==>_GetFilenameExt

Func _GetFilenameInt($sFilePath)
    Local $oWMIService = ObjGet("winmgmts:{impersonationLevel = impersonate}!\\" & "." & "\root\cimv2")
    Local $oColFiles = $oWMIService.ExecQuery("Select * From CIM_Datafile Where Name = '" & StringReplace ( StringReplace ( $sFilePath, "\", "\\"), "'", "\'" ) & "'")
    If IsObj($oColFiles) Then
        For $oObjectFile In $oColFiles
            Return $oObjectFile.Name
        Next
    EndIf
    Return SetError(1, 1, 0)
EndFunc   ;==>_GetFilenameInt

Func _GetFilenameDrive($sFilePath)
    Local $oWMIService = ObjGet("winmgmts:{impersonationLevel = impersonate}!\\" & "." & "\root\cimv2")
    Local $oColFiles = $oWMIService.ExecQuery("Select * From CIM_Datafile Where Name = '" & StringReplace ( StringReplace ( $sFilePath, "\", "\\"), "'", "\'" ) & "'")
    If IsObj($oColFiles) Then
        For $oObjectFile In $oColFiles
            Return StringUpper($oObjectFile.Drive)
        Next
    EndIf
    Return SetError(1, 1, 0)
EndFunc   ;==>_GetFilenameDrive

Func _GetFilenamePath($sFilePath)
    Local $oWMIService = ObjGet("winmgmts:{impersonationLevel = impersonate}!\\" & "." & "\root\cimv2")
    Local $oColFiles = $oWMIService.ExecQuery("Select * From CIM_Datafile Where Name = '" & StringReplace ( StringReplace ( $sFilePath, "\", "\\"), "'", "\'" ) & "'")
    If IsObj($oColFiles) Then
        For $oObjectFile In $oColFiles
            Return $oObjectFile.Path
        Next
    EndIf
    Return SetError(1, 1, 0)
EndFunc   ;==>_GetFilenamePath

The attached zip also contains the script (named "cleanup_downloads), along with any autoit udfs that are not included in the base autoit installation.  The offending menu item action definition is for $menuitem2 located on lines 268-280.  I would really appreciate it if someone could help me understand why the very first snippette of code I included in this post works when run by itself, but when run as part of my script, it causes windows explorer to close and restart.  Thank you in advance.

cleanup_downloads.zip

Edited by MattHiggs
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...