Schedule task in Windows to delete files from specific folder once it reaches a date

I need to create a scheduled task to delete the contents of a specific folder everyday.  I need help with the scrip to do this and the steps on windows server to accomplish this  Thanks in advance for the help.
LVL 9
TonyRebaAsked:
Who is Participating?
 
jsdrayConnect With a Mentor Commented:
cut and paste this to notepad...save as myscript.bat

echo off
cls
set myFolder="c:\temp"

del /q /s %MyFolder%\*.*

then go here for directions on the scheduled task: http://www.hosting.com/support/windows-server-2003/create-a-scheduled-task-in-windows-server-2003
0
 
jsdrayCommented:
step 3 of the link is where you'll browse to this .bat file you just created
0
 
chrismerrittConnect With a Mentor Commented:
Batch is so ugly :P

PowerShell variation below.

Put this into a .ps1 file:

$Folder = "C:\TEMP"
$CutOffDate = (Get-Date).AddDays(-1)

$Items = Get-ChildItem $Folder | ? {$_.PSIsContainer -eq $False}

foreach ($Item in $Items)
{
	if ($Item.LastWriteTime -lt $CutOffDate)
	{
		$Item.Delete()
	}
}

Open in new window


Then put this into a .BAT file, replacing the file path with the one you saved above:

PowerShell.exe -Command "& {C:\Folder\yourfile.ps1}"

Open in new window


Then add a new scheduled task and tell it to run the batch file for the task. I'm assuming you have PowerShell installed.

You may need to enable the right execution policy to exec .ps1 scripts which you can do by running the command from this URL http://technet.microsoft.com/en-us/library/ee176949.aspx

Command is Set-ExecutionPolicy RemoteSigned from a Powershell.exe prompt which has been run as admin if you are lazy :)

0
 
TonyRebaAuthor Commented:
ty.
0
All Courses

From novice to tech pro — start learning today.