No Dialog Find, Find Previous, Find Next, and Replace
I download therefore I rename.
I'm forever making minor name changes to the files that I download from the interwebs. Usually, I need to replace several "_" or "." characters. Many times the title is all upper/lower case and I want to capitalize the title.
A rename program for one file is overkill so I was looking for something else to help me quickly make minor changes to a single file name. I noticed that when making changes to a file in Windows Explorer, an Edit control is shown so I thought, "The
Edit Library would be perfect for this!"
When making changes to a file name in Windows Explorer, the Edit control shown is a self-contained dialog. If any other windows or dialogs are shown, the Edit control is hidden/closed. Adding a dialog to enter Find and Replace information is out of the question. A "No Dialog" Find/Replace command is what is needed.
This script provide the following no-dialog commands:
- Find (Ctrl+Win+Alt+F). To use:
- Press hotkey.
- Enter search text. Press Enter to start the search.
A few notes:
- The search text is not displayed (echoed) as you are typing so enter carefully. The Backspace key can be used to erase incorrect characters.
- The search text is not case sensitive.
- Use the Escape key to abort at any time.
- Find Next (Ctrl+Win+Alt+F3) and Find Previous (Ctrl+Win+Alt+F2). These commands use the last search string entered by the Find command.
- Replace (Ctrl+Win+Alt+H). To use:
- Select working text. This step will limit the Replace command to only the text that is selected.
- Press hotkey.
- Enter the search and replace text. The Enter key indicates the end of input. Press Enter at the end of the search text and at the end the replace text.
A few notes:
- The search/replace text is not displayed (echoed) as you are typing so enter carefully. The Backspace key can be used to erase incorrect characters.
- The search text is not case sensitive but the replace text is used as typed.
- Use the Escape key to abort at any time.
- If needed, use Undo (Ctrl+Z) to reverse the change.
- By design, this is a "Replace All" command. If you only want to replace a specific number of occurrences of the search string, be sure to only select the desired target text. For example, to replace all "." characters in a file name, select all but the last node of the file (Ex: Select everything except the ".pdf" at the end).
For example, to replace "red" with "blue" (sans quotes), do the following:
- Select the text that includes the "red" string.
- Press the Replace hotkey: Ctrl+Win+Alt+H
- Type the following: red{Enter}blue{Enter}
- If needed, use Undo (Ctrl+Z) to reverse the change.
- Convert Case. The following commands are available (select the desired text before using):
- Lowercase (Ctrl+Win+Alt+L).
- Uppercase (Ctrl+Win+Alt+U).
- Capitalize (Ctrl+Win+Alt+C). I use this one a lot.
- Toggle case (Ctrl+Win+Alt+T).
Here's the code (needs the
Edit Library of course):
Code:
;[================]
;[ Convert Case ]
;[================]
^#!c:: ;-- Capitalize
^#!l:: ;-- Lowercase
^#!t:: ;-- Toggle case
^#!u:: ;-- Uppercase
;-- Bounce if an Edit control is not active
if not Edit_GetActiveHandles(hEdit,hWindow,True)
return
;-- Convert
Edit_ConvertCase(hEdit,SubStr(A_ThisHotKey,0))
return
;[===============]
;[ Find ]
;[ (No Dialog) ]
;[===============]
^#!f::
NoDialogFind:
;-- Bounce if an Edit control is not active
if not Edit_GetActiveHandles(hEdit,hWindow,True)
return
;-- Collect FindWhat string
Input $FindWhat,T9,{Enter}{Escape}
if (ErrorLevel<>"EndKey:Enter") or StrLen($FindWhat)=0
{
SoundPlay *16 ;-- Error
return
}
;-- Set $FindFlags
$FindFlags:="d"
;-- Find it
NoDialogFind_Part2: ;-- Used by the Find Previous and Find Next shortcuts
;-- Collect current positions
Edit_GetSel(hEdit,$StartSelPos,$EndSelPos)
;-- Which direction?
if $FindFlags Contains d
$FindPos:=Edit_FindText(hEdit,$FindWhat,$EndSelPos,-1)
else
$FindPos:=Edit_FindText(hEdit,$FindWhat,$StartSelPos,0)
;-- Not found?
if ($FindPos=-1)
{
SoundPlay *-1 ;-- Default beep
return
}
;-- Select it
Edit_SetSel(hEdit,$FindPos,$FindPos+StrLen($FindWhat))
;-- Make sure caret is showing
Edit_ScrollCaret(hEdit)
return
;[=================]
;[ Find Previous ]
;[ (No Dialog) ]
;[=================]
^#!F2::
NoDialogFindPrevious:
;-- Bounce if an Edit control is not active
if not Edit_GetActiveHandles(hEdit,hWindow,True)
return
;-- Bounce if Find was never called
if StrLen($FindWhat)=0
return
;-- Set $FindFlags
$FindFlags:=""
;-- Find it
gosub NoDialogFind_Part2
return
;[===============]
;[ Find Next ]
;[ (No Dialog) ]
;[===============]
^#!F3::
NoDialogFindNext:
;-- Bounce if an Edit control is not active
if not Edit_GetActiveHandles(hEdit,hWindow,True)
return
;-- Bounce if Find was never called
if StrLen($FindWhat)=0
return
;-- Set $FindFlags
$FindFlags:="d"
;-- Find it
gosub NoDialogFind_Part2
return
;[===============]
;[ Replace ]
;[ (No Dialog) ]
;[===============]
^#!h::
NoDialogReplace:
;-- Bounce if an Edit control is not active
if not Edit_GetActiveHandles(hEdit,hWindow,True)
return
;-- Collect current select postions
Edit_GetSel(hEdit,$StartSelPos,$EndSelPos)
if ($StartSelPos=$EndSelPos) ;-- Nothing selected
{
SoundPlay *16 ;-- Error
return
}
;-- Collect FindWhat string
Input $FindWhat,T9,{Enter}{Escape}
if (ErrorLevel<>"EndKey:Enter") or StrLen($FindWhat)=0
{
SoundPlay *16 ;-- Error
return
}
;-- Collect ReplaceWith string
Input $ReplaceWith,T9,{Enter}{Escape}
if ErrorLevel in EndKey:Escape,Timeout
{
SoundPlay *16 ;-- Error
return
}
;-- Collect selected text
$SelectedText:=Edit_GetSelText(hEdit)
;-- Replace All
StringReplace $SelectedText,$SelectedText,%$FindWhat%,%$ReplaceWith%,All
;-- Replace selected text with converted text
Edit_ReplaceSel(hEdit,$SelectedText)
;-- Reselect
Edit_SetSel(hEdit,$StartSelPos,$StartSelPos+StrLen($SelectedText))
return
A couple of final thoughts...
- Although the Find and Replace commands were originally designed for a single-line Edit control, they will work on any edit control.
- This script is just an example. Please feel free to make any changes you want.