Jump to content

SmartRename for XP


Monamo
 Share

Recommended Posts

For those that typically have "Hide file extensions for known file types" disabled in Windows XP, it's an annoyance (at least for me) that using the Windows hotkey "F2" to rename files manually causes you to have to take care to avoid clearing the file extension.

Lifehacker had an AHK script (they're big on AHK) to address this, and I've converted it to AutoIt for those that might want to use it.

My recommendation would be to include it inside of any "persistent" scripts that you have running all the time (say, inside of a system monitoring script, whatever) since it seems kind of excessive to have a compiled version of this running standalone. Unless, of course, you don't have any other "always on" scripts.

Hope it's of use to some of you out there.

HotKeySet("{F2}", "_AnalyzeRename")

While 1
    Sleep(10)
WEnd

Func _AnalyzeRename()
    HotKeySet("{F2}"); prevent infinite loop gotcha
    Send("{F2}")
    If (_WinGetClass(WinGetTitle('')) = "CabinetWClass") Or (_WinGetClass(WinGetTitle('')) = "Progman") Then
        $oldClipboard = ClipGet()
        Sleep(100)
        Send("^c")
        $sFilename = ClipGet()
        $iExtPosition = StringInStr($sFilename, ".", 0, -1)
        If $iExtPosition <> 0 Then
            $iPosition = StringLen($sFilename) - $iExtPosition
            $i = 0
            Send("^{HOME}")
            Do
                Send("+{RIGHT}")
                $i += 1
            Until $i = ($iExtPosition - 1)
            Send("{SHIFTDOWN}{SHIFTUP}")
        EndIf
        ClipPut($oldClipboard)
    EndIf
    HotKeySet("{F2}", "_AnalyzeRename"); re-enable hotkey
EndFunc

Func _WinGetClass($hWnd)
; credit = SmOke_N from post http://www.autoitscript.com/forum/index.php?showtopic=41622&view=findpost&p=309799
    If IsHWnd($hWnd) = 0 And WinExists($hWnd) Then $hWnd = WinGetHandle($hWnd)
    Local $aGCNDLL = DllCall('User32.dll', 'int', 'GetClassName', 'hwnd', $hWnd, 'str', '', 'int', 4095)
    If @error = 0 Then Return $aGCNDLL[2]
    Return SetError(1, 0, '')
EndFunc

- MoChr(77)& Chr(97)& Chr(100)& Chr(101)& Chr(32)& Chr(121)& Chr(97)& Chr(32)& Chr(108)& Chr(111)& Chr(111)& Chr(107)-------I've told you 100,000 times not to exaggerate!-------Don't make me hit you with my cigarette hand...-------My scripts:Random Episode Selector, Keyboard MouseMover, CopyPath v2.1, SmartRename for XP,Window Tracer[sup]New![/sup]

Link to comment
Share on other sites

i wrote one, too for the german forum: http://www.autoit.de/index.php?page=Thread...69756#post69756

(no clipboard used and no send commands)

#include <WinAPI.au3>
While 1
    $ActiveWin = WinGetHandle("")
    $Win = _WinAPI_GetClassName($ActiveWin)
    If $Win = "CabinetWClass" Or $Win = "ExploreWClass" Then
        $Class = ControlGetFocus($ActiveWin)
        If StringRegExp($Class,"\AEdit\d+\Z") Then
            $EditHandle = ControlGetHandle($ActiveWin,"",$Class)
            $ParentClass = _WinAPI_GetClassName(_WinAPI_GetParent($EditHandle))
            If $ParentClass = "SysListView32" Then
                $text = ControlGetText($ActiveWin,"",$Class)
                $LetzterPunkt = StringInStr($text,".",1,-1)
                If $LetzterPunkt Then 
                    _SendMessage($EditHandle,0xB1,0,$LetzterPunkt-1,0,"dword","dword") ; 0xB1 = EM_SETSEL
                    Do
                        Sleep(100)
                    Until ControlGetFocus("") <> $Class
                EndIf
            EndIf
        EndIf
    EndIf
    Sleep(100)
WEnd
Edited by ProgAndy

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

That would be nice also in Firefox when you rename the filename when downloading a file.... :)

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

  • 2 months later...

Can I play? ^_^

#include <String.au3>
#include <WinAPI.au3>

HotKeySet("{F2}", "_VistaRename")

While 1
    Sleep(100)
WEnd

