Jump to content

Recommended Posts

Posted

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.

Global $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
Posted (edited)

Your main problem is that $var1, $var2 are not integers but text.

HotKeySet("{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 by ioa747

I know that I know nothing

Posted (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

#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 by ioa747

I know that I know nothing

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...