File Loop and If/Else Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
jarhead
Posts: 151
Joined: 09 Sep 2020, 12:43

File Loop and If/Else

21 Jun 2021, 15:26

I'm using pdftk to combine two PDF files.

Running the following works fine with one PDF in the folder:

Code: Select all

Loop, Files, %A_Desktop%\MyFolder\*.pdf
Run %comspec% /c pdftk "%A_Desktop%\Folder2\Ins.pdf" "%A_LoopFileFullPath%" cat output "%A_Desktop%\MyFolder\TEST.pdf" "dont_ask", , hide
ExitApp
I'm wanting to find a certain PDF file in the folder. If the file is not found, I want to run FileSelectFile so that I can select the PDF to be used.

The PDF I want to use in the folder will always be named something like Smith, John Ledger 06-21-2021 so I'm assuming it would make since to look for the Ledger 06-21-2021 portion of the file name using something like RegExMatch.

I'm not exactly sure of the best way to do this. I'm assuming something like:

Code: Select all

Loop, Files, %A_Desktop%\MyFolder\*.pdf

If the PDF file name is in this format → Smith, John Ledger 06-21-2021 do this

Run %comspec% /c pdftk "%A_Desktop%\Folder2\Ins.pdf" "%A_LoopFileFullPath%" cat output "%A_Desktop%\MyFolder\TEST.pdf" "dont_ask", , hide
ExitApp

Else 

FileSelectFile, SelectedFile, 3, %A_Desktop%\MyFolder, Select PDF, PDF Docs (*.pdf)

Run %comspec% /c pdftk "%A_Desktop%\Folder2\Ins.pdf" "%SelectedFile%" cat output "%A_Desktop%\MyFolder\TEST.pdf" "dont_ask", , hide
ExitApp
Any help is appreciated. Thanks!
User avatar
mikeyww
Posts: 26871
Joined: 09 Sep 2014, 18:38

Re: File Loop and If/Else

22 Jun 2021, 04:58

When you write, "in this format", how would you describe that format? What are some additional examples that match that format? What are some examples that do not match that format?
jarhead
Posts: 151
Joined: 09 Sep 2020, 12:43

Re: File Loop and If/Else

22 Jun 2021, 07:06

The name of the pdf will always be last name, first name followed by the word Ledger and then the date so Smith, John Ledger 06-22-2021 or De La Cruz, Jose Ledger 06-22-2021. Thanks!
jarhead
Posts: 151
Joined: 09 Sep 2020, 12:43

Re: File Loop and If/Else  Topic is solved

22 Jun 2021, 11:31

I must be using RegExMatch incorrectly as the If portion of the following code is not working. Any ideas?

Code: Select all

Loop, Files, %A_Desktop%\MyFolder\*.pdf
	
If RegExMatch(A_LoopFileName, "Ledger \d{2}-\d{2}-\d{4}.pdf$")
	
{
Run %comspec% /c pdftk "%A_Desktop%\Folder2\Ins.pdf" "%A_LoopFileFullPath%" cat output "%A_Desktop%\MyFolder\TEST.pdf" "dont_ask", , hide
ExitApp
}

Else 

{
	FileSelectFile, SelectedFile, 3, %A_Desktop%\MyFolder, Select PDF, PDF Docs (*.pdf)
	
	Run %comspec% /c pdftk "%A_Desktop%\Folder2\Ins.pdf" "%SelectedFile%" cat output "%A_Desktop%\MyFolder\TEST2.pdf" "dont_ask", , hide
	ExitApp
}
User avatar
boiler
Posts: 16913
Joined: 21 Dec 2014, 02:44

Re: File Loop and If/Else

22 Jun 2021, 12:05

I don’t think it’s the RegEx statement that’s the problem. Here’s a demo:

Code: Select all

LoopFileName := "C\Users\Me\Desktop\Folder2\Smith, John Ledger 06-22-2021.pdf"
If RegExMatch(LoopFileName, "Ledger \d{2}-\d{2}-\d{4}.pdf$")
	MsgBox, Success
Else 
	MsgBox, Fail
jarhead
Posts: 151
Joined: 09 Sep 2020, 12:43

Re: File Loop and If/Else

22 Jun 2021, 12:19

boiler wrote: I don’t think it’s the RegEx statement that’s the problem. Here’s a demo:
Thanks! pdftk must have been hung up or something as the script is working now. Strange.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 336 guests