Solved

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

Posted on 2011-09-29
4
390 Views
Last Modified: 2012-05-12
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.
0
Comment
Question by:TonyReba
  • 2
4 Comments
 
LVL 9

Accepted Solution

by:
jsdray earned 250 total points
Comment Utility
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
 
LVL 9

Expert Comment

by:jsdray
Comment Utility
step 3 of the link is where you'll browse to this .bat file you just created
0
 
LVL 9

Assisted Solution

by:chrismerritt
chrismerritt earned 250 total points
Comment Utility
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
 
LVL 9

Author Closing Comment

by:TonyReba
Comment Utility
ty.
0

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly‚ÄĒanytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

Utilizing an array to gracefully append to a list of EmailAddresses
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
This Micro Tutorial will teach you the basics of configuring your computer to improve its speed. It will also teach you how to disable programs that are running in the background simultaneously. This will be demonstrated using Windows 7 operating…
This Micro Tutorial will give you a basic overview of Windows Live Photo Gallery and show you various editing filters and touches to photos you can apply. This will be demonstrated using Windows Live Photo Gallery on Windows 7 operating system.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now