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".
2.In the "Action set" tab, select Delete blob and fill in the textbox; Then in "Filter set" tab, select a path.
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
How to Search for "R" Materials
Use Trycatch Skip to Next Value of Loop Upon Error
Using R to List All Files with a Specified Extension
Fitting Polynomial Model to Data in R
Remove All Punctuation Except Apostrophes in R
Dplyr Mutate Rowwise Max of Range of Columns
If/Else Constructs Inside and Outside Functions
R Split Numeric Vector at Position
Subsetting Data.Table by 2Nd Column Only of a 2 Column Key, Using Binary Search Not Vector Scan
Gsub() in R Is Not Replacing '.' (Dot)
Error in Grid.Call(L_Textbounds, As.Graphicsannot(X$Label), X$X, X$Y,:Polygon Edge Not Found
How to Paste a String on Each Element of a Vector of Strings Using Apply in R
Use Stat_Summary to Annotate Plot with Number of Observations
Label and Color Leaf Dendrogram
How to Draw a Line Across a Multiple-Figure Environment in R