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

Posted on 2011-09-29
Medium Priority
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.
Question by:TonyReba
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2

Accepted Solution

jsdray earned 1000 total points
ID: 36818499
cut and paste this to notepad...save as myscript.bat

echo off
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

Expert Comment

ID: 36818508
step 3 of the link is where you'll browse to this .bat file you just created

Assisted Solution

chrismerritt earned 1000 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)

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 :)


Author Closing Comment

ID: 36979282

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
This article provides a convenient collection of links to Microsoft provided Security Patches for operating systems that have reached their End of Life support cycle. Included operating systems covered by this article are Windows XP,  Windows Server…
This Micro Tutorial will give you basic overview of the control panel section on Windows 7. It will depth in Network and Internet, Hardware and Sound, etc. This will be demonstrated using Windows 7 operating system.
This Micro Tutorial will give you a introduction in two parts how to utilize Windows Live Movie Maker to its maximum capability. This will be demonstrated using Windows Live Movie Maker on Windows 7 operating system.
Suggested Courses

752 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