Krabax Posted July 19, 2025 Posted July 19, 2025 Hello, since I almost daily have to rename a lot of folders and other stuff, I wanted to create a tiny script. So it automatically renames them 01, 02, 03 ... 45, 46, etc.. It works absolutely fine, unless I give var1 a value with the InputBox and set it to 2, 3, 4, 5, 6, 7, 8 or 9. Values like 1, 10, 11, etc. work just perfectly fine this way. I tried to set var1 to one of these numbers without InputBox to test it and it worked perfectly fine. var2 accepts any values set with InputBox. I have no clue why it ain't working. I'm a total noob when it comes to programming btw. expandcollapse popupGlobal $var1 = 1 Global $var2 = 0 Global $loop = 0 HotKeySet("{ESC}", "Terminate") HotKeySet("{F7}", "Func1") While 1 sleep(100) WEnd Func Func1() $loop = 0 $var1 = InputBox("Rename", "Number to start?", "1", "", -1, 150) $var2 = InputBox("Rename", "Number to stop?", "", "", -1, 150) While $loop = 0 If $var1 <= $var2 Then Send("{F2}") Sleep(20) If $var1 < 10 Then Send("0") EndIf Send($var1) Sleep(20) Send("{ENTER}") Sleep(20) Send("{DOWN}") $var1 = $var1 + 1 Else $loop = 1 EndIf WEnd EndFunc Func Terminate() Exit EndFunc
Solution ioa747 Posted July 19, 2025 Solution Posted July 19, 2025 (edited) Your main problem is that $var1, $var2 are not integers but text. expandcollapse popupHotKeySet("{ESC}", "Terminate") HotKeySet("{F7}", "Func1") While 1 Sleep(100) WEnd Func Func1() Local $var1 = InputBox("Rename", "Number to start?", "1", "", -1, 150) Local $var2 = InputBox("Rename", "Number to stop?", "", "", -1, 150) Local $FileName $var1 = Int($var1) ; convert to integer $var2 = Int($var2) ; convert to integer While 1 If $var1 <= $var2 Then $FileName = $var1 If $var1 < 10 Then $FileName = "0" & $var1 ConsoleWrite("$FileName=" & $FileName & @CRLF) ; for debugging purpose only Send("{F2}") Sleep(20) Send($FileName) Sleep(20) Send("{ENTER}") Sleep(20) Send("{DOWN}") $var1 += 1 Sleep(2000) ; for debugging purpose only Else ExitLoop EndIf WEnd EndFunc ;==>Func1 Func Terminate() Exit EndFunc ;==>Terminate Edited July 19, 2025 by ioa747 I know that I know nothing
Developers Jos Posted July 19, 2025 Developers Posted July 19, 2025 Do you really want to use the Windows Explorer rename option and all its risks? Why not simply use the FileMove() functions? SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past.
ioa747 Posted July 19, 2025 Posted July 19, 2025 (edited) with the above I just wanted to show you where your script fails as mentioned above by Jos, this is not the best method, especially if the data changes order after each rename Here I have a different, more complex but safer approach. (thanks to the footsteps of the Nine ) This script renames multiple folders selected in an open Windows Explorer window numerically, initiated by pressing F7 and requiring an input for the starting number expandcollapse popup#include <Array.au3> HotKeySet("{ESC}", "Terminate") HotKeySet("{F7}", "Func1") While 1 Sleep(100) WEnd Func Func1() Local $hExplorer = WinGetHandle("[REGEXPCLASS:^(Cabinet|Explore)WClass$]") If Not $hExplorer Then Exit Local $oShell = ObjCreate("Shell.Application") For $oWindow In $oShell.Windows() If $oWindow.HWND = $hExplorer Then ExitLoop Next With $oWindow.Document.SelectedItems() If Not .count Then Exit MsgBox(0, "! Error", "Please select a folder(s)") ConsoleWrite("Items selected: " & .count & @CRLF) Local $aFolder[.count] Local $folderCount = 0 For $i = 0 To .count - 1 ; Check if the selected item is a folder If .item($i).IsFolder Then $aFolder[$folderCount] = .item($i).Path $folderCount += 1 EndIf Next ; Resize the array to the actual number of folders selected ReDim $aFolder[$folderCount] EndWith If $folderCount = 0 Then Exit MsgBox(0, "! Error", "Please select at least one folder") Local $var1 = InputBox("Rename", "Number to start?", "1", "", -1, 150) $var1 = Int($var1) ; convert to integer For $i = 0 To UBound($aFolder) - 1 ; Construct the new folder name Local $FolderName If $var1 < 10 Then $FolderName = StringFormat("%02i", $var1) Else $FolderName = $var1 EndIf ; Rename the folder Local $newFolderPath = StringReplace($aFolder[$i], StringRegExpReplace($aFolder[$i], ".*\\", ""), $FolderName) ; Keep the original path DirMove($aFolder[$i], $newFolderPath, 1) ; 1 = overwrite if the new folder already exists ConsoleWrite("Renamed: " & $aFolder[$i] & " to " & $newFolderPath & @CRLF) $var1 += 1 Next EndFunc ;==>Func1 Func Terminate() Exit EndFunc ;==>Terminate Edited July 19, 2025 by ioa747 I know that I know nothing
Krabax Posted July 24, 2025 Author Posted July 24, 2025 Thanks for trying to improve this horrible script, but if i need to change something in there i wouldn't understand a thing. That's why i choose to go with the simple "change to integer" fix. It works perfectly fine and if i mess up, i can easily re-unzip the unchanged files and do it again. Besides that, it's not just folders, but all kind of mixed files i need to rename in a certain order. To be honest, someone with a few programming skills could probably fully automate this small part of my job. 😅
ioa747 Posted July 24, 2025 Posted July 24, 2025 (edited) 2 hours ago, Krabax said: Besides that, it's not just folders, but all kind of mixed files i need to rename in a certain order. This Version takes a mix of files and folders. First selected mix of files and folders in an open Windows Explorer window , then pressing F7 and give the input for the starting number expandcollapse popup#include <Array.au3> #include <File.au3> HotKeySet("{ESC}", "Terminate") HotKeySet("{F7}", "Func1") While 1 Sleep(100) WEnd Func Func1() Local $hExplorer = WinGetHandle("[REGEXPCLASS:^(Cabinet|Explore)WClass$]") If Not $hExplorer Then Exit Local $oShell = ObjCreate("Shell.Application") For $oWindow In $oShell.Windows() If $oWindow.HWND = $hExplorer Then ExitLoop Next With $oWindow.Document.SelectedItems() If Not .count Then Exit MsgBox(0, "! Error", "Please select a folder(s)") Local $var1 = InputBox("Rename", "Number to start?", "1", "", -1, 150) $var1 = Int($var1) ; convert to integer ConsoleWrite("Items selected: " & .count & @CRLF & @CRLF) Local $aItems[.count] For $i = 0 To .count - 1 ; $aItems[$i] = .item($i).Path Local $sDrive = "", $sDir = "", $sFileName = "", $sExtension = "" _PathSplit(.item($i).Path, $sDrive, $sDir, $sFileName, $sExtension) ; Construct the new name Local $sNewItemName If $var1 < 10 Then $sNewItemName = StringFormat("%02i", $var1) Else $sNewItemName = $var1 EndIf ; Rename the folder or file Local $sNewFullPath = $sDrive & $sDir & $sNewItemName & $sExtension If .item($i).IsFolder Then If Not DirMove(.item($i).Path, $sNewFullPath, 1) Then ConsoleWrite("Error renaming folder: " & .item($i).Path & @CRLF) Else ConsoleWrite("Folder Renamed: " & .item($i).Path & @CRLF) ConsoleWrite(" to: " & $sNewFullPath & @CRLF) EndIf Else If Not FileMove(.item($i).Path, $sNewFullPath, 1) Then ConsoleWrite("Error renaming file: " & .item($i).Path & @CRLF) Else ConsoleWrite(" File Renamed: " & .item($i).Path & @CRLF) ConsoleWrite(" to: " & $sNewFullPath & @CRLF) EndIf EndIf ConsoleWrite("" & @CRLF) $var1 += 1 Next EndWith EndFunc ;==>Func1 Func Terminate() Exit EndFunc ;==>Terminate Edited July 24, 2025 by ioa747 I know that I know nothing
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