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
168 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 84

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 84

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
Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

 

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 84

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 84

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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
The viewer will learn how to dynamically set the form action using jQuery.

739 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