Incremental rename selected folders

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
siomosp
Posts: 27
Joined: 18 Jan 2022, 10:21

Incremental rename selected folders

Post by siomosp » 27 Jan 2022, 07:03

Hi,
i want to rename selected folders, adding an incremental number at the end of folder name.
The number is variable from user prompt
sample folders list

foldera
folderb
folderc

sample user variable : 140

the folders new names should be

foldera 140
folderb 141
folderc 142

User avatar
mikeyww
Posts: 26596
Joined: 09 Sep 2014, 18:38

Re: Incremental rename selected folders

Post by mikeyww » 27 Jan 2022, 08:01

Code: Select all

#IfWinActive ahk_class CabinetWClass
F3::
InputBox, index, Rename directories, Enter the starting index number,, 300, 125
If (ErrorLevel || index = "")
 Return
sorted := {}
For each, dir in getSelected()
 If Instr(FileExist(dir), "D")
  sorted[dir] := True
For dir in sorted {
 FileMoveDir, %dir%, % dir " " index++, R
 If ErrorLevel
  MsgBox, 48, Error, An error occurred while renaming the directory.`n`n%dir%
}
Return
#IfWinActive

getSelected() { ; https://www.autohotkey.com/boards/viewtopic.php?style=17&t=60403#p255256
 hwnd := WinExist("A"), selection := []
 WinGetClass, class
 Switch
 { Case class ~= "(Cabinet|Explore)WClass":
    For oWin in ComObjCreate("Shell.Application").Windows
     If (oWin.hwnd = hwnd)
      Break
   Case class ~= "Progman|WorkerW": ; https://www.autohotkey.com/boards/viewtopic.php?p=154836#p154836
    oWindows := ComObjCreate("Shell.Application").Windows, VarSetCapacity(hWnd, 4, 0)
    oWin := oWindows.FindWindowSW(0, "", SWC_DESKTOP := 8, ComObject(0x4003, &hWnd), SWFO_NEEDDISPATCH := 1)
 }
 For oItem in oWin.Document.SelectedItems
  If SubStr(oItem.path, 1, 3) != "::{"
   selection.Push(oItem.Path)
 Return selection
}

siomosp
Posts: 27
Joined: 18 Jan 2022, 10:21

Re: Incremental rename selected folders

Post by siomosp » 27 Jan 2022, 08:15

That was fast, and works perfectly
Thank you one more time!

siomosp
Posts: 27
Joined: 18 Jan 2022, 10:21

Re: Incremental rename selected folders

Post by siomosp » 28 Jan 2022, 13:58

Hello again!
i can use your help again?
Your code works fine at folders
Can be edited to work for all selected folders or files?

User avatar
mikeyww
Posts: 26596
Joined: 09 Sep 2014, 18:38

Re: Incremental rename selected folders

Post by mikeyww » 28 Jan 2022, 14:20

Line 8 filters the files and leaves the folders. You could try removing it. You would also need to change line 11-- add a new conditional statement-- so that it does a FileMove if that Instr(FileExist... value is 0, which indicates a file rather than a directory. If the Instr value is positive, it is a directory, so you do the directory move; otherwise, you FileMove.

siomosp
Posts: 27
Joined: 18 Jan 2022, 10:21

Re: Incremental rename selected folders

Post by siomosp » 28 Jan 2022, 15:26

Following you instructions, the file is renamed :) but the variable is added after the file name
abc.jpg 101 instead of abc 101.jpg

User avatar
mikeyww
Posts: 26596
Joined: 09 Sep 2014, 18:38

Re: Incremental rename selected folders

Post by mikeyww » 28 Jan 2022, 18:33

You can use SplitPath to disassemble the parts, add the suffix, and then add the extension again. Example

siomosp
Posts: 27
Joined: 18 Jan 2022, 10:21

Re: Incremental rename selected folders

Post by siomosp » 30 Jan 2022, 17:05

Hi!
i am learning...
but...
From this code how i can extract filename?

Code: Select all

getSelected() { ; https://www.autohotkey.com/boards/viewtopic.php?style=17&t=60403#p255256
 hwnd := WinExist("A"), selection := []
 WinGetClass, class
 Switch
 { Case class ~= "(Cabinet|Explore)WClass":
    For oWin in ComObjCreate("Shell.Application").Windows
     If (oWin.hwnd = hwnd)
      Break
   Case class ~= "Progman|WorkerW": ; https://www.autohotkey.com/boards/viewtopic.php?p=154836#p154836
    oWindows := ComObjCreate("Shell.Application").Windows, VarSetCapacity(hWnd, 4, 0)
    oWin := oWindows.FindWindowSW(0, "", SWC_DESKTOP := 8, ComObject(0x4003, &hWnd), SWFO_NEEDDISPATCH := 1)
 }
 For oItem in oWin.Document.SelectedItems
  If SubStr(oItem.path, 1, 3) != "::{"
   selection.Push(oItem.Path)
 Return selection

User avatar
mikeyww
Posts: 26596
Joined: 09 Sep 2014, 18:38

Re: Incremental rename selected folders

Post by mikeyww » 30 Jan 2022, 17:50

Code: Select all

#IfWinActive ahk_class CabinetWClass
F3::
InputBox, index, Rename directories, Enter the starting index number,, 300, 125
If (ErrorLevel || index = "")
 Return
sorted := {}
For each, item in getSelected()
 sorted[item] := True
For item in sorted {
 If !Instr(FileExist(item), "D") {
  SplitPath, item,, dir, ext, fnBare
  FileMove, %item%, %dir%\%fnBare% %index%.%ext%
 } Else FileMoveDir, %item%, %item% %index%, R
 If ErrorLevel
  MsgBox, 48, Error, An error occurred while renaming the item.`n`n%item%
 index++
}
Return
#IfWinActive

getSelected() { ; https://www.autohotkey.com/boards/viewtopic.php?style=17&t=60403#p255256
 hwnd := WinExist("A"), selection := []
 WinGetClass, class
 Switch
 { Case class ~= "(Cabinet|Explore)WClass":
    For oWin in ComObjCreate("Shell.Application").Windows
     If (oWin.hwnd = hwnd)
      Break
   Case class ~= "Progman|WorkerW": ; https://www.autohotkey.com/boards/viewtopic.php?p=154836#p154836
    oWindows := ComObjCreate("Shell.Application").Windows, VarSetCapacity(hWnd, 4, 0)
    oWin := oWindows.FindWindowSW(0, "", SWC_DESKTOP := 8, ComObject(0x4003, &hWnd), SWFO_NEEDDISPATCH := 1)
 }
 For oItem in oWin.Document.SelectedItems
  If SubStr(oItem.path, 1, 3) != "::{"
   selection.Push(oItem.Path)
 Return selection
}

siomosp
Posts: 27
Joined: 18 Jan 2022, 10:21

Re: Incremental rename selected folders

Post by siomosp » 30 Jan 2022, 18:36

I was close to do it, but you gave me the code ready one more time :)
I can't Thank you enough !!!!
(Now can get some sleep...)

Post Reply

Return to “Ask for Help (v1)”