Page 1 of 1

Incremental rename selected folders

Posted: 27 Jan 2022, 07:03
by siomosp
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

Re: Incremental rename selected folders

Posted: 27 Jan 2022, 08:01
by mikeyww

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
}

Re: Incremental rename selected folders

Posted: 27 Jan 2022, 08:15
by siomosp
That was fast, and works perfectly
Thank you one more time!

Re: Incremental rename selected folders

Posted: 28 Jan 2022, 13:58
by siomosp
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?

Re: Incremental rename selected folders

Posted: 28 Jan 2022, 14:20
by mikeyww
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.

Re: Incremental rename selected folders

Posted: 28 Jan 2022, 15:26
by siomosp
Following you instructions, the file is renamed :) but the variable is added after the file name
abc.jpg 101 instead of abc 101.jpg

Re: Incremental rename selected folders

Posted: 28 Jan 2022, 18:33
by mikeyww
You can use SplitPath to disassemble the parts, add the suffix, and then add the extension again. Example

Re: Incremental rename selected folders

Posted: 30 Jan 2022, 17:05
by siomosp
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

Re: Incremental rename selected folders

Posted: 30 Jan 2022, 17:50
by mikeyww

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
}

Re: Incremental rename selected folders

Posted: 30 Jan 2022, 18:36
by siomosp
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...)