AutoHotKey Renamer Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
arpit12
Posts: 56
Joined: 10 Apr 2022, 04:19

AutoHotKey Renamer

12 Apr 2022, 11:41

Hi i am new to autohotkey. i am looking to make a Renamer.
I want to do few things
Search For Copy and delete it
"Regular2_Alpha_Button_copy"--- removes copy--- "Regular2_Alpha_Button"
Search For Alpha and replace it with Mask
"Regular2_Alpha_Button" ----replace Alpha -- "Regular2_Mask_Button"
Search For name and take input then replace it
"Regular2_Mask_Button"----take input from user and replace---- "Regular2" with the user input.
Thanks in advance
Last edited by arpit12 on 12 Apr 2022, 14:49, edited 1 time in total.
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: AutoHotKey Renamer

12 Apr 2022, 13:33

Regular2_Alpha_Button_copy removes copy Regular2_Alpha_Button - FileDelete
Regular2_Alpha_Button replace Alpha -- Regular2_Mask_Button - FileMove
Regular2_Mask_Button----take input from user and replace Regular2 with the user input. - InputBox + FileMove
arpit12
Posts: 56
Joined: 10 Apr 2022, 04:19

Re: AutoHotKey Renamer

12 Apr 2022, 14:08

Hi,
@BoBo I am looking to rename the file but fileDelete and fileMove work on whole file.
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: AutoHotKey Renamer

12 Apr 2022, 14:22

@arpit12 -
FileMove

Moves or renames one or more files.
:think:
arpit12
Posts: 56
Joined: 10 Apr 2022, 04:19

Re: AutoHotKey Renamer

12 Apr 2022, 15:54

Sorry as i have mentioned i am new to coding. If anyone can provide me with syntax that would be very helpful.
User avatar
Cuadrix
Posts: 236
Joined: 07 May 2017, 08:26

Re: AutoHotKey Renamer

12 Apr 2022, 16:06

arpit12 wrote: Sorry as i have mentioned i am new to coding. If anyone can provide me with syntax that would be very helpful.

https://www.autohotkey.com/docs/commands/FileMove.htm#ExRename
#2: Renames a single file.

Code: Select all

FileMove, C:\File Before.txt, C:\File After.txt
Use the docs.
arpit12
Posts: 56
Joined: 10 Apr 2022, 04:19

Re: AutoHotKey Renamer

13 Apr 2022, 00:43

Hi @Cuadrix
Thanks but i saw this syntax in docs but as you see here they have given full path of the file but what i am looking for is that program search for particular word and replace it with another Like search for alpha and replace with Mask.
User avatar
Cuadrix
Posts: 236
Joined: 07 May 2017, 08:26

Re: AutoHotKey Renamer

13 Apr 2022, 02:55

Untested. Press F1 to "Search For name and take input then replace it"

Code: Select all

SetWorkingDir %A_ScriptDir%

Prefix := "Regular2"
Suffix := "copy"
OldText := "Alpha"
NewText := "Mask"
_ := "_" ; Separator

; Loop through all files in each subdirectory
Loop, Files, *, R
{
  ; Remove copies
  if (InStr(A_LoopFilePath, _ Suffix)) {
    FileDelete, % A_LoopFilePath
  
  ; Rename "Alpha" to "Mask"
  } else if (InStr(A_LoopFilePath, _ OldText _)) {
    FileMove, % A_LoopFilePath, % StrReplace(A_LoopFilePath, _ OldText _, _ NewText _)
  }
}

F1::
; Manual user input triggered on hotkey press
InputBox, FileToSearch,, % "File to search (path relative to working directory):"
InputBox, NewPrefix,, % "Replace " Prefix " with:"

; Rename file according to the input
FileMove, % FileToSearch, % StrReplace(FileToSearch, Prefix _, NewPrefix _)
Return
arpit12
Posts: 56
Joined: 10 Apr 2022, 04:19

Re: AutoHotKey Renamer

13 Apr 2022, 12:27

Hi,
@Cuadrix Thank for the help
I tested the code but
filedelete is deleting the whole file.
Second function works fine it is replacing
third function also not working it does nothing.
User avatar
Cuadrix
Posts: 236
Joined: 07 May 2017, 08:26

Re: AutoHotKey Renamer

13 Apr 2022, 13:41

I see now. Replace "copy" not delete the file...
Try this:

Code: Select all

SetWorkingDir %A_ScriptDir%

Prefix := "Regular2"
Suffix := "copy"
OldText := "Alpha"
NewText := "Mask"
_ := "_" ; Separator

; Loop through all files in each subdirectory
Loop, Files, *, R
{
  ; Remove "copy" suffix
  FileName := StrReplace(A_LoopFilePath, _ Suffix)

  ; Replace "Alpha" with "Mask"
  FileName := StrReplace(FileName, _ OldText _, _ NewText _)

  ; Finally rename the file
  FileMove, % A_LoopFilePath, % FileName
}

F1::
; Manual user input triggered on hotkey press
InputBox, FileToSearch,, % "File to search (path relative to working directory):"
InputBox, NewPrefix,, % "Replace " Prefix " with:"

; Rename file according to the input
FileMove, % FileToSearch, % StrReplace(FileToSearch, Prefix _, NewPrefix _)
Return
The 3rd function should work just fine
arpit12
Posts: 56
Joined: 10 Apr 2022, 04:19

Re: AutoHotKey Renamer

14 Apr 2022, 05:43

Thanks @Cuadrix this works.
Is it possible to multiple value to
Suffix := "copy"
Also the third function only works if I gave the name of the file like
Regular2_Mask_Button.jpg in "path relative to working directory"
would it be possible if it takes prefix as input and search the same folder as it is doing for first two function and replace by new input.
User avatar
Cuadrix
Posts: 236
Joined: 07 May 2017, 08:26

