Simple Temp Folder Cleanup Script in Windows Task Scheduler

This is a simple script to automatically delete temporary files from a folder that are older than 2 weeks. I tend to download and work on a lot of files that don’t necessarily need to be archived after their first use. They can be used on demand in a batch file or, for me, scheduled to run every two days in task scheduler.

Relevant lines – one delete files, one delete folders:

  • forfiles -p “C:\Users\davychiu\Desktop\Temp Files” -s -m *.* -d -15 -c “cmd /c del /q /s @path”
  • forfiles -p “C:\Users\davychiu\Desktop\Temp Files” -d -15 -c “cmd /c if @ISDIR==TRUE echo rd /q /s @path &rd /q /s @path”

Temp Files Maintenance.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.3" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    <Date>2013-07-12T20:45:43.8641683</Date>
    <Author>COMTEK\davychiu</Author>
    <Description>Delete files in the Temp Files folder that are at least 15 days old.</Description>
  </RegistrationInfo>
  <Triggers>
    <CalendarTrigger>
      <StartBoundary>2013-07-12T00:00:00</StartBoundary>
      <Enabled>true</Enabled>
      <ScheduleByDay>
        <DaysInterval>2</DaysInterval>
      </ScheduleByDay>
    </CalendarTrigger>
  </Triggers>
  <Principals>
    <Principal id="Author">
      <UserId>S-1-5-18</UserId>
      <RunLevel>LeastPrivilege</RunLevel>
    </Principal>
  </Principals>
  <Settings>
    <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
    <DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
    <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
    <AllowHardTerminate>true</AllowHardTerminate>
    <StartWhenAvailable>false</StartWhenAvailable>
    <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
    <IdleSettings>
      <StopOnIdleEnd>true</StopOnIdleEnd>
      <RestartOnIdle>false</RestartOnIdle>
    </IdleSettings>
    <AllowStartOnDemand>true</AllowStartOnDemand>
    <Enabled>true</Enabled>
    <Hidden>true</Hidden>
    <RunOnlyIfIdle>false</RunOnlyIfIdle>
    <DisallowStartOnRemoteAppSession>false</DisallowStartOnRemoteAppSession>
    <UseUnifiedSchedulingEngine>false</UseUnifiedSchedulingEngine>
    <WakeToRun>false</WakeToRun>
    <ExecutionTimeLimit>P3D</ExecutionTimeLimit>
    <Priority>7</Priority>
  </Settings>
  <Actions Context="Author">
    <Exec>
      <Command>forfiles</Command>
      <Arguments>-p "C:\Users\davychiu\Desktop\Temp Files" -s -m *.* -d -15 -c "cmd /c del /q /s @path"</Arguments>
    </Exec>
    <Exec>
      <Command>forfiles</Command>
      <Arguments>-p "C:\Users\davychiu\Desktop\Temp Files" -d -15 -c "cmd /c if @ISDIR==TRUE echo rd /q /s @path &amp;rd /q /s @path"</Arguments>
    </Exec>
  </Actions>
</Task>
This entry was posted in Uncategorized and tagged , . Bookmark the permalink.

2 Responses to Simple Temp Folder Cleanup Script in Windows Task Scheduler

  1. Manuel says:

    When I execute this:

    forfiles -p “C:\Windows\Temp” -s -m *.* -d -15 -c “cmd /c del /q /s @path”

    I get an error message:
    “ERROR: No files found with the specified search criteria.”

    In this folder there are a lot files older tan 15 days.

    OS: Windows Web Server 2008 R2

  2. Manuel says:

    Sorry.

    I miss the minus sign after d parameter. Now It works.

    Thanks.

Leave a Reply to Manuel Cancel reply

Your email address will not be published. Required fields are marked *