Func _VistaRename()
    Local $sClass = _WinAPI_GetClassName(WinGetHandle("[ACTIVE]")), $sFile
    If $sClass = "ExploreWClass" Or $sClass = "CabinetWClass" Or $sClass = "Progman" Then
        HotKeySet("{F2}")
        Send("{F2}")
        $sFile = _WinAPI_GetWindowText(ControlGetHandle("[ACTIVE]", "", ControlGetFocus("[ACTIVE]")))
        Local $i = StringLen($sFile) - StringInStr($sFile, ".", 0, -1) + 1
        If $i < StringLen($sFile) Then Send("{SHIFTDOWN}" & _StringRepeat("{LEFT}", $i) & "{SHIFTUP}")
        HotKeySet("{F2}", "_VistaRename")
    EndIf
EndFunc

No clipboard. I was trying to make this concise. I probably missed something ...

WBD

Link to comment
Share on other sites

Can I play? ^_^

#include <String.au3>
#include <WinAPI.au3>

HotKeySet("{F2}", "_VistaRename")

While 1
    Sleep(100)
WEnd

Func _VistaRename()
    Local $sClass = _WinAPI_GetClassName(WinGetHandle("[ACTIVE]")), $sFile
    If $sClass = "ExploreWClass" Or $sClass = "CabinetWClass" Or $sClass = "Progman" Then
        HotKeySet("{F2}")
        Send("{F2}")
        $sFile = _WinAPI_GetWindowText(ControlGetHandle("[ACTIVE]", "", ControlGetFocus("[ACTIVE]")))
        Local $i = StringLen($sFile) - StringInStr($sFile, ".", 0, -1) + 1
        If $i < StringLen($sFile) Then Send("{SHIFTDOWN}" & _StringRepeat("{LEFT}", $i) & "{SHIFTUP}")
        HotKeySet("{F2}", "_VistaRename")
    EndIf
EndFunc

No clipboard. I was trying to make this concise. I probably missed something ...

WBD

@WideBoyDixon

If I may:

#include <String.au3>
#include <WinAPI.au3>
#include <GuiEdit.au3>


HotKeySet("{F2}", "_VistaRename")

While 1
    Sleep(100)
WEnd

Func _VistaRename()
    HotKeySet("{F2}")
    Local $sClass = _WinAPI_GetClassName(WinGetHandle("[ACTIVE]")), $sFile
    If $sClass = "ExploreWClass" Or $sClass = "CabinetWClass" Or $sClass = "Progman" Then
        
        Send("{F2}")
        $hFileEdit = ControlGetHandle("[ACTIVE]", "", ControlGetFocus("[ACTIVE]"))
        $sFile = _WinAPI_GetWindowText(ControlGetHandle("[ACTIVE]", "", $hFileEdit))
        Local $i = StringLen($sFile) - StringInStr($sFile, ".", 0, -1) + 1
        If $i < StringLen($sFile) Then
            _GUICtrlEdit_SetSel($hFileEdit, 0, StringLen($sFile) - $i)
        EndIf
    Else
        Send("{F2}"); if some other window need to pass the F2 through
    EndIf
    HotKeySet("{F2}", "_VistaRename")
EndFunc

No SendKey ;)

Link to comment
Share on other sites

@WideBoyDixon

If I may:

#include <String.au3>
#include <WinAPI.au3>
#include <GuiEdit.au3>


HotKeySet("{F2}", "_VistaRename")

While 1
    Sleep(100)
WEnd

Func _VistaRename()
    HotKeySet("{F2}")
    Local $sClass = _WinAPI_GetClassName(WinGetHandle("[ACTIVE]")), $sFile
    If $sClass = "ExploreWClass" Or $sClass = "CabinetWClass" Or $sClass = "Progman" Then
        
        Send("{F2}")
        $hFileEdit = ControlGetHandle("[ACTIVE]", "", ControlGetFocus("[ACTIVE]"))
        $sFile = _WinAPI_GetWindowText(ControlGetHandle("[ACTIVE]", "", $hFileEdit))
        Local $i = StringLen($sFile) - StringInStr($sFile, ".", 0, -1) + 1
        If $i < StringLen($sFile) Then
            _GUICtrlEdit_SetSel($hFileEdit, 0, StringLen($sFile) - $i)
        EndIf
    Else
        Send("{F2}"); if some other window need to pass the F2 through
    EndIf
    HotKeySet("{F2}", "_VistaRename")
EndFunc

No SendKey ;)

