Solved

Need a script to delete all files older than 3 days from C:\windows\temp on Windows 2008R2 server

Posted on 2014-11-25
10
160 Views
Last Modified: 2015-05-01
I need to write a script to run as a regular task, most likely from Task Scheduler script that will delete all files from the C:\Windows\temp directory that are older than 3 days. Can anyone assist with how to write that given the "older than" or time relationship needed? I'm thinking it would delete anything older than the current time... How would that be written?
0
Comment
Question by:Noah_Williams35
  • 4
  • 2
  • 2
10 Comments
 
LVL 28

Expert Comment

by:becraig
Comment Utility
Powershell

gci c:\temp -recurse | ? {$_.creationtime -gt (Get-Date).AddDays(-3)} | Remove-Item -recurse -Force -whatif

Open in new window


This should find all 3day old files and force a delete.
0
 
LVL 82

Expert Comment

by:oBdA
Comment Utility
You can use robocopy for that (without really copying anything); the following batch script is in test mode and will only display the files it would normally delete; remove the uppercase ECHO in front of "del" to run it for real:
@echo off
for /f "tokens=1*" %%a in ('robocopy.exe "%Systemroot%\Temp" "C:\Garbage" *.* /minage:3 /r:0 /l /bytes /nc /ndl /njh /njs') do (ECHO del "%%b")

Open in new window

0
 
LVL 82

Expert Comment

by:oBdA
Comment Utility
Oh, and then there's of course the tool I forget on a regular basis: forfiles.exe; again in test mode, same as above:
@echo off
forfiles.exe /p "C:\Windows\Temp" /m *.* /d -3 /c "cmd.exe /c ECHO del @path"

Open in new window

0
 

Author Comment

by:Noah_Williams35
Comment Utility
oBdA,

The forfiles command is great! I forgot about that myself! It asks me to confirm the delete. How do I supress that and just delete, so that it can be automated?

Thank you!
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 82

Expert Comment

by:oBdA
Comment Utility
Oh, sorry. These would be subdirectories found inside the folder in question; do you want subfolders to be removed as well, or rather skip them and process only files?
0
 

Author Comment

by:Noah_Williams35
Comment Utility
Yes, files and subdirectories are to be deleted...
0
 
LVL 28

Expert Comment

by:becraig
Comment Utility
I am guessing you do not need a powershell solution since the script above would do just that.
0
 
LVL 82

Accepted Solution

by:
oBdA earned 500 total points
Comment Utility
This now handles subdirectories; again in test mode (note that there are two ECHOs now):
@echo off
forfiles.exe /p "C:\Windows\Temp" /m * /d -3 /c "cmd /c if @isdir==TRUE (ECHO rd /s /q @path) else (ECHO del /f @path)"

Open in new window

0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

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

6 Experts available now in Live!

Get 1:1 Help Now