How do I write a batch file that will delete all temporary files (.tmp, doc, etc.) in a folder and its subfolders

Posted on 2011-05-13
Last Modified: 2012-08-13
I am trying to write a batch file that deletes all temporary files, hidden and not hidden, in a folder and its subfolders . The batch file I have started is like this.

Del C:\Temp\ /S ~*.tmp

What this ends up doing is deleting every file in every folder in Temp. What do I need to do to make this only delete temporary files (doc, tmp, etc), hidden and not hidden? I have tried the /AH command and this essentially does the same thing, but deleting every hidden file instead.

Thanks for the help,
Question by:FCB_IT
    LVL 8

    Expert Comment

    This clears out all temp stuff on all user profiles on your local machine.  Just amend your starting folder..

    @echo off

    Rem Delete all temporary stuff
    Rem _________________________

    echo Ensure all applications are closed as work may be lost.

    rem Clear all local user "Temporary Internet Files"
    FOR /F %%A IN ('DIR/B "C:\Documents and Settings"') DO DEL/S/F/Q "C:\Documents and Settings\%%A\Local Settings\Temporary Internet Files\*.*"
    FOR /F %%A IN ('DIR/B "C:\Documents and Settings"') DO RD/S/Q "C:\Documents and Settings\%%A\Local Settings\Temporary Internet Files\"

    rem Clear Local User Temp files
    FOR /F %%A IN ('DIR/B "C:\Documents and Settings"') DO DEL/S/F/Q "C:\Documents and Settings\%%A\Local Settings\Temp\*.*"
    FOR /F %%A IN ('DIR/B "C:\Documents and Settings"') DO RD/S/Q "C:\Documents and Settings\%%A\Local Settings\Temp\"

    rem Clear Windows Temp folders
    DEL/S/F/Q c:\windows\temp
    DEL/S/F/Q c:\windows\prefetch

    Author Comment

    Sorry, I should have been a little more specific. I am trying to clean up a shared folder that many users store Words, Excel, etc docs in. I just need to delete the temp files from that folder and its subfolders and maintain the normal word docs.

    Markie, would that batch you wrote work in this case?
    LVL 8

    Accepted Solution

    Near enough as you had it

    DEL C:\TEMP\*.tmp /S

    Author Comment

    That took care of it. I see my syntax error now. Thanks for your help!

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    This article was inspired by a question here at Experts Exchange ( The requirements stated in that question are (1) reduce the file size of a large number of…
    I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    732 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

    20 Experts available now in Live!

    Get 1:1 Help Now