Solved

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

Posted on 2011-09-29
4
393 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
ID: 36818499
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
ID: 36818508
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
ID: 36818663
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
ID: 36979282
ty.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When you try to share a printer , you may receive one of the following error messages. Error message when you use the Add Printer Wizard to share a printer: Windows could not share your printer. Operation could not be completed (Error 0x000006…
A quick guide on how to use Group Policy to create a custom power plan and set it active on Windows 7.
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …

920 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

15 Experts available now in Live!

Get 1:1 Help Now