JNietert
JNietert

Reputation: 21

How to do multiple actions with AutoHotKey

I haven't used AHK before so it's probably a typographical error but would love any help. I'm trying to click into the search bar on File Explorer, search for the phrase .pdf and then select all of the files. Then I want to click the back button so I can go to the parent folder and then paste the files. Lastly, I want to click home and then click the folder at the top and delete it. The purpose for all of this is to take all the PDFs out of the subfolders and put them into the main folder, and then delete the subfolders.

I've tried this.

^e::
Click 3510,201
Send, *.pdf*{Enter} ^a ^x
Click 2601,200
^v
Send {Home}
Click 2896,266
Send {Delete}
Return

Currently, when I try it, it deletes all of the files in my folder.

Upvotes: 1

Views: 879

Answers (1)

user10364927
user10364927

Reputation:

There appear to be a few issues with your code and addressing each may make your code work, but I suggest a different, more robust, approach.

Try using a file-loop to find all files in a location (and sub-locations) with the PDF extension and FileMove to move these to your desired location.

https://www.autohotkey.com/docs/commands/LoopFile.htm
https://www.autohotkey.com/docs/commands/FileMove.htm

Edit (per comments)
There are a few ways to grab the path from file explorer. The easiest I can think is to send a ^{f4} which will select the full path, from there you can send a ^c copy it to the clipboard and use it for the file-loop path.

Edit2 (working example)

f1::
WinGetClass , vClass , A
If !(vClass = "CabinetWClass") ; Stops here if file explorer isn't the active window
    Return
clipboard := ""
Send , ^{f4}{esc}^c
ClipWait , 1
If ErrorLevel ; Stops here if nothing was copied
    Return
Loop , Files , % clipboard . "\*.pdf" , R
    FileMove , %A_LoopFileLongPath% , %clipboard%
Return

Note that this will fail for named locations like "Documents", Downloads", or "This PC"; it must be a full path to work. Also, this will not delete the subfolders, but see FileRemoveDir for help with that.
https://www.autohotkey.com/docs/commands/FileRemoveDir.htm

Edit3 (using FileRemoveDir to delete empty folders)
This snippet will delete empty folders, starting with the deepest level and working to the top level. I take no credit for this as it by SKAN from the original AHK forums.

SetBatchLines -1
FileSelectFolder, Folder, , % (Del:=0), Purge Empty Folders
If ( ErrorLevel Or Folder="" )
  Return
Loop, %Folder%\*, 2, 1
  FL .= ((FL<>"") ? "`n" : "" ) A_LoopFileFullPath
Sort, FL, R D`n ; Arrange folder-paths inside-out
Loop, Parse, FL, `n
{
  FileRemoveDir, %A_LoopField% ; Do not remove the folder unless is  empty
  If ! ErrorLevel
       Del := Del+1,  RFL .= ((RFL<>"") ? "`n" : "" ) A_LoopField
}
MsgBox, 64, Empty Folders Purged : %Del%, %RFL%

This can be adapted to fit the example from Edit2 and you should be good to go!

Upvotes: 2

Related Questions