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
166 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 29

Expert Comment

by:becraig
ID: 40465197
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 83

Expert Comment

by:oBdA
ID: 40465269
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 83

Expert Comment

by:oBdA
ID: 40465298
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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:Noah_Williams35
ID: 40465375
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
 
LVL 83

Expert Comment

by:oBdA
ID: 40465400
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
ID: 40465426
Yes, files and subdirectories are to be deleted...
0
 
LVL 29

Expert Comment

by:becraig
ID: 40465433
I am guessing you do not need a powershell solution since the script above would do just that.
0
 
LVL 83

Accepted Solution

by:
oBdA earned 500 total points
ID: 40465435
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

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.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
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.

772 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