Re: AutoHotKey Renamer

14 Apr 2022, 07:58

Code: Select all

SetWorkingDir %A_ScriptDir%

FileList := []

Prefix := "Regular2"
SuffixList := ["copy", "dragon", "banana"]
OldText := "Alpha"
NewText := "Mask"
_ := "_" ; Separator

; Loop through all files in each subdirectory
Loop, Files, *, R
{
    ; Shorten because too much eye cancer
    LFP := A_LoopFilePath

    ; Skip if prefix not in the beginning of file name
    If (InStr(LFP, Prefix _) != 1)
        continue

    ; Remove "copy..." suffix
    For Idx, Suffix in SuffixList
    {
        Length := StrLen(LFP) - StrLen(_ Suffix)
        If (InStr(LFP, _ Suffix, Length)) {
            FileName := SubStr(LFP, 1, Length)
            break
        }
    }

    ; Replace "Alpha" with "Mask"
    FileName := StrReplace(FileName, _ OldText _, _ NewText _)

    ; Finally rename the file
    If (FileName) {
        FileMove, % LFP, % FileName

        ; Add file to list for manual operation later
        FileList.Push(FileName)
        break
    }
}

F1::
; Manual user input triggered on hotkey press
InputBox, OldPrefix,, % "File to search (prefix)(path relative to working directory):"
InputBox, NewPrefix,, % "Replace " OldPrefix " with:"

For Idx, File in FileList
{
    If (InStr(File, OldPrefix)) {
        ; Rename file according to the input
        FileMove, % File, % StrReplace(File, OldPrefix _, NewPrefix _)
    }
}
Return
arpit12
Posts: 56
Joined: 10 Apr 2022, 04:19

Re: AutoHotKey Renamer

14 Apr 2022, 09:27

Code is not working anymore.it does nothing.
User avatar
Cuadrix
Posts: 236
Joined: 07 May 2017, 08:26

Re: AutoHotKey Renamer

14 Apr 2022, 12:49

arpit12 wrote:
14 Apr 2022, 09:27
Code is not working anymore.it does nothing.
Works fine for me
arpit12
Posts: 56
Joined: 10 Apr 2022, 04:19

Re: AutoHotKey Renamer

14 Apr 2022, 13:05

Hi
@Cuadrix I copied the code and run
this is my file name :: Regular2_Alpha_Button_Copy.jpg
it is not doing anything.
Also two input box so first should i give like Regular2 and then Regular3
Let me know if i am doing anything wrong here.
Thanks
User avatar
Cuadrix
Posts: 236
Joined: 07 May 2017, 08:26

Re: AutoHotKey Renamer  Topic is solved

14 Apr 2022, 13:50

Just press F1 again to search for "Regular3". Just remember to add it to the PrefixList

Code: Select all

SetWorkingDir %A_ScriptDir%

FileList := []

PrefixList := ["Regular2", "Regular3"]
SuffixList := ["copy", "dragon", "banana"]
OldText := "Alpha"
NewText := "Mask"
_ := "_" ; Separator

; Loop through all files in each subdirectory
Loop, Files, *, R
{
    SplitPath, A_LoopFilePath,, FileDir, FileExt, FileName

    ; Skip if prefix not in the beginning of file name
    For Idx, Prefix in PrefixList
    {
        ; Get back to main outerloop if prefix exists
        If (InStr(FileName, Prefix _) = 1)
            Break
        
        ; Skip if reached end of list and still no prefix
        Else If (Idx = PrefixList.Length())
            Continue, 2
    }

    ; Remove "copy..." suffix
    For Idx, Suffix in SuffixList
    {
        Length := StrLen(FileName) - StrLen(_ Suffix)
        If (InStr(FileName, _ Suffix, Length)) {
            FileName := SubStr(FileName, 1, Length)
            Break
        }
    }

    ; Replace "Alpha" with "Mask"
    FileName := StrReplace(FileName, _ OldText _, _ NewText _)

    If (FileName) {
        ; Remember to join dir, ext and name back together
        If (FileDir)
            FileName := FileDir "\" FileName
        
        If (FileExt)
            FileName := FileName "." FileExt
        
        ; Finally rename the file
        FileMove, % A_LoopFilePath, % FileName

        ; Add file to list for manual operation later
        FileList.Push(FileName)
    }
}

F1::
; Manual user input triggered on hotkey press
InputBox, OldPrefix,, % "File to search (prefix)(path relative to working directory):"
InputBox, NewPrefix,, % "Replace " OldPrefix " with:"

For Idx, File in FileList
{
    If (InStr(File, OldPrefix)) {
        ; Rename file according to the input
        FileMove, % File, % StrReplace(File, OldPrefix _, NewPrefix _)
    }
}
Return
arpit12
Posts: 56
Joined: 10 Apr 2022, 04:19

Re: AutoHotKey Renamer

14 Apr 2022, 14:07

Thank you so much it works.
arpit12
Posts: 56
Joined: 10 Apr 2022, 04:19

Re: AutoHotKey Renamer

23 Apr 2022, 02:21

Hi,
@Cuadrix
Please help i need few changes to this code. I want to remove the prefix also but before removing prefix would it be possible to create the folder and move same files and then remove the prefix
Like::
Regular2_Alpha_Button_copy
Regular2_Alpha_Rivet_copy
Regular3_Alpha_Button_copy
Regular3_Alpha_Rivet_copy
So in this case it would create two folder with name Regular2 & Regula3
and inside file naming would be
Mask_Button
Mask_Rivet
If it would be possible without user input.
Thank you in advance.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: VanVarden and 103 guests