You have me at a disadvantage sir with your ninja coding skillz (is that how the yoof of today say it?). Totally forgot about passing through F2 to other windows :(

However, since I'm here now, some notes:

[1] I did notice that $hFileEdit is already a window handle so _WinAPI_GetWindowText($hFileEdit) should be sufficient shouldn't it?

[2] The calculation of $i should be re-worked for using _GUICtrlEdit_SetSel():

Local $i = StringInStr($sFile, ".", 0, -1)
        If $i > 1 Then _GUICtrlEdit_SetSel($hFileEdit, 0, $i - 1)

[3] Actually, I quite liked the "sweep" of sending {LEFT}. Sad really ^_^

WBD

Link to comment
Share on other sites

You have me at a disadvantage sir with your ninja coding skillz (is that how the yoof of today say it?). Totally forgot about passing through F2 to other windows ;)

However, since I'm here now, some notes:

[1] I did notice that $hFileEdit is already a window handle so _WinAPI_GetWindowText($hFileEdit) should be sufficient shouldn't it?

[2] The calculation of $i should be re-worked for using _GUICtrlEdit_SetSel():

Local $i = StringInStr($sFile, ".", 0, -1)
        If $i > 1 Then _GUICtrlEdit_SetSel($hFileEdit, 0, $i - 1)

[3] Actually, I quite liked the "sweep" of sending {LEFT}. Sad really ^_^

WBD

OK, so here's the most concise version I can come up with (for now).

And look: I got rid of one more Send() !

While I too liked the visual appeal of the left "sweep", I really like to avoid Send() whenever I can.

#include <String.au3>
#include <WinAPI.au3>
#include <GuiEdit.au3>
#include <GuiListView.au3>

HotKeySet("{F2}", "_VistaRename")

While 1
    Sleep(100)
WEnd

Func _VistaRename()
    HotKeySet("{F2}")
    Local $sClass = _WinAPI_GetClassName(WinGetHandle("[ACTIVE]")), $sFile
    If $sClass = "ExploreWClass" Or $sClass = "CabinetWClass" Or $sClass = "Progman" Then
        $hListView = ControlGetHandle("[ACTIVE]", "", ControlGetFocus("[ACTIVE]"))
        _GUICtrlListView_EditLabel($hListView, _GUICtrlListView_GetNextItem($hListView))

;$hFileEdit = _GUICtrlListView_GetEditControl($hListView)
;hmmm, GUICtrlEdit_GetText & _GUICtrlEdit_SetSel doesn't seem to work with this

        $hFileEdit = ControlGetHandle("[ACTIVE]", "", ControlGetFocus("[ACTIVE]"))
        $sFile = _GUICtrlEdit_GetText($hFileEdit)
        $i = StringInStr($sFile, ".", 0, -1) - 1
        _GUICtrlEdit_SetSel($hFileEdit, 0, $i)
    Else
        Send("{F2}"); if some other window need to pass the F2 through
    EndIf
    HotKeySet("{F2}", "_VistaRename")

EndFunc ;==>_VistaRename

Edit: On reviewing this entire thread, it appears my method is not so different from ProgAndy's in post #2...

