Delete original PDF when doing a File → Save As

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

Delete original PDF when doing a File → Save As

27 Jul 2021, 15:06

I have a folder full of PDF files that is continuously updated with new PDF files throughout the day (all are named randomly). I currently open the PDF file and do a File → Save As... naming it with a different name. Is it possible to delete the original file that was opened after doing a File → Save As...?

My current practice is to open the randomly named PDF file, do a File → Save As... (based on info contained in the PDF) and then deleting the randomly named file.

So file name is EFAXB1_2107221804310250.pdf. I open the file and do a Save As so now I have the new recently named file and the original PDF file. It would be awesome to automatically delete the previous file.
User avatar
mikeyww
Posts: 26937
Joined: 09 Sep 2014, 18:38

Re: Delete original PDF when doing a File → Save As

27 Jul 2021, 17:34

Code: Select all

dir := A_ScriptDir, control := "Edit1"
#IfWinActive ahk_exe Acrobat.exe
F12::
InputBox, new, New file name, Enter a new file name.,, 300, 125
If (ErrorLevel || new = "")
 Return
Send ^+s ; Save as...
WinWaitActive, Save As PDF ahk_class #32770 ahk_exe Acrobat.exe,, 40
If ErrorLevel {
 MsgBox, 48, Error, An error occurred while waiting for the window. Aborting.
 Return
} Else Sleep, 1000
ControlGetText, old, %control%
Control, EditPaste, %new%, %control%
Send `n
Sleep, 1000
If FileExist(dir "\"  new ".pdf")
 FileRecycle, %dir%\%old%
Else MsgBox, 48, Error, The new file was not found.`n`n%new%.pdf
SoundBeep, 1500
Return
#IfWinActive
jarhead
Posts: 151
Joined: 09 Sep 2020, 12:43

Re: Delete original PDF when doing a File → Save As

28 Jul 2021, 07:39

This is awesome and I appreciate your time spent.

Currently, the script prompts me for the new file name. I enter the new name, the save as dialogue appears and it is saved out with the new name. However, the old file is not deleted.

The only thing I changed in the script is the directory. Any ideas?
User avatar
mikeyww
Posts: 26937
Joined: 09 Sep 2014, 18:38

Re: Delete original PDF when doing a File → Save As

28 Jul 2021, 09:13

If you do not see the message box about "file was not found", then it means that the file path is wrong, or perhaps the file is open by another program at the same time, or perhaps you do not have access rights to delete the file. Identifying the problem should be straightforward by examining these possibilities. You can try running the script as admin if access rights seems that it might be one of the possibilities. You can also have the script display the file path to ensure that it is correct. You can add a new line to display whether the file exists. You might also try increasing the final sleep. If you are trying to delete the old file while Adobe is still accessing it, the deletion might fail, so this is why increasing the final sleep might be needed. Among all of the possibilities, my guess would be that the final sleep is currently too short.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: mikeyww, RussF and 193 guests