Periodically delete top level folders with specific name pattern Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
DavidP
Posts: 225
Joined: 10 Aug 2014, 07:31

Periodically delete top level folders with specific name pattern

12 Jun 2021, 05:45

Hello Forum,

what would be a good approach for an AHK script that scans all drive letters from D-Z every few seconds to see if there are any folders at the top folder level that contain "Foo" or "Bar" in the folder name, and then deletes those folders accordingly?

Thanks for ideas!
User avatar
DavidP
Posts: 225
Joined: 10 Aug 2014, 07:31

Re: Periodically delete top level folders with specific name pattern

12 Jun 2021, 06:51

Thank you. WatchFolder seems to go a little over my head for the time being.

Couldn't I use something along the lines of:

Code: Select all

if InStr(FileExist("D:\*foobar*"), "D") 
Do something
...and have this looped?

Edit: this would be great, but it seems that the command doesn't support wildcards:

Code: Select all

FileRemoveDir, "D:\*foobar*"
User avatar
boiler
Posts: 16956
Joined: 21 Dec 2014, 02:44

Re: Periodically delete top level folders with specific name pattern

12 Jun 2021, 07:03

You could use Loop, Files with a pattern including wildcards, and you would need to either use that within a loop or with SetTimer to continuously check it. It would be much better to take the time to learn to implement WatchFolder, however.
User avatar
DavidP
Posts: 225
Joined: 10 Aug 2014, 07:31

Re: Periodically delete top level folders with specific name pattern

12 Jun 2021, 07:10

OK thanks.

Am I right that FileRemoveDir doesn't support wildcards -- other than FileDelete, which does for some reason?
User avatar
DavidP
Posts: 225
Joined: 10 Aug 2014, 07:31

Re: Periodically delete top level folders with specific name pattern

12 Jun 2021, 10:05

I put something together now using Loop.

Slowly getting somewhere:

Code: Select all

Loop Files, D:\*.* , D
{
    If InStr(A_LoopFileName, "foo") ; look for name pattern in the file name
	MsgBox, 4, , Filename = %A_LoopFileFullPath%`n`nContinue?
    IfMsgBox, No
        break
	else
		FileRemoveDir, %A_LoopFileFullPath%, 1
}
	MsgBox, Finished
But I fear that I just mass-deleted some folders when trying this script... :wtf:
Last edited by DavidP on 12 Jun 2021, 10:38, edited 1 time in total.
User avatar
mikeyww
Posts: 26939
Joined: 09 Sep 2014, 18:38

Re: Periodically delete top level folders with specific name pattern

12 Jun 2021, 10:38

See Loop, Files. Mode would be D for directories.

Most directory names do not contain a dot, so you can use * for your directory mask.
Last edited by mikeyww on 12 Jun 2021, 10:44, edited 2 times in total.
User avatar
DavidP
Posts: 225
Joined: 10 Aug 2014, 07:31

Re: Periodically delete top level folders with specific name pattern

12 Jun 2021, 10:40

Can I exclude files with Loop, Files?

The docs seem not to be clear about this :?
Mode
If blank or omitted, only files are included and subdirectories are not recursed into. Otherwise, specify one or more of the following letters:

D = Include directories (folders).
F = Include files. If both F and D are omitted, files are included but not folders.
R = Recurse into subdirectories (subfolders). If R is omitted, files and folders in subfolders are not included.
User avatar
boiler
Posts: 16956
Joined: 21 Dec 2014, 02:44

Re: Periodically delete top level folders with specific name pattern

12 Jun 2021, 10:43

Yes, it might seem that “include” means not exclusive, but it is worded that way because you can include both files and directories using FD. But D alone would include only directories.
User avatar
mikeyww
Posts: 26939
Joined: 09 Sep 2014, 18:38

Re: Periodically delete top level folders with specific name pattern

12 Jun 2021, 10:45

Recycling is generally safe.

Code: Select all

FileRecycle, %A_LoopFileFullPath%
But:
The file may be permanently deleted without warning if the file cannot be recycled for other reasons, such as:
•The file is on a removable drive.
•The Recycle Bin has been disabled, such as via the NukeOnDelete registry value.
Last edited by mikeyww on 12 Jun 2021, 10:46, edited 1 time in total.
User avatar
DavidP
Posts: 225
Joined: 10 Aug 2014, 07:31

Re: Periodically delete top level folders with specific name pattern

12 Jun 2021, 10:45

Thanks!
DavidP wrote:
12 Jun 2021, 10:05
But I fear that I just mass-deleted some folders when trying this script... :wtf:
Was my "IfMsgBox, No... else..." query correct?
User avatar
mikeyww
Posts: 26939
Joined: 09 Sep 2014, 18:38

Re: Periodically delete top level folders with specific name pattern

12 Jun 2021, 10:48

Sure, but if Instr is zero, then the directory would be deleted. Perhaps:

Code: Select all

dir = %A_ScriptDir%
Loop, Files, %dir%\*foo*, D
{
 SoundBeep, 1500
 MsgBox, 36,, Filename = %A_LoopFileFullPath%`n`nContinue?
 IfMsgBox, No
  Break
 FileRecycle, %A_LoopFileFullPath%
}
MsgBox, 64, Done, Finished!
Return
If applies to the subsequent block in braces. Without a block, it applies only to the following line.
User avatar
DavidP
Posts: 225
Joined: 10 Aug 2014, 07:31

Re: Periodically delete top level folders with specific name pattern

12 Jun 2021, 11:14

Thanks guys. Am now in "accidentally mass-deleted files" mode for a while, and will then come back
User avatar
DavidP
Posts: 225
Joined: 10 Aug 2014, 07:31

Re: Periodically delete top level folders with specific name pattern

12 Jun 2021, 12:11

Before I continue trying, please have a look at that previous version of the script:

Code: Select all

Loop Files, D:\*.* 
{
    If InStr(A_LoopFileName, "foo") ; look for name pattern in the file name
	MsgBox, 4, , Filename = %A_LoopFileFullPath%`n`nContinue?
    IfMsgBox, No
        break
	else
		FileRemoveDir, %A_LoopFileFullPath%, 1
}
	MsgBox, Finished
Am I correct that this script would have deleted every top level folder NOT containing "foo" without warning, when run long enough (and I did let it run for like 20 seconds) :eh:

However, could it actually happen that the above script has mass-deleted some contents of some of those top level folders BEFORE actually deleting the top level folder itself? It seems that this has happened because some of the top level folders under D:\ seem to be much smaller than before, but I am not totally sure yet...
User avatar
boiler
Posts: 16956
Joined: 21 Dec 2014, 02:44

Re: Periodically delete top level folders with specific name pattern

12 Jun 2021, 12:29

A couple problems:

1. You’re in danger of deleting files because you didn’t specify the D option in the loop statement. Well, since you’re using FileRemoveDir, probably not, but it’s still wrong because it’s going to ask about files as well as folders.

2. The structure of your script flow is not correct. The IfMsgBox line is going to be executed whether or not the first If statement is true or false, which means you could be deleting files and directories you that don’t have foo in them. That script is very dangerous.
Last edited by boiler on 12 Jun 2021, 12:31, edited 1 time in total.
User avatar
DavidP
Posts: 225
Joined: 10 Aug 2014, 07:31

Re: Periodically delete top level folders with specific name pattern

12 Jun 2021, 12:31

mikeyww wrote:
12 Jun 2021, 10:48

Code: Select all

dir = %A_ScriptDir%
Loop, Files, %dir%\*foo*, D
{
 SoundBeep, 1500
 MsgBox, 36,, Filename = %A_LoopFileFullPath%`n`nContinue?
 IfMsgBox, No
  Break
 FileRecycle, %A_LoopFileFullPath%
}
MsgBox, 64, Done, Finished!
Return
Wow Mike, this works perfect.
Image
User avatar
DavidP
Posts: 225
Joined: 10 Aug 2014, 07:31

Re: Periodically delete top level folders with specific name pattern

12 Jun 2021, 12:32

boiler wrote:
12 Jun 2021, 12:29
That script is very dangerous.
Thank you, will check with my backups what has been deleted!
User avatar
DavidP
Posts: 225
Joined: 10 Aug 2014, 07:31

Re: Periodically delete top level folders with specific name pattern

12 Jun 2021, 13:10

DavidP wrote:
12 Jun 2021, 12:11

Code: Select all

Loop Files, D:\*.* 
{
    If InStr(A_LoopFileName, "foo") ; look for name pattern in the file name
	MsgBox, 4, , Filename = %A_LoopFileFullPath%`n`nContinue?
    IfMsgBox, No
        break
	else
		FileRemoveDir, %A_LoopFileFullPath%, 1
}
	MsgBox, Finished
Am I correct that this script would have deleted every top level folder NOT containing "foo" without warning, when run long enough (and I did let it run for like 20 seconds) :eh:

However, could it actually happen that the above script has mass-deleted some contents of some of those top level folders BEFORE actually deleting the top level folder itself? It seems that this has happened because some of the top level folders under D:\ seem to be much smaller than before, but I am not totally sure yet...
Uuh, it looks like this script has deleted at least some 2,800 folders with some 58,000 files (48 GB) in about 10 seconds.

Is that even possible, technically? I mean, it would take much longer to delete that much data using Windows Explorer!
Last edited by DavidP on 12 Jun 2021, 13:23, edited 2 times in total.
User avatar
boiler
Posts: 16956
Joined: 21 Dec 2014, 02:44

Re: Periodically delete top level folders with specific name pattern

12 Jun 2021, 13:13

Copying files takes a while. It seems that deleting stuff would be really fast because it just sets a flag, basically. It’s not like it has to move those files anywhere.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Joey5 and 255 guests