( http://www.autoitscript.com/forum/index.ph...st&p=638874 )

Edited by ResNullius
Link to comment
Share on other sites

Mo' conciser:

#include <String.au3>
#include <WinAPI.au3>
#include <GuiEdit.au3>
#include <GuiListView.au3>

HotKeySet("{F2}", "_VistaRename")

While 1
    Sleep(100)
WEnd

Func _VistaRename()
    HotKeySet("{F2}")
    Local $sClass = _WinAPI_GetClassName(WinGetHandle("[ACTIVE]"))
    Local $sFile, $hListView, $hFileEdit, $iDotPos
    If $sClass = "ExploreWClass" Or $sClass = "CabinetWClass" Or $sClass = "Progman" Then
        $hListView = ControlGetHandle("[ACTIVE]", "", ControlGetFocus("[ACTIVE]"))
        $hFileEdit = _GUICtrlListView_EditLabel($hListView, _GUICtrlListView_GetNextItem($hListView))
        $sFile = _GUICtrlEdit_GetText($hFileEdit)
        $iDotPos = StringInStr($sFile, ".", 0, -1) - 1
        _GUICtrlEdit_SetSel($hFileEdit, 0, $iDotPos)
    Else
        Send("{F2}");if some other window need to pass the F2 through
    EndIf
    HotKeySet("{F2}", "_VistaRename")

EndFunc;==>_VistaRename

Edit: Added missing local variable declarations

Edited by ResNullius
Link to comment
Share on other sites

Ah but you broke my ability to use F2 in the TreeView in Explorer. I'm settling on ...

#include <String.au3>
#include <WinAPI.au3>
#include <GuiEdit.au3>
#include <GuiListView.au3>

HotKeySet("{F2}", "_VistaRename")

While 1
    Sleep(100)
WEnd

Func _VistaRename()
    HotKeySet("{F2}")
    Local $sClass = _WinAPI_GetClassName(WinGetHandle("[ACTIVE]")), $sFile, $hListView, $hFileEdit
    If StringInstr("|ExploreWClass|CabinetWClass|Progman|", "|" & $sClass & "|") > 0 Then
        $hActiveControl = ControlGetHandle("[ACTIVE]", "", ControlGetFocus("[ACTIVE]"))
        If _WinAPI_GetClassName($hActiveControl) = "SysListView32" Then
            $hFileEdit = _GUICtrlListView_EditLabel($hActiveControl, _GUICtrlListView_GetNextItem($hActiveControl))
            $sFile = _GUICtrlEdit_GetText($hFileEdit)
            _GUICtrlEdit_SetSel($hFileEdit, 0, StringInStr($sFile, ".", 0, -1) - 1)
        Else
            Send("{F2}")
        EndIf
    Else
        Send("{F2}")
    EndIf
    HotKeySet("{F2}", "_VistaRename")
EndFunc   ;==>_VistaRename

WBD

Link to comment
Share on other sites

Ah but you broke my ability to use F2 in the TreeView in Explorer. I'm settling on ...

#include <String.au3>
#include <WinAPI.au3>
#include <GuiEdit.au3>
#include <GuiListView.au3>

HotKeySet("{F2}", "_VistaRename")

While 1
    Sleep(100)
WEnd

Func _VistaRename()
    HotKeySet("{F2}")
    Local $sClass = _WinAPI_GetClassName(WinGetHandle("[ACTIVE]")), $sFile, $hListView, $hFileEdit
    If StringInstr("|ExploreWClass|CabinetWClass|Progman|", "|" & $sClass & "|") > 0 Then
        $hActiveControl = ControlGetHandle("[ACTIVE]", "", ControlGetFocus("[ACTIVE]"))
        If _WinAPI_GetClassName($hActiveControl) = "SysListView32" Then
            $hFileEdit = _GUICtrlListView_EditLabel($hActiveControl, _GUICtrlListView_GetNextItem($hActiveControl))
            $sFile = _GUICtrlEdit_GetText($hFileEdit)
            _GUICtrlEdit_SetSel($hFileEdit, 0, StringInStr($sFile, ".", 0, -1) - 1)
        Else
            Send("{F2}")
        EndIf
    Else
        Send("{F2}")
    EndIf
    HotKeySet("{F2}", "_VistaRename")
EndFunc   ;==>_VistaRename

WBD

Agreed good sir.

Like the way you "concised" up my $iDotPos into the _SetSel. Cheers!

Link to comment
Share on other sites

I want to play!

#include <String.au3>
#include <WinAPI.au3>
#include <GuiEdit.au3>
#include <GuiListView.au3>
#include <GuiTreeView.au3>

HotKeySet("{F2}", "_VistaRename")

While 1
    Sleep(100)
WEnd

Func _VistaRename()
    HotKeySet("{F2}")
    Local $sClass = _WinAPI_GetClassName(WinGetHandle("[ACTIVE]")), $sFile, $hListView, $hFileEdit
    If StringInstr("|ExploreWClass|CabinetWClass|Progman|", "|" & $sClass & "|") > 0 Then
        $hActiveControl = ControlGetHandle("[ACTIVE]", "", ControlGetFocus("[ACTIVE]"))
        If _WinAPI_GetClassName($hActiveControl) = "SysListView32" Then
            $hFileEdit = _GUICtrlListView_EditLabel($hActiveControl, _GUICtrlListView_GetNextItem($hActiveControl))
            $sFile = _GUICtrlEdit_GetText($hFileEdit)
            _GUICtrlEdit_SetSel($hFileEdit, 0, StringInStr($sFile, ".", 0, -1) - 1)
        ElseIf _WinAPI_GetClassName($hActiveControl) = "SysTreeView32" Then
            $hFileEdit = _GUICtrlTreeView_EditText($hActiveControl, _GUICtrlTreeView_GetSelection($hActiveControl))
            $sFile = _GUICtrlEdit_GetText($hFileEdit)
            _GUICtrlEdit_SetSel($hFileEdit, 0, StringInStr($sFile, ".", 0, -1) - 1)
        Else
            Send("{F2}")
        EndIf
    Else
        Send("{F2}")
    EndIf
    HotKeySet("{F2}", "_VistaRename")
EndFunc   ;==>_VistaRename

This is for renaming zip files in the treeview. Only problem with it, is after the edit, I loose it's ability to expand/collapse. Haven't found a fix for that yet.

Edit: Apparently, that also happens without the script running for me, so the script is fine as is.

Edited by SkinnyWhiteGuy
Link to comment
Share on other sites

@WideBoyDixon

Your script works like a charm. thank you.

Edited by goldenix
My Projects:[list][*]Guide - ytube step by step tut for reading memory with autoitscript + samples[*]WinHide - tool to show hide windows, Skinned With GDI+[*]Virtualdub batch job list maker - Batch Process all files with same settings[*]Exp calc - Exp calculator for online games[*]Automated Microsoft SQL Server 2000 installer[*]Image sorter helper for IrfanView - 1 click opens img & move ur mouse to close opened img[/list]
Link to comment
Share on other sites

Before I try to spoil everything, thankyou wideboydixon + resnullius, not only for the script, but also the entertainment value. It reminds us all how valuable competition is to us all. brings prices down.

all have errors, but they can't be solved without breaking the whole idea of a small script...

1) You can rename folders to "Folder.ext". It thinks its a file, and mucks up by acting as it would for a file (This was me deliberately trying to be a pain)

