Posted On March 31, 2019

Script to remove files older than X days

kimconnect 0 comments
blog.KimConnect.com >> Codes >> Script to remove files older than X days
------------- Batch file ------------------------
@echo off
:: set folder path
set dump_path="E:\Shared\APP01\LOGFILES\RatingWebService2\Rating Comments\Production"

:: set min age of files and folders to delete
set max_days=7

:: remove files from %dump_path%
forfiles -p %dump_path% -m *.* -d -%max_days% -c "cmd /c del /q @path"

:: remove sub directories from %dump_path%
There are two methods to achieve this task

forfiles -p %dump_path% -d -%max_days% -c "cmd /c IF @isdir == TRUE rd /S /Q @path"

-------------- Powershell -------------------------
# set folder path
$dump_path = "E:\Shared\APP01\LOGFILES\RatingWebService2\Rating Comments\Production"

# set min age of files
$max_days = "-7"

# get the current date
$curr_date = Get-Date

# determine how far back we go based on current date
$del_date = $curr_date.AddDays($max_days)

# delete the files
Get-ChildItem $dump_path -Recurse | Where-Object { $_.LastWriteTime -lt $del_date } | Remove-Item

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

How To Modify a Windows Service

Here's the freebie code: # Version 0.02 $serviceName='Windows_Exporter' $newExePath=$false $newSwitches=" --log.format logger:eventlog?name=$serviceName --collectors.enabled os,cpu,cs,logical_disk,net,tcp,service,textfile" function…

PowerShell: Microsoft SQL Administration

Script to Install Microsoft Clustering Service $proxy="http://proxy:8080"$exclusionList="localhost;*.kimconnect.com"function checkProxy{try{$connectionTest=iwr download.microsoft.com#$connectionSucceeds=Test-NetConnection -Computername download.microsoft.com -Port 443 -InformationLevel Quietif…

Some Useful Windows Commands to Troubleshoot Networking on Windoze

# Check this computer's trust relationship to its domain controllers$domainName="intranet.kimconnect.com"PS C:\Windows\system32> nltest /SC_QUERY:$domainNameFlags: 0Trusted DC…