Please note that VisualCron support is not actively monitoring this community forum. Please use our contact page for contacting the VisualCron support directly.


rainhailrob
2012-10-24T20:40:59Z
Has anyone used VisualCron to delete files based on 10 days prior to today's date? Also need to try to setup another job to delete all but the most recent 10 files, has anyone done this before with VisualCron? The files are located on UNC path (NAS device).
Sponsor
Forum information
bbusse
2012-10-24T23:52:09Z
Originally Posted by: rainhailrob 

Has anyone used VisualCron to delete files based on 10 days prior to today's date? Also need to try to setup another job to delete all but the most recent 10 files, has anyone done this before with VisualCron? The files are located on UNC path (NAS device).



The first question is quite easy to do directly within visualcron using the Delete File(s) task, the 2nd question I think you'd have to use a custom powershell (or other language) script to do the work for you.

For the first question, deleting files older than 10 days, all you have to do is add a new 'Delete Files' task to your job.

Enter your folder and change the file mask from * to *.txt or *.tif, etc.. (if you need to).

Under the 'Date' tab, enter a variable for a date 10 days ago.

October 14 = {DATENOWADD(Days|-10|M)}
10 Days prior to RIGHT NOW = {DATENOWADD(Days|10|)}

See Screenshot.

Brian
bbusse attached the following image(s):
bbusse
2012-10-25T00:02:46Z
For question #2,

Option 1: Using powershell code for deleting all files only keeping the most recent 10 (based on creation time):

NOTE: Remove the '-Recurse' piece if you do not want it to search subdirectories


$Path = 'c:\Temp\'

$files = Get-ChildItem -Path $path -Recurse | Where-Object {-not $_.PsIsContainer}
$keep = 10
if ($files.Count -gt $keep) 
	{
	$files | Sort-Object CreationTime | Select-Object -First ($files.Count - $keep) | Remove-Item -Force
	}



Option 2: Using powershell code for deleting all files only keeping the most recent 10 (based on Last Modified):

NOTE: Remove the '-Recurse' piece if you do not want it to search subdirectories


$Path = 'c:\Temp\'

$files = Get-ChildItem -Path $path -Recurse | Where-Object {-not $_.PsIsContainer}
$keep = 10
if ($files.Count -gt $keep) 
	{
	$files | Sort-Object LastWriteTime | Select-Object -First ($files.Count - $keep) | Remove-Item -Force
	}



Hopefully this helps.

Brian
Scroll to Top