2) haven't got into the context menu > rename yet. I tried, I failed. nowhere in the shell for * or file, neither was it mentioned in the CLSID for file. Tried looking at various explorer related keys, and lastly made a last ditch attempt to search the whole registry for "Rena&me", still nothing. I dont know how you could do it that way then, Possibly editing explorer.exe, but thats not my cup of tea or coffee.

3) Extensions with dots in them (once again, me being an asshole)

4) Do you actually use string.au3 anywhere? That looks like a waste of space to me...

5) you could streamline it further, picking out includes etc. but that gets bit pointless as its a small script, and unless you're compiling it won't make much size difference.

I am being very annoying with some of those, redirecting would be nice though if it was possible... had a quick look in reshacker, though I don't know what I'm looking for really.

MDiesel

Edited by mdiesel
Link to comment
Share on other sites

  • 1 year later...

Sorry to revive this; found none related. Looking to use WBDs code and resolve some of Mat's issues.

How would I code multiple presses of F2 while renaming to switch between selections?

For example (red indicates highlighted text):

$sFile = "sample.txt.bak"

Press F2 - sample.txt.bak

Press F2 again - sample.txt.bak

Press F2 again - sample.txt.bak

Press F2 again - sample.txt.bak

My slow, broken, ugly solution which changes the occurrence number for StringInStr:

Func _VistaRename()
    HotKeySet("{F2}")
    Local $i = 0
    Local $sClass = _WinAPI_GetClassName(WinGetHandle("[ACTIVE]")), $sFile, $hListView, $hFileEdit
    If StringInstr("|ExploreWClass|CabinetWClass|Progman|", "|" & $sClass & "|") > 0 Then
        $hActiveControl = ControlGetHandle("[ACTIVE]", "", ControlGetFocus("[ACTIVE]"))
        If _WinAPI_GetClassName($hActiveControl) = "SysListView32" Then
            $hFileEdit = _GUICtrlListView_EditLabel($hActiveControl, _GUICtrlListView_GetNextItem($hActiveControl))
            $sFile = _GUICtrlEdit_GetText($hFileEdit)
            $split = StringSplit($sFile, ".")
            $occur = $split[0]
            Do
                _GUICtrlEdit_SetSel($hFileEdit, 0, StringInStr($sFile, ".", 0, -$i) - 1)
                Sleep(100)
                if _isPressed("71", $dll) Then $i = $i+1 ;if {F2}   
            Until $i > $occur
        Else
            Send("{F2}")
        EndIf
    Else
        Send("{F2}")
    EndIf
    HotKeySet("{F2}", "_VistaRename")
EndFunc   ;==>_VistaRename

Please pardon my lack of experience (none). Help is appreciated! :blink:

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