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

Re: Periodically delete top level folders with specific name pattern

12 Jun 2021, 13:14

So maybe I should start with an undelete tool then...
User avatar
mikeyww
Posts: 26851
Joined: 09 Sep 2014, 18:38

Re: Periodically delete top level folders with specific name pattern

12 Jun 2021, 13:15

I agree. If you do not have a backup, the files can probably be recovered with data recovery ("undelete") software. It takes a while. Would avoid turning off your computer, and avoid creating any new files (yourself), until recovery is done.
User avatar
boiler
Posts: 16902
Joined: 21 Dec 2014, 02:44

Re: Periodically delete top level folders with specific name pattern

12 Jun 2021, 13:19

Good rule of thumb: Never run a script designed to delete a bunch of files and directories, especially at the top level of a drive with a wildcard, without having backed up all files first. This is especially true if you are not experienced in writing such scripts.
User avatar
mikeyww
Posts: 26851
Joined: 09 Sep 2014, 18:38

Re: Periodically delete top level folders with specific name pattern

12 Jun 2021, 13:22

Another one that I use is: don't delete files! Recycle them. I have some rare exceptions but not in scripts. I also do not empty my recycle bin. Instead, I copy files older than 30 days to a trash directory, and then delete that.
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:42

Thanks gents.

Unfortunately, I had already started copying files back from a backup. Because of this, Recuva now tells me that many of the deleted files are overwritten.

However, at least I can see from the list of 360,000 deleted files which Recuva found that I *should* have a current backup for all deleted files under D:/ (*knocks on wood*)
User avatar
DavidP
Posts: 225
Joined: 10 Aug 2014, 07:31

Re: Periodically delete top level folders with specific name pattern

12 Jun 2021, 14:51

Shouldn't this run indefinitely? It however quits after looping through all directories once :eh:

Code: Select all

SetTimer, H-Disk, 11111
H-Disk:
Loop, Files, H:\*foo*, D 
FileRecycle, %A_LoopFileFullPath%
Return
Also, I still don't understand the parameters of Loop.

The documentation goes
D: Include directories (folders)
... but just below it says:
IncludeFolders?
One of the following digits, or blank to use the default:
0 (default) Folders are not retrieved (only files).
1 All files and folders that match the wildcard pattern are retrieved.
2 Only folders are retrieved (no files).
User avatar
mikeyww
Posts: 26851
Joined: 09 Sep 2014, 18:38

Re: Periodically delete top level folders with specific name pattern

12 Jun 2021, 14:59

"Loop, Files" is not an infinite looping. It loops through all of the matching files once. That is how the command works. To do it infinitely, you can put that loop inside a regular Loop.

You are looking at the deprecated syntax for Loop, Filepattern rather than Loop, Files.
User avatar
DavidP
Posts: 225
Joined: 10 Aug 2014, 07:31

Re: Periodically delete top level folders with specific name pattern

12 Jun 2021, 15:06

mikeyww wrote:
12 Jun 2021, 14:59
To do it infinitely, you can put that loop inside a regular Loop.
Thanks, that works!

Should I put the "SetTimer" inside the outer Loop as well, or use something like "Sleep" instead, in order to run the "Loop, Files" only every so often?
User avatar
boiler
Posts: 16902
Joined: 21 Dec 2014, 02:44

Re: Periodically delete top level folders with specific name pattern

12 Jun 2021, 15:10

SetTimer should not be in a loop nor paired with Sleep. The SetTimer command itself runs the indicated subroutine on a repeating basis, with the period between each iteration as specified in the command. See the linked documentation for further instructions on its use.
User avatar
DavidP
Posts: 225
Joined: 10 Aug 2014, 07:31

Re: Periodically delete top level folders with specific name pattern

12 Jun 2021, 15:13

But the outer loop runs the inner loop at maximum speed indefinitely this way, doesn't it?

Code: Select all

SetTimer, H-Disk, 11111

H-Disk:
Loop
{
Loop, Files, H:\*foo*, D 
FileRecycle, %A_LoopFileFullPath%
}
Return
User avatar
boiler
Posts: 16902
Joined: 21 Dec 2014, 02:44

Re: Periodically delete top level folders with specific name pattern

12 Jun 2021, 15:16

