Results 1 to 12 of 12
  1. #1
    5 Star Lounger
    Join Date
    May 2001
    Location
    Allen, Texas, USA
    Posts
    727
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Batch file to delete Windows temp files

    I am in need of a bat file to run on each of our work PC's to run from the Task Scheduler at night to delete all of the Windows temp files and folders at: C:\Users\[username]\AppData\Local\Temp. Is that a recommended practive to do on a regular basis?

    How is C:\Windows\Temp used differently than the Local\temp folder?
    Is it OK to delete all of the folders in both directories?
    How would a delete function deal with files that are open or in use? It needs to ignore those.

    Would this work? Is there a way to confirm that it's in the correct path? Sure wouldn't want it to do a delete all in the wrong folder by accident.

    CD C:\Users\[username]\AppData\Local\Temp
    del %TEMP%\*.* /f /s /q

    Thanks!
    KZ

  2. #2
    5 Star Lounger
    Join Date
    May 2001
    Location
    Allen, Texas, USA
    Posts
    727
    Thanks
    1
    Thanked 0 Times in 0 Posts
    ...Or is it simpler to just run CCleaner and only choose the items desired to clean?

  3. #3
    WS Lounge VIP
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    8,199
    Thanks
    48
    Thanked 986 Times in 916 Posts
    I have never bothered with the temp directories on work PCs because they use relatively small amounts of data, but you do need to scan them as users inevitably manage to get viruses from web sites they shouldn't be visiting. The biggest data use, in my experience, is music and video files stored on local PCs / servers - users seem to think the PC is theirs and it's OK to download / save all sorts of crud on the box provided to allow them to do the work they are paid for. Some of the biggest culprits are the IT staff!

    cheers, Paul

  4. #4
    Super Moderator
    Join Date
    Aug 2012
    Location
    Durham UK
    Posts
    6,640
    Thanks
    147
    Thanked 883 Times in 844 Posts
    I use CCleaner to clean one side of things and the free version of SAS to clean out %temp% which you can enable in its settings.

    While %temp% doesn't normally get over populated, it can do after downloading drivers and I came across one machine that had close on 3GB in there - don't know how they managed that as it was just a case of "plug & play" for them without any thought of maintenance or checking everything was up to date.

    It was that over population that was causing the laptop to crawl.

    I normally give mine a quick scan with SAS then use CCleaner at the end of each session in that order, as the latter doesn't clean %temp% with default settings.
    Last edited by Sudo15; 2014-10-02 at 12:52.

  5. #5
    Super Moderator BATcher's Avatar
    Join Date
    Feb 2008
    Location
    A cultural area in SW England
    Posts
    3,421
    Thanks
    33
    Thanked 195 Times in 175 Posts
    Code:
    CD C:\Users\[username]\AppData\Local\Temp
    del %TEMP%\*.* /f /s /q
    As it stands your CD statement is of no use, since your DEL statement acts on whatever is in the TEMP environment variable, which should/will be C:\Users\[username]\AppData\Local\Temp.

    If the contents of the TEMP variable are wrong, or null, then Unhappiness May Occur (this would be surprising, but theoretically possible).

    A little bit more safety might be derived from:
    Code:
    if "%temp%"=="C:\Users\%username%\AppData\Local\Temp" del %temp%\*.* /f /s /q
    BATcher

    Time prevents everything happening all at once...

  6. #6
    5 Star Lounger
    Join Date
    May 2001
    Location
    Allen, Texas, USA
    Posts
    727
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks BATcher. That bat file does look a lot safer!
    Thanks Sudo15...good comments.

    Question from above: How is C:\Windows\Temp used differently than the Local\temp folder?

  7. #7
    WS Lounge VIP
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    8,199
    Thanks
    48
    Thanked 986 Times in 916 Posts
    C:\Windows\Temp is used by your OS and users generally don't have access. The local\temp folder is for user created temp files / programs running with user permissions created temp files.

    cheers, Paul

  8. #8
    5 Star Lounger
    Join Date
    May 2001
    Location
    Allen, Texas, USA
    Posts
    727
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks Paul!

  9. #9
    Lounger
    Join Date
    Feb 2014
    Posts
    40
    Thanks
    1
    Thanked 2 Times in 2 Posts
    I use CCleaner set to run at startup. I put the cookies I want to keep in the Keeper(?) file, and delete the rest. Thanks for the tip on SAS, by the way: from now on I run it, too. Except for cookies needed with some of my high-usage programs, none of the cookies are needed. I use Chrome as my only browser and you can go into History and erase ALL your browses and searches. These can build up over time and really slow your PC down! I just wish I knew how to clean out the Page file and make it smaller. I knew how in XP!

  10. #10
    WS Lounge VIP
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    8,199
    Thanks
    48
    Thanked 986 Times in 916 Posts
    Making the page file smaller will probably not have an impact on performance unless it's used often and is badly fragmented. Open Task Manager and check how much memory is used vs how much you have installed.
    If you really want to speed up your PC install the OS on an SSD.

    cheers, Paul

  11. #11
    Bronze Lounger DrWho's Avatar
    Join Date
    Dec 2009
    Location
    Central Florida
    Posts
    1,501
    Thanks
    30
    Thanked 205 Times in 163 Posts
    All the way back in the '98 days, I wrote my first Cleanup.bat program and shared it with all my customers.
    I put it in their Startup folder for a free daily cleanup. That won't get everything, but it goes a long ways toward keeping a PC clean.

    That was then, and this is now! Today my daily cleanup batch file, run from the Startup folder, has almost forty lines in it. It deletes the virus vault folders and even empties the trash bins.

    Any folder in your computer, that stores .tmp or .~mp or .bak files, can usually be deleted safely. Or you may not want to delete an entire folder, but just a particular type of file in a folder, leaving everything else in that folder alone.

    CCleaner? Not in my life! I tried it twice over several years and both times it crashed my OS, demanding that I do a Backup Restore to get my OS working again.
    Unless you're a software Guru and fully understand what that program is doing, don't muck around with it.

    Find the folders in your HD where the junk files are hiding and add those locations to your Cleanup batch file. Don't guess! Look them up!
    For instance.....if you do a search for *.tmp you'll get a list of all the locations where temp files live.
    Then you can write your batch file to incorporate those locations.

    Or, lets say that you keep getting a piece of spyware that installs as a program, called "Vampire".
    You can write a line in your batch file to delete that program every time you run your batch file.

    del /F /S /Q "C:\Program Files\vampire\*.*"
    RD "C:\Program Files\vampire"


    I had to do that for one friend of mine who keeps getting the same spyware program time after time.
    They do have Anti-Spyware software, but they neglect to run it.

    This is really pretty simple stuff, if you just stop and think about it a little. Find the junk and delete the junk!

    Good Luck and Happy Computing!
    The Doctor
    Last edited by DrWho; 2014-10-19 at 09:25.
    Experience is truly the best teacher.

    Backup! Backup! Backup! GHOST Rocks!

  12. #12
    WS Lounge VIP Coochin's Avatar
    Join Date
    Jun 2014
    Location
    Queensland, Australia
    Posts
    2,154
    Thanks
    31
    Thanked 307 Times in 267 Posts
    Quote Originally Posted by kzkz View Post
    I am in need of a bat file to run on each of our work PC's to run from the Task Scheduler at night to delete all of the Windows temp files and folders at: C:\Users\[username]\AppData\Local\Temp. Is that a recommended practive to do on a regular basis?

    How is C:\Windows\Temp used differently than the Local\temp folder?
    Is it OK to delete all of the folders in both directories?
    How would a delete function deal with files that are open or in use? It needs to ignore those.

    Would this work? Is there a way to confirm that it's in the correct path? Sure wouldn't want it to do a delete all in the wrong folder by accident.

    CD C:\Users\[username]\AppData\Local\Temp
    del %TEMP%\*.* /f /s /q

    Thanks!
    KZ
    Have encountered similar problem in business situations in the past.

    Running a batch file will probably not help in your situation; such a batch file would need to be coded very intelligently to avoid deleting files needed at next system bootup, and the necessary parameters are likely to change over time.

    Better to install a program like Cleanup! on each PC and make it company policy that each user should run Cleanup! before shutting-down each day or at startup next day. You will soon detect which users are not following policy.
    Computer Consultant/Technician since 1998 (first PC was Atari 1040STE in 1988).
    Most common computing error is EBKAC: Error Between Keyboard And Chairback
    AMD FX8120 (8-core @ 3.1GHz) CPU, Gigabyte GA-990FXA-D3 motherboard, 8GB (2x4GB) DDR3 1866MHz RAM, ATI-AMD Radeon HD6770 PCI-E VGA, 480GB Kingston SSD, 2TB Seagate SATA3.0 HDD, ASUS DVD/RW.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •