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!
Periodically delete top level folders with specific name pattern Topic is solved
Re: Periodically delete top level folders with specific name pattern
Thank you. WatchFolder seems to go a little over my head for the time being.
Couldn't I use something along the lines of:
...and have this looped?
Edit: this would be great, but it seems that the command doesn't support wildcards:
Couldn't I use something along the lines of:
Code: Select all
if InStr(FileExist("D:\*foobar*"), "D")
Do something
Edit: this would be great, but it seems that the command doesn't support wildcards:
Code: Select all
FileRemoveDir, "D:\*foobar*"
Re: Periodically delete top level folders with specific name pattern
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.
Re: Periodically delete top level folders with specific name pattern
OK thanks.
Am I right that FileRemoveDir doesn't support wildcards -- other than FileDelete, which does for some reason?
Am I right that FileRemoveDir doesn't support wildcards -- other than FileDelete, which does for some reason?
Re: Periodically delete top level folders with specific name pattern
That does appear to be true.
Re: Periodically delete top level folders with specific name pattern
I put something together now using Loop.
Slowly getting somewhere:
But I fear that I just mass-deleted some folders when trying this script...
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
Last edited by DavidP on 12 Jun 2021, 10:38, edited 1 time in total.
Re: Periodically delete top level folders with specific name pattern
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.
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.
Re: Periodically delete top level folders with specific name pattern
Can I exclude files with Loop, Files?
The docs seem not to be clear about this
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.
Re: Periodically delete top level folders with specific name pattern
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.
Re: Periodically delete top level folders with specific name pattern
Recycling is generally safe.
But:
Code: Select all
FileRecycle, %A_LoopFileFullPath%
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.
Re: Periodically delete top level folders with specific name pattern
Sure, but if Instr is zero, then the directory would be deleted. Perhaps:
If applies to the subsequent block in braces. Without a block, it applies only to the following line.
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
Re: Periodically delete top level folders with specific name pattern
Thanks guys. Am now in "accidentally mass-deleted files" mode for a while, and will then come back
Re: Periodically delete top level folders with specific name pattern
Before I continue trying, please have a look at that previous version of the script:
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)
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...
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
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...
Re: Periodically delete top level folders with specific name pattern
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.
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.
Re: Periodically delete top level folders with specific name pattern
Wow Mike, this works perfect.mikeyww wrote: ↑12 Jun 2021, 10:48Code: 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
Re: Periodically delete top level folders with specific name pattern
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.DavidP wrote: ↑12 Jun 2021, 12:11Am 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)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
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...
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.
Re: Periodically delete top level folders with specific name pattern
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.
Who is online
Users browsing this forum: Joey5 and 255 guests