Script Help: Delete Last File on F9 Press

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
CrowexBR
Posts: 12
Joined: 15 Apr 2024, 06:57

Script Help: Delete Last File on F9 Press

06 May 2024, 08:07

Could someone please help me with a script to delete the last file in the directory
C:\Users\crowe\OneDrive\Pictures\Screenshots when I press {F9}?

Thank you.
User avatar
mikeyww
Posts: 27169
Joined: 09 Sep 2014, 18:38

Re: Script Help: Delete Last File on F9 Press

06 May 2024, 08:18

Hello,

How is "last file" defined?
User avatar
CrowexBR
Posts: 12
Joined: 15 Apr 2024, 06:57

Re: Script Help: Delete Last File on F9 Press

06 May 2024, 09:50

mikeyww wrote:
06 May 2024, 08:18
Hello,

How is "last file" defined?
For example, I clicked "win + printscreen" to take a screenshot of the screen:

printscreen01.png
printscreen02.png
printscreen03.png

And then I press {F9} so that I can find the last file to delete, which would be printscreen03.png

Got it?
User avatar
mikeyww
Posts: 27169
Joined: 09 Sep 2014, 18:38

Re: Script Help: Delete Last File on F9 Press

06 May 2024, 10:02

Are you referring to the file that is last in alphanumeric order by filename?
User avatar
Seven0528
Posts: 393
Joined: 23 Jan 2023, 04:52
Location: South Korea
Contact:

Re: Script Help: Delete Last File on F9 Press

06 May 2024, 10:11

 Wouldn't it be a good idea to use Loop Files to obtain the file path with the most recent A_LoopFileTimeCreated value?
(Anyway, I'm just realizing now that screenshots are being saved to OneDrive...)
  • English is not my native language. Please forgive any awkward expressions.
  • 영어는 제 모국어가 아닙니다. 어색한 표현이 있어도 양해해 주세요.
User avatar
CrowexBR
Posts: 12
Joined: 15 Apr 2024, 06:57

Re: Script Help: Delete Last File on F9 Press

06 May 2024, 11:06

mikeyww wrote:
06 May 2024, 10:02
Are you referring to the file that is last in alphanumeric order by filename?
Yes, I meant the file that was created most recently in terms of order of creation date.
So, I can delete the most recently created file this way.
User avatar
mikeyww
Posts: 27169
Joined: 09 Sep 2014, 18:38

Re: Script Help: Delete Last File on F9 Press

06 May 2024, 11:21

Code: Select all

#Requires AutoHotkey v2.0

F9:: {
 Static shotsDir := StrReplace(A_Desktop, 'Desktop', 'Pictures\Screenshots')
 If FileExist(newest := getNewestFile(shotsDir)) {
  FileRecycle newest
  MsgBox 'Deleted:`n`n' newest, 'Status', 'Iconi'
 } Else MsgBox 'Nothing was deleted.', 'Warning', 'Icon!'
}

getNewestFile(dir) {
 time := 0
 Loop Files dir '\*.*'
  If A_LoopFileTimeCreated > time {
   time  := A_LoopFileTimeCreated
   fPath := A_LoopFilePath
  }
 Return fPath ?? ''
}
User avatar
CrowexBR
Posts: 12
Joined: 15 Apr 2024, 06:57

Re: Script Help: Delete Last File on F9 Press

06 May 2024, 11:55

mikeyww wrote:
06 May 2024, 11:21

Code: Select all

#Requires AutoHotkey v2.0

F9:: {
 Static shotsDir := StrReplace(A_Desktop, 'Desktop', 'Pictures\Screenshots')
 If FileExist(newest := getNewestFile(shotsDir)) {
  FileRecycle newest
  MsgBox 'Deleted:`n`n' newest, 'Status', 'Iconi'
 } Else MsgBox 'Nothing was deleted.', 'Warning', 'Icon!'
}

getNewestFile(dir) {
 time := 0
 Loop Files dir '\*.*'
  If A_LoopFileTimeCreated > time {
   time  := A_LoopFileTimeCreated
   fPath := A_LoopFilePath
  }
 Return fPath ?? ''
}
I got error:
Image
User avatar
mikeyww
Posts: 27169
Joined: 09 Sep 2014, 18:38

Re: Script Help: Delete Last File on F9 Press

06 May 2024, 12:14

Use the latest version posted.

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Bing [Bot] and 35 guests