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
171 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
[X]
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
  • 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 85

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 85

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
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 

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 85

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 85

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
The viewer will learn how to count occurrences of each item in an array.
Simple Linear Regression

734 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