KSum Posted July 8, 2008 Share Posted July 8, 2008 Hello all, I can't quite figure out why the following will not work for me. I am probably making a bad assumption or two (OK, 5 or 6...) Basically, I have a GUI form that I collect information in. It then allows for 2 possible actions: Process the information (rename files) Or show a list of the possible results. The second action, showing a list, does so via a second form. The second form is simply a list view, and 2 buttons. The first cancels this second form, the other processes the infromation. My problem is that the second form displays, but the buttons do nothing. I am trying to use the OnEvent mode and think having 2 forms with events may be causing the problem. I have bypassed the first form, straight to the second, and it worked. So I think it has to do with having 2 forms going, but can't figure out where I went astray. Can someone help out? Sorry for the long code. I tried pulling things out that wouldn't keep you from being able to run this. Regards, Karl expandcollapse popup#include <GuiConstants.au3> #include <GuiListView.au3> #include <File.au3> #Include <Array.au3> Global $filelist[1] Global $newfilelist[1] Global $SearchPath, $SearchFile, $ReplaceFile, $NewSize, $FileListForm AutoItSetOption("GUICoordMode", 1) ;1=absolute, 0=relative, 2=cell Opt("GUIOnEventMode", 1) ; Change to OnEvent mode ;Bypass the main form ;~ ShowResultsButton() ;~ Exit ;---------------------------------------------- ; Form Creation ;---------------------------------------------- ; Create a GUI $MainForm = GuiCreate("KCI's File Renamer", 560, 358) GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked", $MainForm) ;Top Instructions GuiCtrlCreateLabel("Rename Files based on a Search that includes an asterisk (*) to define zero or more characters.", 410, 10, 125, 50) ;---------------------------------------------- ;File Information ;---------------------------------------------- ;Labels GuiCtrlCreateLabel("Folder:", 320, 102) GuiCtrlCreateLabel("Search For:", 320, 167) GuiCtrlCreateLabel("Replace With:", 320, 192) $SearchPathInput = GUICtrlCreateInput("M:\0199909.CAD\Code\AutoIT", 400, 100, 125) $SearchFileInput = GUICtrlCreateInput("*Test*.*", 400, 165, 125) $ReplaceFileInput = GUICtrlCreateInput("*NewTest*.*", 400, 190, 125) ;Browse Button $BrowseButton = GUICtrlCreateButton("Browse for Search Folder", 425, 125, 80, 30, BitOr($BS_CENTER , $BS_MULTILINE)) GUICtrlSetOnEvent($BrowseButton, "BrowseButton") ;---------------------------------------------- ; BUTTONS ;---------------------------------------------- $ShowResultsButton = GUICtrlCreateButton ("Show Results", 350, 220, 150) $ReplaceButton = GUICtrlCreateButton ("Replace", 330, 260, 50) $CancelButton = GUICtrlCreateButton ( "Cancel", 400, 260, 50) $HelpButton = GUICtrlCreateButton ( "Help", 470, 260, 50) GUICtrlSetOnEvent($ReplaceButton, "ReplaceButton") GUICtrlSetOnEvent($CancelButton, "CancelButton") GUICtrlSetOnEvent($HelpButton, "HelpButton") GUICtrlSetOnEvent($ShowResultsButton, "ShowResultsButton") ;---------------------------------------------- ; DEFAULTS ;---------------------------------------------- ; GUI MESSAGE LOOP GuiSetState() While GuiGetMsg() <> $GUI_EVENT_CLOSE WEnd ;~ While 1 ;~ $msg = GUIGetMsg() ;~ Select ;~ Case $msg = $GUI_EVENT_CLOSE ;~ CLOSEClicked() ;~ Case $msg = $ReplaceButton ;~ ReplaceButton() ;~ ;~ Case $msg = $CancelButton ;~ CancelButton() ;~ Case $msg = $HelpButton ;~ HelpButton() ;~ Case $msg = $ShowResultsButton ;~ ShowResultsButton() ;~ EndSelect ;~ WEnd ;---------------------------------------------- ; USER FUNCTIONS ;---------------------------------------------- ;---------------------------------------------- ; Collect a list of the files to rename. ;---------------------------------------------- Func CollectRenameFiles() ; Make sure the search path contains a trailing backslash If StringRight( $SearchPath, 1) <> "\" Then $SearchPath = $SearchPath & "\" EndIf ; Create the proper Regular Expression syntax from the passed replacement string ; The search pattern starts with (?i) to make the search case-insensitive. If StringInStr ($SearchFile, ".") Then $RE_SearchPattern = StringReplace( $SearchFile, ".", "\.") EndIf $RE_SearchPattern = StringReplace( $RE_SearchPattern, "*", "(.*)") If StringRight ($RE_SearchPattern, 1) = "(" Then $RE_SearchPattern = StringTrimRight( $RE_SearchPattern, 1) EndIf $RE_SearchPattern = "(?i)" & $RE_SearchPattern ; Create the replacement string $RE_ReplacementString = $ReplaceFile $i = 0 While StringInStr ($RE_ReplacementString, "*") $i = $i + 1 $RE_ReplacementString = StringReplace( $RE_ReplacementString, "*", "\" & $i, 1) WEnd ; Get the files matching the passed criteria $tempfilelist = _FileListToArray($SearchPath, $SearchFile) ; If an error occured retrieving the file array, report it and exit If @Error > 0 Then Select Case @error = 1 MsgBox (0,"","Invalid File Path. " & $SearchPath) Exit Case @error = 2 MsgBox (0,"","Invalid File Filter. Currently only * is allowed. Your seach pattern was: " & $SearchFile) Exit Case @error = 3 MsgBox (0,"","No Files\Folders Found") Exit Case @error = 4 MsgBox (0,"","No files found matching your search criteria." & @CRLF & @TAB & "Path: " & $SearchPath & @CRLF & @TAB & "Search: " & $SearchFile) Exit EndSelect EndIf ; Files were retrieved, so work on them. $NewSize = UBound($tempfilelist) - 1 ReDim $newfilelist[$NewSize] ReDim $filelist[$NewSize] $filelist = $tempfilelist $newfilelist = $filelist For $i = 1 To $NewSize $newfilelist[$i] = StringRegExpReplace($filelist[$i], $RE_SearchPattern , $RE_ReplacementString) Next EndFunc ;---------------------------------------------- ; The Cancel Button was pressed. ;---------------------------------------------- Func CancelButton() Cleanup_Exit() EndFunc ;---------------------------------------------- ; The window was closed. ;---------------------------------------------- Func CLOSEClicked() Cleanup_Exit() EndFunc ;---------------------------------------------- ; The Install Button was pressed. ;---------------------------------------------- Func ReplaceButton() SetVariables() ;Rename the files CollectRenameFiles() GoRenameFiles() ;exit cleanly Cleanup_Exit() EndFunc ;---------------------------------------------- ; The Help Button was pressed. ;---------------------------------------------- Func HelpButton() EndFunc ;---------------------------------------------- ; The Browse Button was pressed. ;---------------------------------------------- Func BrowseButton() GUICtrlSetData ($SearchPathInput, FileSelectFolder("Choose a folder.", "", 6, GUICtrlRead($SearchPathInput))) EndFunc ;---------------------------------------------- ; The Show Results Button was pressed. ;---------------------------------------------- Func ShowResultsButton() ;~ SetVariables() $SearchPath = "M:\0199909.CAD\Code\AutoIT" $SearchFile = "*Test*.*" $ReplaceFile = "*NewTest*.*" ;Display the results if we were to rename the files CollectRenameFiles() ;~ $FileListForm = GuiCreate("KCI's File Renamer List", 560, 500, -1, -1, -1, -1, $MainForm) $FileListForm = GuiCreate("KCI's File Renamer List", 560, 500) Opt("GUIOnEventMode", 1) ; Change to OnEvent mode GUISetOnEvent($GUI_EVENT_CLOSE, "ListCLOSEClicked", $FileListForm) ;Create a List View to show the files Original Name and NEw Name $FileListView = GUICtrlCreateListView ("From|To", 1, 1, 500, 450) $FileCount = UBound ($filelist) Dim $ListFilesArray[$FileCount] For $i = 0 To $FileCount -1 $ListFilesArray[$i] = GUICtrlCreateListViewItem ($filelist[$i] & "|" & $newfilelist[$i], $FileListView) Next ;Create a button to complete the rename $ListReplaceButton = GUICtrlCreateButton ("Replace", 330, 450, 50) ;Create a button to close the form $ListCancelButton = GUICtrlCreateButton ( "Cancel", 400, 450, 50) GUICtrlSetOnEvent($ListReplaceButton, "ListReplaceButtonPressed") GUICtrlSetOnEvent($ListCancelButton, "ListCancelButtonPressed") ; GUI MESSAGE LOOP GuiSetState() While GuiGetMsg() <> $GUI_EVENT_CLOSE ;~ While 1 ;~ $msg = GUIGetMsg() ;~ Select ;~ Case $msg = $GUI_EVENT_CLOSE ;~ ListCLOSEClicked() ;~ Case $msg = $ListReplaceButton ;~ ListReplaceButtonPressed() ;~ ;~ Case $msg = $ListCancelButton ;~ ListCancelButtonPressed() ;~ EndSelect WEnd ListCleanup_Exit() EndFunc ;---------------------------------------------- ; The Cancel Button was pressed. ;---------------------------------------------- Func ListCancelButtonPressed() ListCleanup_Exit() EndFunc ;---------------------------------------------- ; The Replace Button was pressed. ;---------------------------------------------- Func ListReplaceButtonPressed() ListCleanup_Exit() GoRenameFiles() EndFunc ;---------------------------------------------- ; The window was closed. ;---------------------------------------------- Func ListCLOSEClicked() ListCleanup_Exit() EndFunc ;---------------------------------------------- ; Close the program after doing any necessary cleanup. ;---------------------------------------------- Func ListCleanup_Exit() GUIDelete ($FileListForm) EndFunc ;---------------------------------------------- ; The Install Button was pressed. ;---------------------------------------------- Func SetVariables() $SearchPath = GUICtrlRead ($SearchPathInput) $SearchFile = GUICtrlRead ($SearchFileInput) $ReplaceFile = GUICtrlRead ($ReplaceFileInput) EndFunc ;---------------------------------------------- ; Rename the Files. ;---------------------------------------------- Func GoRenameFiles() For $i = 1 To $NewSize FileMove($SearchPath & $filelist[$i], $SearchPath & $newfilelist[$i],8) Next EndFunc ;---------------------------------------------- ; Close the program after doing any necessary cleanup. ;---------------------------------------------- Func Cleanup_Exit() ;End the program Exit EndFunc Link to comment Share on other sites More sharing options...
Valuater Posted July 8, 2008 Share Posted July 8, 2008 You cannot mix "on-event" with "GUIGetMsg" expandcollapse popup#include <GuiConstants.au3> #include <GuiListView.au3> #include <File.au3> #include <Array.au3> #include <ButtonConstants.au3> Global $filelist[1] Global $newfilelist[1] Global $SearchPath, $SearchFile, $ReplaceFile, $NewSize, $FileListForm AutoItSetOption("GUICoordMode", 1) ;1=absolute, 0=relative, 2=cell Opt("GUIOnEventMode", 1) ; Change to OnEvent mode ;Bypass the main form ;~ ShowResultsButton() ;~ Exit ;---------------------------------------------- ; Form Creation ;---------------------------------------------- ; Create a GUI $MainForm = GUICreate("KCI's File Renamer", 560, 358) GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked", $MainForm) ;Top Instructions GUICtrlCreateLabel("Rename Files based on a Search that includes an asterisk (*) to define zero or more characters.", 410, 10, 125, 50) ;---------------------------------------------- ;File Information ;---------------------------------------------- ;Labels GUICtrlCreateLabel("Folder:", 320, 102) GUICtrlCreateLabel("Search For:", 320, 167) GUICtrlCreateLabel("Replace With:", 320, 192) $SearchPathInput = GUICtrlCreateInput("M:\0199909.CAD\Code\AutoIT", 400, 100, 125) $SearchFileInput = GUICtrlCreateInput("*Test*.*", 400, 165, 125) $ReplaceFileInput = GUICtrlCreateInput("*NewTest*.*", 400, 190, 125) ;Browse Button $BrowseButton = GUICtrlCreateButton("Browse for Search Folder", 425, 125, 80, 30, BitOR($BS_CENTER, $BS_MULTILINE)) GUICtrlSetOnEvent($BrowseButton, "BrowseButton") ;---------------------------------------------- ; BUTTONS ;---------------------------------------------- $ShowResultsButton = GUICtrlCreateButton("Show Results", 350, 220, 150) $ReplaceButton = GUICtrlCreateButton("Replace", 330, 260, 50) $CancelButton = GUICtrlCreateButton("Cancel", 400, 260, 50) $HelpButton = GUICtrlCreateButton("Help", 470, 260, 50) GUICtrlSetOnEvent($ReplaceButton, "ReplaceButton") GUICtrlSetOnEvent($CancelButton, "CancelButton") GUICtrlSetOnEvent($HelpButton, "HelpButton") GUICtrlSetOnEvent($ShowResultsButton, "ShowResultsButton") ;---------------------------------------------- ; DEFAULTS ;---------------------------------------------- ; GUI MESSAGE LOOP GUISetState() While 1 ;GuiGetMsg() <> $GUI_EVENT_CLOSE Sleep(50) WEnd ;~ While 1 ;~ $msg = GUIGetMsg() ;~ Select ;~ Case $msg = $GUI_EVENT_CLOSE ;~ CLOSEClicked() ;~ Case $msg = $ReplaceButton ;~ ReplaceButton() ;~ ;~ Case $msg = $CancelButton ;~ CancelButton() ;~ Case $msg = $HelpButton ;~ HelpButton() ;~ Case $msg = $ShowResultsButton ;~ ShowResultsButton() ;~ EndSelect ;~ WEnd ;---------------------------------------------- ; USER FUNCTIONS ;---------------------------------------------- ;---------------------------------------------- ; Collect a list of the files to rename. ;---------------------------------------------- Func CollectRenameFiles() ; Make sure the search path contains a trailing backslash If StringRight($SearchPath, 1) <> "\" Then $SearchPath = $SearchPath & "\" EndIf ; Create the proper Regular Expression syntax from the passed replacement string ; The search pattern starts with (?i) to make the search case-insensitive. If StringInStr($SearchFile, ".") Then $RE_SearchPattern = StringReplace($SearchFile, ".", "\.") EndIf $RE_SearchPattern = StringReplace($RE_SearchPattern, "*", "(.*)") If StringRight($RE_SearchPattern, 1) = "(" Then $RE_SearchPattern = StringTrimRight($RE_SearchPattern, 1) EndIf $RE_SearchPattern = "(?i)" & $RE_SearchPattern ; Create the replacement string $RE_ReplacementString = $ReplaceFile $i = 0 While StringInStr($RE_ReplacementString, "*") $i = $i + 1 $RE_ReplacementString = StringReplace($RE_ReplacementString, "*", "\" & $i, 1) WEnd ; Get the files matching the passed criteria $tempfilelist = _FileListToArray($SearchPath, $SearchFile) ; If an error occured retrieving the file array, report it and exit If @error > 0 Then Select Case @error = 1 MsgBox(0, "", "Invalid File Path. " & $SearchPath) Exit Case @error = 2 MsgBox(0, "", "Invalid File Filter. Currently only * is allowed. Your seach pattern was: " & $SearchFile) Exit Case @error = 3 MsgBox(0, "", "No Files\Folders Found") Exit Case @error = 4 MsgBox(0, "", "No files found matching your search criteria." & @CRLF & @TAB & "Path: " & $SearchPath & @CRLF & @TAB & "Search: " & $SearchFile) Exit EndSelect EndIf ; Files were retrieved, so work on them. $NewSize = UBound($tempfilelist) - 1 ReDim $newfilelist[$NewSize] ReDim $filelist[$NewSize] $filelist = $tempfilelist $newfilelist = $filelist For $i = 1 To $NewSize $newfilelist[$i] = StringRegExpReplace($filelist[$i], $RE_SearchPattern, $RE_ReplacementString) Next EndFunc ;==>CollectRenameFiles ;---------------------------------------------- ; The Cancel Button was pressed. ;---------------------------------------------- Func CancelButton() Cleanup_Exit() EndFunc ;==>CancelButton ;---------------------------------------------- ; The window was closed. ;---------------------------------------------- Func CLOSEClicked() Cleanup_Exit() EndFunc ;==>CLOSEClicked ;---------------------------------------------- ; The Install Button was pressed. ;---------------------------------------------- Func ReplaceButton() SetVariables() ;Rename the files CollectRenameFiles() GoRenameFiles() ;exit cleanly Cleanup_Exit() EndFunc ;==>ReplaceButton ;---------------------------------------------- ; The Help Button was pressed. ;---------------------------------------------- Func HelpButton() EndFunc ;==>HelpButton ;---------------------------------------------- ; The Browse Button was pressed. ;---------------------------------------------- Func BrowseButton() GUICtrlSetData($SearchPathInput, FileSelectFolder("Choose a folder.", "", 6, GUICtrlRead($SearchPathInput))) EndFunc ;==>BrowseButton ;---------------------------------------------- ; The Show Results Button was pressed. ;---------------------------------------------- Func ShowResultsButton() ;~ SetVariables() $SearchPath = "M:\0199909.CAD\Code\AutoIT" $SearchFile = "*Test*.*" $ReplaceFile = "*NewTest*.*" ;Display the results if we were to rename the files CollectRenameFiles() ;~ $FileListForm = GuiCreate("KCI's File Renamer List", 560, 500, -1, -1, -1, -1, $MainForm) $FileListForm = GUICreate("KCI's File Renamer List", 560, 500) ; ************ Opt("GUIOnEventMode", 1) ; Change to OnEvent mode ******************* already done at the top of the script GUISetOnEvent($GUI_EVENT_CLOSE, "ListCLOSEClicked", $FileListForm) ;Create a List View to show the files Original Name and NEw Name $FileListView = GUICtrlCreateListView("From|To", 1, 1, 500, 450) $FileCount = UBound($filelist) Dim $ListFilesArray[$FileCount] For $i = 0 To $FileCount - 1 $ListFilesArray[$i] = GUICtrlCreateListViewItem($filelist[$i] & "|" & $newfilelist[$i], $FileListView) Next ;Create a button to complete the rename $ListReplaceButton = GUICtrlCreateButton("Replace", 330, 450, 50) ;Create a button to close the form $ListCancelButton = GUICtrlCreateButton("Cancel", 400, 450, 50) GUICtrlSetOnEvent($ListReplaceButton, "ListReplaceButtonPressed") GUICtrlSetOnEvent($ListCancelButton, "ListCancelButtonPressed") ; GUI MESSAGE LOOP GUISetState() ;While GuiGetMsg() <> $GUI_EVENT_CLOSE While 1 ;~ $msg = GUIGetMsg() ;~ Select ;~ Case $msg = $GUI_EVENT_CLOSE ;~ ListCLOSEClicked() ;~ Case $msg = $ListReplaceButton ;~ ListReplaceButtonPressed() ;~ ;~ Case $msg = $ListCancelButton ;~ ListCancelButtonPressed() ;~ EndSelect Sleep(50) WEnd ListCleanup_Exit() EndFunc ;==>ShowResultsButton ;---------------------------------------------- ; The Cancel Button was pressed. ;---------------------------------------------- Func ListCancelButtonPressed() ListCleanup_Exit() EndFunc ;==>ListCancelButtonPressed ;---------------------------------------------- ; The Replace Button was pressed. ;---------------------------------------------- Func ListReplaceButtonPressed() ListCleanup_Exit() GoRenameFiles() EndFunc ;==>ListReplaceButtonPressed ;---------------------------------------------- ; The window was closed. ;---------------------------------------------- Func ListCLOSEClicked() ListCleanup_Exit() EndFunc ;==>ListCLOSEClicked ;---------------------------------------------- ; Close the program after doing any necessary cleanup. ;---------------------------------------------- Func ListCleanup_Exit() GUIDelete($FileListForm) EndFunc ;==>ListCleanup_Exit ;---------------------------------------------- ; The Install Button was pressed. ;---------------------------------------------- Func SetVariables() $SearchPath = GUICtrlRead($SearchPathInput) $SearchFile = GUICtrlRead($SearchFileInput) $ReplaceFile = GUICtrlRead($ReplaceFileInput) EndFunc ;==>SetVariables ;---------------------------------------------- ; Rename the Files. ;---------------------------------------------- Func GoRenameFiles() For $i = 1 To $NewSize FileMove($SearchPath & $filelist[$i], $SearchPath & $newfilelist[$i], 8) Next EndFunc ;==>GoRenameFiles ;---------------------------------------------- ; Close the program after doing any necessary cleanup. ;---------------------------------------------- Func Cleanup_Exit() ;End the program Exit EndFunc ;==>Cleanup_Exit 8) Link to comment Share on other sites More sharing options...
KSum Posted July 8, 2008 Author Share Posted July 8, 2008 Valuater, Thank you for the reply. I tried changing the While loops and even cut the code you sent. Does it work on your end? The second form still does not allow the buttons to work. They look like they are pressed, visually, but nothing happens. I threw Message Boxes in the functions that should have been called. It appears the functions (such as ListCancelButtonPressed) are not being run. I have to exit the program manually. Any ideas? Karl Link to comment Share on other sites More sharing options...
Valuater Posted July 9, 2008 Share Posted July 9, 2008 well, the second GUI was created inside a function and was still "inside" that function when trying to get the response from a button. The code below leaves the function and works properly expandcollapse popup#include <GuiConstants.au3> #include <GuiListView.au3> #include <File.au3> #include <Array.au3> #include <ButtonConstants.au3> Global $filelist[1] Global $newfilelist[1] Global $SearchPath, $SearchFile, $ReplaceFile, $NewSize, $FileListForm AutoItSetOption("GUICoordMode", 1) ;1=absolute, 0=relative, 2=cell Opt("GUIOnEventMode", 1) ; Change to OnEvent mode ;Bypass the main form ;~ ShowResultsButton() ;~ Exit ;---------------------------------------------- ; Form Creation ;---------------------------------------------- ; Create a GUI $MainForm = GUICreate("KCI's File Renamer", 560, 358) GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked", $MainForm) ;Top Instructions GUICtrlCreateLabel("Rename Files based on a Search that includes an asterisk (*) to define zero or more characters.", 410, 10, 125, 50) ;---------------------------------------------- ;File Information ;---------------------------------------------- ;Labels GUICtrlCreateLabel("Folder:", 320, 102) GUICtrlCreateLabel("Search For:", 320, 167) GUICtrlCreateLabel("Replace With:", 320, 192) $SearchPathInput = GUICtrlCreateInput("M:\0199909.CAD\Code\AutoIT", 400, 100, 125) $SearchFileInput = GUICtrlCreateInput("*Test*.*", 400, 165, 125) $ReplaceFileInput = GUICtrlCreateInput("*NewTest*.*", 400, 190, 125) ;Browse Button $BrowseButton = GUICtrlCreateButton("Browse for Search Folder", 425, 125, 80, 30, BitOR($BS_CENTER, $BS_MULTILINE)) GUICtrlSetOnEvent($BrowseButton, "BrowseButton") ;---------------------------------------------- ; BUTTONS ;---------------------------------------------- $ShowResultsButton = GUICtrlCreateButton("Show Results", 350, 220, 150) $ReplaceButton = GUICtrlCreateButton("Replace", 330, 260, 50) $CancelButton = GUICtrlCreateButton("Cancel", 400, 260, 50) $HelpButton = GUICtrlCreateButton("Help", 470, 260, 50) GUICtrlSetOnEvent($ReplaceButton, "ReplaceButton") GUICtrlSetOnEvent($CancelButton, "CancelButton") GUICtrlSetOnEvent($HelpButton, "HelpButton") GUICtrlSetOnEvent($ShowResultsButton, "ShowResultsButton") ;---------------------------------------------- ; DEFAULTS ;---------------------------------------------- ; GUI MESSAGE LOOP GUISetState() While 1 ;GuiGetMsg() <> $GUI_EVENT_CLOSE Sleep(50) WEnd ;~ While 1 ;~ $msg = GUIGetMsg() ;~ Select ;~ Case $msg = $GUI_EVENT_CLOSE ;~ CLOSEClicked() ;~ Case $msg = $ReplaceButton ;~ ReplaceButton() ;~ ;~ Case $msg = $CancelButton ;~ CancelButton() ;~ Case $msg = $HelpButton ;~ HelpButton() ;~ Case $msg = $ShowResultsButton ;~ ShowResultsButton() ;~ EndSelect ;~ WEnd ;---------------------------------------------- ; USER FUNCTIONS ;---------------------------------------------- ;---------------------------------------------- ; Collect a list of the files to rename. ;---------------------------------------------- Func CollectRenameFiles() ; Make sure the search path contains a trailing backslash If StringRight($SearchPath, 1) <> "\" Then $SearchPath = $SearchPath & "\" EndIf ; Create the proper Regular Expression syntax from the passed replacement string ; The search pattern starts with (?i) to make the search case-insensitive. If StringInStr($SearchFile, ".") Then $RE_SearchPattern = StringReplace($SearchFile, ".", "\.") EndIf $RE_SearchPattern = StringReplace($RE_SearchPattern, "*", "(.*)") If StringRight($RE_SearchPattern, 1) = "(" Then $RE_SearchPattern = StringTrimRight($RE_SearchPattern, 1) EndIf $RE_SearchPattern = "(?i)" & $RE_SearchPattern ; Create the replacement string $RE_ReplacementString = $ReplaceFile $i = 0 While StringInStr($RE_ReplacementString, "*") $i = $i + 1 $RE_ReplacementString = StringReplace($RE_ReplacementString, "*", "\" & $i, 1) WEnd ; Get the files matching the passed criteria $tempfilelist = _FileListToArray($SearchPath, $SearchFile) ; If an error occured retrieving the file array, report it and exit If @error > 0 Then Select Case @error = 1 MsgBox(0, "", "Invalid File Path. " & $SearchPath) Exit Case @error = 2 MsgBox(0, "", "Invalid File Filter. Currently only * is allowed. Your seach pattern was: " & $SearchFile) Exit Case @error = 3 MsgBox(0, "", "No Files\Folders Found") Exit Case @error = 4 MsgBox(0, "", "No files found matching your search criteria." & @CRLF & @TAB & "Path: " & $SearchPath & @CRLF & @TAB & "Search: " & $SearchFile) Exit EndSelect EndIf ; Files were retrieved, so work on them. $NewSize = UBound($tempfilelist) - 1 ReDim $newfilelist[$NewSize] ReDim $filelist[$NewSize] $filelist = $tempfilelist $newfilelist = $filelist For $i = 1 To $NewSize $newfilelist[$i] = StringRegExpReplace($filelist[$i], $RE_SearchPattern, $RE_ReplacementString) Next EndFunc ;==>CollectRenameFiles ;---------------------------------------------- ; The Cancel Button was pressed. ;---------------------------------------------- Func CancelButton() Cleanup_Exit() EndFunc ;==>CancelButton ;---------------------------------------------- ; The window was closed. ;---------------------------------------------- Func CLOSEClicked() Cleanup_Exit() EndFunc ;==>CLOSEClicked ;---------------------------------------------- ; The Install Button was pressed. ;---------------------------------------------- Func ReplaceButton() SetVariables() ;Rename the files CollectRenameFiles() GoRenameFiles() ;exit cleanly Cleanup_Exit() EndFunc ;==>ReplaceButton ;---------------------------------------------- ; The Help Button was pressed. ;---------------------------------------------- Func HelpButton() EndFunc ;==>HelpButton ;---------------------------------------------- ; The Browse Button was pressed. ;---------------------------------------------- Func BrowseButton() GUICtrlSetData($SearchPathInput, FileSelectFolder("Choose a folder.", "", 6, GUICtrlRead($SearchPathInput))) EndFunc ;==>BrowseButton ;---------------------------------------------- ; The Show Results Button was pressed. ;---------------------------------------------- Func ShowResultsButton() SetVariables() ;$SearchPath = "M:\0199909.CAD\Code\AutoIT" ;$SearchFile = "*Test*.*" ;$ReplaceFile = "*NewTest*.*" ;Display the results if we were to rename the files CollectRenameFiles() ;~ $FileListForm = GuiCreate("KCI's File Renamer List", 560, 500, -1, -1, -1, -1, $MainForm) $FileListForm = GUICreate("KCI's File Renamer List", 560, 500) ; ************ Opt("GUIOnEventMode", 1) ; Change to OnEvent mode ******************* already done at the top of the script GUISetOnEvent($GUI_EVENT_CLOSE, "ListCLOSEClicked", $FileListForm) ;Create a List View to show the files Original Name and NEw Name $FileListView = GUICtrlCreateListView("From|To", 1, 1, 500, 450) $FileCount = UBound($filelist) Dim $ListFilesArray[$FileCount] For $i = 0 To $FileCount - 1 $ListFilesArray[$i] = GUICtrlCreateListViewItem($filelist[$i] & "|" & $newfilelist[$i], $FileListView) Next ;Create a button to complete the rename $ListReplaceButton = GUICtrlCreateButton("Replace", 330, 450, 50) ;Create a button to close the form $ListCancelButton = GUICtrlCreateButton("Cancel", 400, 450, 50) GUICtrlSetOnEvent($ListReplaceButton, "ListReplaceButtonPressed") GUICtrlSetOnEvent($ListCancelButton, "ListCancelButtonPressed") ; GUI MESSAGE LOOP GUISetState() ;While GuiGetMsg() <> $GUI_EVENT_CLOSE ;While 1 ;~ $msg = GUIGetMsg() ;~ Select ;~ Case $msg = $GUI_EVENT_CLOSE ;~ ListCLOSEClicked() ;~ Case $msg = $ListReplaceButton ;~ ListReplaceButtonPressed() ;~ ;~ Case $msg = $ListCancelButton ;~ ListCancelButtonPressed() ;~ EndSelect ; Sleep(50) ;WEnd ;ListCleanup_Exit() EndFunc ;==>ShowResultsButton ;---------------------------------------------- ; The Cancel Button was pressed. ;---------------------------------------------- Func ListCancelButtonPressed() ListCleanup_Exit() EndFunc ;==>ListCancelButtonPressed ;---------------------------------------------- ; The Replace Button was pressed. ;---------------------------------------------- Func ListReplaceButtonPressed() ListCleanup_Exit() GoRenameFiles() EndFunc ;==>ListReplaceButtonPressed ;---------------------------------------------- ; The window was closed. ;---------------------------------------------- Func ListCLOSEClicked() ListCleanup_Exit() EndFunc ;==>ListCLOSEClicked ;---------------------------------------------- ; Close the program after doing any necessary cleanup. ;---------------------------------------------- Func ListCleanup_Exit() GUIDelete($FileListForm) EndFunc ;==>ListCleanup_Exit ;---------------------------------------------- ; The Install Button was pressed. ;---------------------------------------------- Func SetVariables() $SearchPath = GUICtrlRead($SearchPathInput) $SearchFile = GUICtrlRead($SearchFileInput) $ReplaceFile = GUICtrlRead($ReplaceFileInput) EndFunc ;==>SetVariables ;---------------------------------------------- ; Rename the Files. ;---------------------------------------------- Func GoRenameFiles() For $i = 1 To $NewSize FileMove($SearchPath & $filelist[$i], $SearchPath & $newfilelist[$i], 8) Next EndFunc ;==>GoRenameFiles ;---------------------------------------------- ; Close the program after doing any necessary cleanup. ;---------------------------------------------- Func Cleanup_Exit() ;End the program Exit EndFunc ;==>Cleanup_Exit 8) Link to comment Share on other sites More sharing options...
KSum Posted July 9, 2008 Author Share Posted July 9, 2008 Perfect! The Whie Loop is not needed for the second GUI, just use the first. The OnEvent handles both GUI controls simultaneously as opposed to beinfg suspended for one while the other is runing. Got it! Thank you! Karl Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now