Jump to content

help with this code


Recommended Posts

below is code I have pulled from another script that involves a GUI. I thought I pulled everything I needed. But it is not working. It changes a file name as such.

before

AR123 Smith Bob v1 Full Mix.wav

after

AR123 Smith Bob Full Mix v1.wav

is there some kind soul out there can help me figure out what is wrong. Thank you so much. see code below.

#include <GUIConstantsEx.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <Array.au3>
#include <File.au3>



Local $oErrorHandler = ObjEvent("AutoIt.Error", "_ComErrFunc")

            $aWinList=WinList("[REGEXPCLASS:^(Explore|Cabinet)WClass$]")

            For $i = 1 To $aWinList[0][0]
            ; Get base folder and selections
            $aSelection = _ExplorerWinGetSelectedItems($aWinList[$i][1])

            ; Display as strings
            ConsoleWrite("Explorer Window: " & $aSelection[1] & " Selection(s)" & @CRLF)
            For $j = 2 To $aSelection[0] + 1
            ConsoleWrite(@TAB & $aSelection[$j] & @CRLF)
         Next


          ; START SHAWN's CODE____________________________
         ; repeat with the selected items
         For $File_Name = 1 To $aSelection
         $result = StringInStr($File_Name, "mix",0,1)
         If $result > 0 Then
            $filename2 = StringInStr ( $File_Name, "\" , 0 , -1) ; (last "\" is 47 chars in from righy)
            $the_folder_path = StringLeft ( $File_Name, $filename2 ) ; C:\Documents and Settings\sebrad\Desktop\shawn\
            $file_path_len = StringLen($File_Name) ; 79 total characters
            $char_in_file_name = $file_path_len - $filename2 ;31 char in file name only
            $orig_file_name = StringRight ($File_Name, $char_in_file_name) ;AR123 Smith, Bob v1 FULL MIX.txt
            $new_file_name = StringRegExpReplace($orig_file_name, "(?i)(.*)(\sv.*)(\s.*\sMIX)(\.wav)", "$1$3$2$4") ;AR123 Smith, Bob FULL MIX v1.txt
            FileCopy ( $File_Name, ($the_folder_path & $new_file_name), 1 ) ; 1 = with overwright
            FileDelete ( $the_folder_path & $orig_file_name)
         EndIf

         Next
         ; END SHAWN's CODE________________________

         Next
         ; end button 7























; ==========================================================================================================================

; Func _ObjectSHFolderViewFromWin($hWnd)
;
; Returns an 'ShellFolderView' Object for the given Window handle
;
; Author: Ascend4nt, based on code by KaFu, klaus.s
; ==========================================================================================================================

Func _ObjectSHFolderViewFromWin($hWnd)
    If Not IsHWnd($hWnd) Then Return SetError(1,0,0)
    Local $oShell,$oShellWindows,$oIEObject,$oSHFolderView

    ; Shell Object
    $oShell=ObjCreate("Shell.Application")
    If Not IsObj($oShell) Then Return SetError(2,0,0)

;   Get a 'ShellWindows Collection' object
    $oShellWindows = $oShell.Windows()
    If Not IsObj($oShellWindows) Then Return SetError(3,0,0)

;   Iterate through the collection - each of type 'InternetExplorer' Object

    For $oIEObject In $oShellWindows
        If $oIEObject.HWND = $hWnd Then
            ; InternetExplorer->Document = ShellFolderView object
            $oSHFolderView=$oIEObject.Document
            If IsObj($oSHFolderView) Then Return $oSHFolderView
            Return SetError(4,0,0)
        EndIf
    Next

    Return SetError(-1,0,0)
EndFunc

; ==========================================================================================================================
; Func _ExplorerWinGetSelectedItems($hWnd)
;
;
; Author: klaus.s, KaFu, Ascend4nt (consolidation & cleanup, Path name simplification)
; ==========================================================================================================================

Func _ExplorerWinGetSelectedItems($hWnd)
    If Not IsHWnd($hWnd) Then Return SetError(1,0,'')
    Local $oSHFolderView
    Local $iSelectedItems,$iCounter=2,$aSelectedItems[2] = [0, ""]

    $oSHFolderView=_ObjectSHFolderViewFromWin($hWnd)
    If @error Then Return SetError(@error,0,'')

;   SelectedItems = FolderItems Collection object->Count
    $iSelectedItems = $oSHFolderView.SelectedItems.Count

    Dim $aSelectedItems[$iSelectedItems+2]  ; 2 extra -> 1 for count [0], 1 for Folder Path [1]

    $aSelectedItems[0]=$iSelectedItems
;   ShellFolderView->Folder->Self as 'FolderItem'->Path
    $aSelectedItems[1]=$oSHFolderView.Folder.Self.Path

;   ShellFolderView->SelectedItems = FolderItems Collection object
    $oSelectedFolderItems = $oSHFolderView.SelectedItems

#cs
    ; For ALL items in an Explorer window (not just the selected ones):
    $oSelectedFolderItems = $oSHFolderView.Folder.Items
    ReDim $aSelectedItems[$oSelectedFolderItems.Count+2]
#ce

    For $oFolderItem In $oSelectedFolderItems
        $aSelectedItems[$iCounter] = $oFolderItem.Path
        $iCounter += 1
    Next

    Return SetExtended($iCounter-2,$aSelectedItems)
EndFunc   ;==>_ExplorerWinGetSelectedItems

Func _ComErrFunc($oError)
    ConsoleWrite("COM Error occurred:"  & @CRLF & _
        "Number: " & @TAB & $oError.number & @CRLF & _
        "Windescription:" & @TAB & $oError.windescription & @CRLF & _
        "Description is: " & @TAB & $oError.description & @CRLF & _
        "Source is: " & @TAB & $oError.source & @CRLF & _
        "Helpfile is: " & @TAB & $oError.helpfile & @CRLF & _
        "Helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _
        "Lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _
        "Scriptline is: " & @TAB & $oError.scriptline & @CRLF & _
        "Retcode is: " & @TAB & $oError.retcode & @CRLF & @CRLF)
EndFunc   ;==>_ComErrFunc
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...