You shouldn’t put a loop around the inner loop when you’re using SetTimer. Regarding your earlier question, it should run indefinitely (without the extra loop you just added), repeating approximately every 11 seconds.
safetycar
Posts: 435
Joined: 12 Aug 2017, 04:27

Re: Periodically delete top level folders with specific name pattern

12 Jun 2021, 15:17

DavidP wrote:
12 Jun 2021, 14:51
... but just below it says:
IncludeFolders?
One of the following digits, or blank to use the default:
0 (default) Folders are not retrieved (only files).
1 All files and folders that match the wildcard pattern are retrieved.
2 Only folders are retrieved (no files).
Take care with that you are mixing old and new syntax (look at the headers and the deprecated warning).
The old syntax is there as information for people that needs to do maintenance on old code.
User avatar
boiler
Posts: 16902
Joined: 21 Dec 2014, 02:44

Re: Periodically delete top level folders with specific name pattern

12 Jun 2021, 15:20

mikeyww wrote:
12 Jun 2021, 14:59
"Loop, Files" is not an infinite looping. It loops through all of the matching files once. That is how the command works. To do it infinitely, you can put that loop inside a regular Loop.
The SetTimer should have it repeating indefinitely, right?
User avatar
mikeyww
Posts: 26851
Joined: 09 Sep 2014, 18:38

Re: Periodically delete top level folders with specific name pattern

12 Jun 2021, 15:25

I admit that my comment to use "Loop" was misleading, considering the need of "periodically"! A timer is a better option for that (alternative rather than an addition), as boiler is explaining-- as a timer is designed to meet a periodic need, rather than a continuous one. The timer is not like your kitchen timer. It keeps repeating until stopped.
Last edited by mikeyww on 12 Jun 2021, 15:25, 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, 15:25

boiler wrote:
12 Jun 2021, 15:20
The SetTimer should have it repeating indefinitely, right?
It doesn't ever seem to get to the Timer's "Return", instead quits?
User avatar
mikeyww
Posts: 26851
Joined: 09 Sep 2014, 18:38

Re: Periodically delete top level folders with specific name pattern

12 Jun 2021, 15:27

Code: Select all

#Persistent
SetTimer, H-Disk, 11111
H-Disk:
Loop, Files, H:\*foo*, D
 FileRecycle, %A_LoopFilePath%
Return
Without #Persistent, the script exits!
A script is persistent if any of the following conditions are true:
At least one hotkey or hotstring has been defined in the script or created by the Hotkey command or Hotstring function, even if it is not enabled.
The keyboard hook or mouse hook is installed.
The script contains any use of Gui, even if it has not been called.
The script contains any use of OnMessage, or has called it dynamically or retrieved a reference with Func.
The Input command has been called.
The #Persistent directive is present anywhere in the script.
Last edited by mikeyww on 12 Jun 2021, 15:29, edited 2 times in total.
User avatar
boiler
Posts: 16902
Joined: 21 Dec 2014, 02:44

Re: Periodically delete top level folders with specific name pattern

12 Jun 2021, 15:28

mikeyww wrote:
12 Jun 2021, 15:27
Without #Persistent, the script exits!
Good catch!
User avatar
DavidP
Posts: 225
Joined: 10 Aug 2014, 07:31

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

12 Jun 2021, 15:30

Now its perfect!

Code: Select all

#Persistent
SetTimer, H-Disk, 11111
H-Disk:
SoundBeep, 1500
Loop, Files, H:\*foo*, D 
FileRecycle, %A_LoopFileFullPath%
Return
Takes about 10 seconds to recycle 10 GB (3,000 folders; 30,000 files)

Many thanks to the forum and to mikeyww for the proposed solution.
Last edited by DavidP on 13 Jun 2021, 05:11, 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, 15:40

When using FileRemoveDir or FileRecycle, can I maximize the effect (i.e. force delete even when write-protected, or wrong credentials etc.) while making sure that under no circumstances the deletion throws an error message should it fail for some of the files/folders?
User avatar
mikeyww
Posts: 26851
Joined: 09 Sep 2014, 18:38

Re: Periodically delete top level folders with specific name pattern

12 Jun 2021, 16:15

If needed, you can run as admin or change file attributes. I would do so with extreme caution. You can also check ErrorLevel to know what failed. In certain situations, running as admin actually disables visibility of certain kinds of drives.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Archimede, Draken, haomingchen1998, ReyAHK and 256 guests