Automatically Delete Files/Folders

Automatically Delete Files from Google Shared Drive Older than X days?

function delFilesInFolderOlderThanXday() {
const folder=DriveApp.getFolderById('id');
const files=folder.getFiles();
const dt=new Date();
const threshold=new Date(dt.getFullYear(),dt.getMonth(),dt.getDate()-X).valueOf();
while(files.hasNext()) {
let file=files.next();
let dtv=new Date(file.getLastUpdated()).valueOf();
if(dtv<threshold) {
Drive.Files.remove(file.getId(),{supportsAllDrives:true});
}
}
}

You will have to enable Drive API if you wish to delete. Otherwise you can put them in trash with DriveApp. You will also have to set X.

I tested this code as of Feb 4, 2022 and it is still working

Actually to be clear I tested this code because I just wanted to make sure that Drive.Files.remove() was still working:

function delFilesInFolderOlderThanXday() {
const folder = DriveApp.getFolderById('11wTZxbF9r-WlMK1uJlbcbmol7UcDV2X1');
const files = folder.getFiles();
const dt = new Date();
while (files.hasNext()) {
let file = files.next();
Drive.Files.remove(file.getId(), { supportsAllDrives: true });
}
}

Automatically Delete Files/Folders

Maybe you're just looking for a combination of file.remove and list.files? Maybe something like:

do.call(file.remove, list(list.files("C:/Temp", full.names = TRUE)))

And I guess you can filter the list of files down to those whose names match a certain pattern using grep or grepl, no?

Automatically delete files in storage

For automatically delete all these files in blob storage, you can use the Lifecycle Management of blob storage.

It's easy to set up a rule and filter, after the rule is set up, all the files will be deleted as per the rule you defined.

Simple steps:

1.Nav to azure portal -> your storage account -> Blob services -> Lifecycle Management, then click "Add rule".

Sample Image

2.In the "Action set" tab, select Delete blob and fill in the textbox; Then in "Filter set" tab, select a path.

Sample Image

For more details/instructions, please follow this article.

Also note that the rule runs once per day, and for the first time, it may take 24 hours to take effect.

how to auto delete file in drive d:if creation time 30 days vb.net

fileName seems to only have the file name. You need the full path to Delete.

Dim eachFileInMydirectory As New DirectoryInfo("C:\MyFolder")
Dim fileName As IO.FileInfo

For Each fileName In eachFileInMydirectory.GetFiles
If fileName.Extension.Equals(".txt") AndAlso (Now - fileName.CreationTime).Days > 8 Then
Dim fullPath As String = Path.Combine("C:\MyFolder", fileName.ToString)
File.Delete(fullPath)
End If
Next


Related Topics



Leave a reply



Submit