Generate File Names based on existing ones

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
3di
Posts: 12
Joined: 27 Jan 2023, 08:36

Generate File Names based on existing ones

Post by 3di » 27 Jan 2023, 08:41

Hi Swarm Intelligence,


I'm new to AHK and would like to automate something that grinds my gears on a daily basis.

I'm generating a lot of new files that change just I'm some kind of filename.
 

Example:

20230126_Some_Text_that_changes_only_daily_01.txt

20230126_Some_Text_that_changes_only_daily_02.txt

20230126_Some_Text_that_changes_only_daily_03.txt 

Idea, I click on a file name that already Exists and then a button for example F9, AHK gets the old file name and changes „01“ to 02“ and presses Save/Enter
Step 2 would be to bring a Message box that asks for the „Some_Text_that_changes_only_daily“ and generates the filename with the current date if none is existent in the folders. 

I experimented with trim and splitpath but didn’t get anything to work

Any Idea how to start?

-Thank You-

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

Re: Generate File Names based on existing ones

Post by mikeyww » 27 Jan 2023, 09:01

Welcome to this AutoHotkey forum!

In Windows File Explorer?

What folders?

Code: Select all

#Requires AutoHotkey v2.0

orig := "20230126_Some_Text_that_changes_only_daily_99.txt"

F9::MsgBox incr := incrementSuffix(orig)

incrementSuffix(str) {                               ; Increment a filename suffix (number)
 Static regex := "\d+$"
 SplitPath str,,, &ext, &fnBare                      ; Get the file name and extension
 If RegExMatch(fnBare, regex, &suffix) {             ; Get the filename suffix
  incr := suffix[] + 1                               ; Increment suffix by 1
  Return RegExReplace(fnBare, regex
          , Format("{:0"                             ; Left-pad with zeroes
          . Max(StrLen(suffix[]), StrLen(incr)) "}"  ; Determine the new suffix length
          , incr)) '.' ext                           ; Append the file extension
 }
}

3di
Posts: 12
Joined: 27 Jan 2023, 08:36

Re: Generate File Names based on existing ones

Post by 3di » 27 Jan 2023, 12:43

Hi @mikeyww ,

Thanks for your help, I tried it and I pretty sure it does 78% of what i need :bravo: :D, thank You!!

I need it when being in save as dialog of an application, would like to click on an existing file and do a suffix +1
else if no file is existing build a new string based on the scheme:

ABC_Some_Text_that_changes_only_daily_99.txt

ABC = a user specific 3 digit code
"Some_Text_that_changes_only_daily" Text asked for in a message box
01 as it is the first file.

Thanks again for your help, i appreciate that and I'm very thankful.

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

Re: Generate File Names based on existing ones

Post by mikeyww » 27 Jan 2023, 19:39

Code: Select all

#Requires AutoHotkey v2.0

code := 234

#HotIf WinActive("ahk_class #32770")
F9:: {
 dir := StrReplace(ControlGetText('ToolbarWindow324'), "Address: ") ; Directory
 (!DirExist(dir)) && dir := StrReplace(A_Desktop, "Desktop") dir
 If !FileExist(dir '\' fn := StrReplace(ControlGetText('Edit1'), "*"))
  If (ib := InputBox('Enter the file name.', 'File name', 'w400 h90')).Result = "OK"
   fn := code '_' ib.Value '_01.txt'
  Else Return
 ControlSetText nextFileName(dir '\' fn), 'Edit1'
 SoundBeep 2000
}
#HotIf

nextFileName(filePath) {
 SplitPath filePath, &fn, &dir
 While FileExist(dir '\' fn)
  fn := incrementSuffix(fn)
 Return fn
}

incrementSuffix(fn) {                                ; Increment a filename suffix (number)
 Static regex := "\d+$"
 SplitPath fn,,, &ext, &fnBare                       ; Get the file name and extension
 If RegExMatch(fnBare, regex, &suffix) {             ; Get the filename suffix
  incr := suffix[] + 1                               ; Increment suffix by 1
  Return RegExReplace(fnBare, regex
          , Format("{:0"                             ; Left-pad with zeroes
          . Max(StrLen(suffix[]), StrLen(incr)) "}"  ; Determine the new suffix length
          , incr)) '.' ext                           ; Append the file extension
 }
}

3di
Posts: 12
Joined: 27 Jan 2023, 08:36

Re: Generate File Names based on existing ones

Post by 3di » 17 Feb 2023, 12:26

Hi @mikeyww

Thanks for your Help, and the nice inspiration with the #hotif.

As I'm a new to AHK, I might need a little more help in achieving my goal.

My idea was that I would like to mark an existing file in the "safe as" dialog. The dialog would get the existing FileName01.txt in the input box, and I would press F9.
The magic number +1 happens, and Ctrl+v puts the new FileName02.txt in the new Filename box.

I tried a lot but didn't succeed in this.

I'll attach what i tried.

Code: Select all


F9::{

Send "^c"

orig := A_clipboard

incr := incrementSuffix(orig)

Send "^v"


incrementSuffix(str) {                               ; Increment a filename suffix (number)
 Static regex := "\d+$"
 SplitPath str,,, &ext, &fnBare                      ; Get the file name and extension
 If RegExMatch(fnBare, regex, &suffix) {             ; Get the filename suffix
  incr := suffix[] + 1                               ; Increment suffix by 1
  Return RegExReplace(fnBare, regex
          , Format("{:0"                             ; Left-pad with zeroes
          . Max(StrLen(suffix[]), StrLen(incr)) "}"  ; Determine the new suffix length
          , incr)) '.' ext                           ; Append the file extension
 }
}
Thanks again!

Regards

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

Re: Generate File Names based on existing ones

Post by mikeyww » 17 Feb 2023, 15:04

The last script that I posted already matches your description.

Post Reply

Return to “Ask for Help (v1)”