Home / Microsoft / Microsoft DOS / Remove the content of Temp Directory!???
0 Members and 2 Guests are viewing this topic. « previous next »
Pages: 1 2 [All] - (Bottom) Print
Author Topic: Remove the content of Temp Directory!???  (Read 619 times)
Floppyman
Topic Starter
Rookie



Thanked: 2
Posts: 30


« on: November 15, 2011, 07:11:54 AM »

Hello folks, i want a bat file to remove every thing from the %temp% directory and my code is
Code: [Select]
del /q "%userprofile%\Local Settings\Temp\*.* but this bat file only removes the files and not the folders as ccleaner does.
So i am asking if anyone knows of a better bat files that could remove the folder as well.

Thank you, hoping for a suitable solution:
IP logged
Raven19528
Hopeful



Thanked: 29
Posts: 284

Computer: Specs
Experience: Experienced
OS: Windows 7



« Reply #1 on: November 15, 2011, 10:11:58 AM »

Try using this:

Code: [Select]
rd /s /q "%userprofile%\Local Settings\Temp"

You may need to add this to ensure you don't have system issues.

Code: [Select]
md "%userprofile%\Local Settings\Temp"

BTW, this isn't working on my system because there are processes still running that are connected to files within the Temp directory. You may need to taskkill some stuff before the whole thing will work.

Personally, I think they should just bring back deltree and we'll call it a day.  ;D
IP logged

"All things that are
Are with more spirit chased than enjoy'd" -Shakespeare
BC_Programmer
Mastermind


Thanked: 697
Posts: 15,876

Computer: Specs
Experience: Beginner
OS: Windows 7


Pinkie Pie is best pony

BC-Programming.com 1 1
« Reply #2 on: November 15, 2011, 10:37:33 AM »

Code: [Select]
del /q /s "%userprofile%\Local Settings\Temp\*.*

There is no reason to delete the temp folder itself. And using that path specification is specific to Windows XP... why not use:

Code: [Select]
del /q /s "%temp%\*.*"

IP logged

My Blog

BASeBlock 2.3.0 (NOW WITH MACGUFFINS!)
Linux711
Adviser



Thanked: 49
Posts: 921

Certifications: List
Computer: Specs
Experience: Experienced
OS: Windows XP

Programming Blog 1
« Reply #3 on: November 15, 2011, 10:49:03 AM »

This is more of a suggestion than an answer to your question, but anyway. It's so much nicer to just make a RAM disk and then re-route all your temp files to the RAM disk so whenever you reboot, then temp is automatically lost because the RAM is cleared. If you're going to delete the temp anyway, then why even store it on the hdd? It's just going to get fragged faster and bloated with crap. I think this should be built into future versions of Windows. @BC: I know you're going to say why I am wrong. Go ahead.

Anyway, if you're interested, I can provide you with more instructions on how to do this.
IP logged

YouTube

"Genius is persistence, not brain power." - Me

"Insomnia is just a byproduct of, "It can't be done"" - LaVolpe

BC_Programmer
Mastermind


Thanked: 697
Posts: 15,876

Computer: Specs
Experience: Beginner
OS: Windows 7


Pinkie Pie is best pony

BC-Programming.com 1 1
« Reply #4 on: November 15, 2011, 10:55:18 AM »

@BC: I know you're going to say why I am wrong. Go ahead.

Main problem would be rerouting the temp files to the RAM disk, since %TEMP% is a per-user variable. And of course the issue of using up your RAM for the otherwise menial task of storing temporary files. Aside from that, it's not a terrible idea on XP and earlier if the system has gobs of RAM but low disk space.
IP logged

My Blog

BASeBlock 2.3.0 (NOW WITH MACGUFFINS!)
Linux711
Adviser



Thanked: 49
Posts: 921

Certifications: List
Computer: Specs
Experience: Experienced
OS: Windows XP

Programming Blog 1
« Reply #5 on: November 15, 2011, 11:03:16 AM »

Quote
Main problem would be rerouting the temp files to the RAM disk, since %TEMP% is a per-user variable.

True. That's why I have created a batch that sets temp variables easily, so that it can be done for each user.

Code: [Select]
@echo off
SET /P DIRNAME=Type the path to the RAMDrive:
REG ADD "HKCU\Environment" /v TEMP /t REG_EXPAND_SZ /d %DIRNAME% /f
REG ADD "HKCU\Environment" /v TMP /t REG_EXPAND_SZ /d %DIRNAME% /f
REG ADD "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v TEMP /t REG_EXPAND_SZ /d %DIRNAME% /f
REG ADD "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v TMP /t REG_EXPAND_SZ /d %DIRNAME% /f
IP logged

YouTube

"Genius is persistence, not brain power." - Me

"Insomnia is just a byproduct of, "It can't be done"" - LaVolpe

Floppyman
Topic Starter
Rookie



Thanked: 2
Posts: 30


« Reply #6 on: November 16, 2011, 02:30:57 AM »

Try using this:

Code: [Select]
rd /s /q "%userprofile%\Local Settings\Temp"
BTW, this isn't working on my system because there are processes still running that are connected to files within the Temp directory. You may need to taskkill some stuff before the whole thing will work.

aannnnn Raven19528 what do you know
Code: [Select]
rd /s /q "%userprofile%\Local Settings\Temp"
your code works and it removes the folder as well as the files. Good going, by the way rd is the remove directory isn't it:then how come it removes the file as well . Sorry BC Programmer your code i.e.,
Code: [Select]
del /q /s "%userprofile%\Local Settings\Temp\*.*  doesn't work it only removes the files same like my code except giving the /s switch. Same was for the other codes.

Anyways isn't it great to know what a bat file can achieve to do the same task.

PS: Just thanked Raven for this job.
IP logged
BC_Programmer
Mastermind


Thanked: 697
Posts: 15,876

Computer: Specs
Experience: Beginner
OS: Windows 7


Pinkie Pie is best pony

BC-Programming.com 1 1
« Reply #7 on: November 16, 2011, 03:51:19 AM »

is the 4KB used for the folder's directory entry really that critical?
IP logged

My Blog

BASeBlock 2.3.0 (NOW WITH MACGUFFINS!)
Raven19528
Hopeful



Thanked: 29
Posts: 284

Computer: Specs
Experience: Experienced
OS: Windows 7



« Reply #8 on: November 16, 2011, 09:27:19 AM »

by the way rd is the remove directory isn't it:then how come it removes the file as well .

Using the /s switch on the rd (which yes, is remove directory, same as rmdir) causes it to remove the folder and all files and folders within the directory. You can view the full explanation by typing rd /? at the cmd prompt.
IP logged

"All things that are
Are with more spirit chased than enjoy'd" -Shakespeare
Floppyman
Topic Starter
Rookie



Thanked: 2
Posts: 30


« Reply #9 on: November 25, 2011, 10:07:04 PM »

Hello hello newsflash
Code: [Select]
rd /s /q "%userprofile%\Local Settings\Temp" this code of Raven has stopped working , last time i had to perform a System Recovery and what do you know the bat file doesn't work to well.
 It removes the TEMP folder entirely, has  anyone even been trying this out. Its really important for me.

BC programmers does perform a little better
Code: [Select]
del /q /s "%userprofile%\Local Settings\Temp\*.*
Any suggestion i have to be better aware next time when i thank someone.
Little help here.
IP logged
Salmon Trout
Sage



Thanked: 546
Posts: 7,948

Computer: Specs
Experience: Beginner
OS: Unknown

1
« Reply #10 on: November 26, 2011, 12:35:45 AM »

Any suggestion i have to be better aware next time when i thank someone.
Little help here.

What I do is perform rd /s . (can you see the dot?) from within each directory I want to clear. Being logged in to a directory via the CD command means it won't get deleted. I tend to always add the /d switch to the cd command but I guess that's a personal preference.

To turn off the "., Are you sure (Y/N)?" prompt, use the /q switch and if you don't want to see the messages about what it couldn't delete, add 2>nul to redirect stderr to the null device.

For example: (the paths are mine, yours may differ)

cd /d %temp% & rd /s . /q 2>nul
cd /d %windir%\temp & rd /s . /q 2>nul


And Floppyman, maybe a bit less of the smart mouth? People have given their time freely to help you, and have done their best. Crude ungrateful sarcasm spoils the forum and looks bad. Anyhow, that's just my opinion.
« Last Edit: November 26, 2011, 12:58:40 AM by Salmon Trout » IP logged


Proud to be European
Salmon Trout
Sage



Thanked: 546
Posts: 7,948

Computer: Specs
Experience: Beginner
OS: Unknown

1
« Reply #11 on: November 26, 2011, 12:49:54 AM »

Little help here.

No need for this either. Helping you is not obligatory, and the goodwill that motivates people to do so is a fragile thing that is easily dispersed, as I hinted above.

IP logged


Proud to be European
kimsland
Intermediate



Thanked: 10
Posts: 127

Experience: Guru
OS: Windows XP
Leaving, CH can obviously do without my support.

« Reply #12 on: November 27, 2011, 07:59:38 AM »

I use to have this on a CD
Of course my 'CleanIt' folder also contained these files:
AllDone.exe (this is a 'dropcount' file)
CHOICE.com
sizeOLD.exe (this is a 'dropcount' file)
sizeNEW.exe (this is a 'dropcount' file)
taskkill.exe
TempClose.exe (An old AutoIt file)
wscript.exe

Anyway, if you want me to zip it up and attach all this stuff let me know
Actually not even sure if I'm allowed to do that?

It was originally made by me for Windows XP terminals that needed their Temp folders cleaned out periodically.
I also put an invisible script on it http://forums.techguy.org/dos-other/644932-solved-howto-run-batch-file.html

But these days, CCleaner has command prompt options, so its of no use anymore :(

Quote
@echo off
TITLE ^>^>Temp Cleaner^<^<
start %cdrom%\CleanIt\sizeold %temp%
start /D%temp% /max .
rem start /D%temp% .
call %cdrom%\CleanIt\CHOICE /T:N,3
call %cdrom%\CleanIt\taskkill /im sizeold.exe
attrib -r -s %temp%\*.* /s
del %temp% /f /s /q
rmdir %temp% /s /q
del "%userprofile%\Recent" /q
start %cdrom%\CleanIt\sizenew %temp%
call %cdrom%\CleanIt\CHOICE /T:N,3
call %cdrom%\CleanIt\taskkill /im sizenew.exe
call %cdrom%\CleanIt\CHOICE /T:N,1
start %cdrom%\CleanIt\tempclose.exe
start %cdrom%\CleanIt\AllDone %temp%
call %cdrom%\CleanIt\CHOICE /T:N,3
call %cdrom%\CleanIt\taskkill /im AllDone.exe
exit
IP logged
BC_Programmer
Mastermind


Thanked: 697
Posts: 15,876

Computer: Specs
Experience: Beginner
OS: Windows 7


Pinkie Pie is best pony

BC-Programming.com 1 1
« Reply #13 on: November 27, 2011, 10:31:26 AM »

If there is one thing I've yet to understand, it's people's obsession with files in the temp directory, and treating their deletion like some kind of ritual involving relatively complex scripts.
IP logged

My Blog

BASeBlock 2.3.0 (NOW WITH MACGUFFINS!)
patio
Moderator
Genius



Thanked: 1069
Posts: 11,351

Experience: Beginner
OS: Windows 7


Maud' Dib

« Reply #14 on: November 27, 2011, 11:18:19 AM »

Could not agree more...course there are 100's of other relatively simple tasks we see people attempting to write complex scripts for as well.
IP logged

   
"
All generalizations are false, including this one.  "
TheShadow
Intermediate



Thanked: 9
Posts: 220

Certifications: List
Computer: Specs
Experience: Guru
OS: Windows XP


Retiree in Florida

The Doctor 1
« Reply #15 on: November 28, 2011, 04:03:32 PM »

I saw it mentioned something to the effect that "Deltree.exe" is no more. 
Well, at least within the Windows XP community, that is far from correct.
Deltree.exe can be copied to XP from any earlier OS, like '98 or ME.
I even have it on my own web site, for an easy download.

But with the new functions built into the RD command, that's no longer necessary.
However, I too like Deltree and continue to use it in my own Cleanup batch files.

As for US old timers who just don't like JUNK forever building up on our hard drives, and sucking the life out of our PC's,
a cleanup batch file in the Startup folder is like FREE Maid Service for our PC's.

I've been using that means for keeping my PC nice and clean, Daily, for many years now.  I share that technique with my customers.  It really helps those who will never do their own maintenance.
The actual syntax has changed somewhat, since the windows 98 days, but the idea is the same.
Then with Vista, Win-7 and Win-8, where the Deltree command no longer works, I've modified my cleanup batch files to start with the DEL command.....like this:

del /F /S /Q "C:\temp\*.*"
del /F /S /Q "%systemroot%\temp\*.*"

That does essentially the same thing as using Deltree.

It's pointless to delete the actual 'temp' folder.  Windows just has to remake it on the next boot.  So don't just make extra work for windows when it's trying to boot up..... leave the 'temp' folder alone.  (Take out the junk, but leave the folder)

If you go through your own hard drive and find every folder where windows or some other program stores junk files, and then add a line containing that folder to a batch file, your cleanup batch file will probably be as long or longer than mine.
Mine is currently about forty lines long.
Oh, I'm also deleting the contents of my AVG virus vault and my Recycle Bin.

I hate junk and I try to get rid of as much of it as is humanly possible.   With the HD clean, lean and mean, scans and backups go just so much faster.

A batch file that you write yourself, can get the job done and be much safer than those 'do everything' cleanup programs like CCleaner.   

Cheers Mates!
 8)
IP logged

Experience is truly the best teacher.
Backup! Backup! Backup!  Ghost Rocks!
BC_Programmer
Mastermind


Thanked: 697
Posts: 15,876

Computer: Specs
Experience: Beginner
OS: Windows 7


Pinkie Pie is best pony

BC-Programming.com 1 1
« Reply #16 on: November 28, 2011, 04:44:47 PM »

Quote
Windows just has to remake it on the next boot.
Windows Does not create the directory if it doesn't exist at any point. If the path pointed to by %temp% (or %tmp%) doesn't exist Windows itself will store temporary files in the root of the system drive. How an application reacts depends on the application. Usually the application will encounter errors when it tries to create a file, unless it is written to handle the case where the path returned by GetTempPath() doesn't exist. Basically, deleting the temp folder itself isn't merely pointless but more apt to cause problems.



A batch file that you write yourself, can get the job done and be much safer than those 'do everything' cleanup programs like CCleaner.   

Even if the batch file is only a subset of the functionality.

Files stored on a hard drive do not slow it down. I suppose it would slow down backups, but if your backup process involves backing up temporary files... well I don't know what to say to that.

Quote
As for US old timers who just don't like JUNK forever building up on our hard drives, and sucking the life out of our PC's,
a cleanup batch file in the Startup folder is like FREE Maid Service for our PC's.
It's not so much being an old timer (from the past) as it is being 'stuck' in said past, or rather, stuck in the way of doing things. I used DOS 3.11, DOS 6, PC-DOS, and so forth myself, and wrote similar batch files to clear out the temporary files from Windows 3.1 (.WOA files being left all over the place from standard mode windows crashed, for example). But I stopped after Windows 9x, because there were far too many locations that temporary files were stored, the files stored within could often be in-use at the time the deletion was attempted, and unless I was running low on disk space it didn't really matter, and if I was running low I would run disk cleanup which would clear out those folders anyway. Not to mention, that temporary files generally only remain when the application that created them crashed or didn't delete them for some reason. Of course the occasional cleanup doesn't hurt, but to do so meticulously on a daily basis probably wastes more time than it saves in the long run. One would be better to address why their applications are crashing often enough that their temporary files are in a quantity worth deleting.

The main reason I don't like the idea of Batch files deleting temporary files is because people are often under the mistaken impression of two things:

1. that applications that use a temporary file will keep that temporary file open during the time that it is "used"
and
2. that applications that keep those files open open it with the specific share flags that will prevent a batch script from deleting it.

I don't know about anybody else, but I'd rather not trust the safety and stability of my system to such assumptions. The first is outright wrong; Many applications don't keep the file open; Photoshop, for example, has it's own swapping mechanism whereby it stores open documents that haven't been accessed and undo stacks as temporary files. However, it doesn't keep them open; instead it keeps track of the files  themselves and opens them and makes modifications as needed. If one was to delete the temporary folder contents while Photoshop was running, there is a likelihood that you could lose a lot of data. The second is an assumption. Many applications will create temporary files with FILE_FLAG_DELETE_ON_CLOSE so they will be deleted when they are closed, and they often open it with the FILE_SHARE_DELETE deletion flag, depending on the purpose of the temporary file. This means that other applications- or batch files- can delete the file while it is open by the application, which would also result in data loss.

of course, if the batch file runs at boot time, this won't be an issue, as long as one doesn't run any applications while the batch file is running. But, if it's run on a schedule, than there is no telling what can be lost. Personally, I'd rather use Disk Cleanup since it is a pre-emptive, multi-tasking Application that is designed to work co-operatively and is far less likely to delete files that it is able to detect are in use, rather than relying on specific circumstances of both the application opening the temporary file as well as the method by which it accesses said file.
IP logged

My Blog

BASeBlock 2.3.0 (NOW WITH MACGUFFINS!)
Raven19528
Hopeful



Thanked: 29
Posts: 284

Computer: Specs
Experience: Experienced
OS: Windows 7



« Reply #17 on: November 29, 2011, 11:17:51 AM »

Hello hello newsflash
Code: [Select]
rd /s /q "%userprofile%\Local Settings\Temp" this code of Raven has stopped working , last time i had to perform a System Recovery and what do you know the bat file doesn't work to well.
 It removes the TEMP folder entirely, has  anyone even been trying this out. Its really important for me.

This is the reason the original post also mentioned putting the md "%userprofile%\Local Settings\Temp" right after the rd command. Try this:

Code: [Select]
rd /s /q "%userprofile%\Local Settings\Temp"
md "%userprofile%\Local Settings\Temp"

Any suggestion i have to be better aware next time when i thank someone.
Little help here.

Not necessary or welcome. To borrow a line from a fellow board member, "Your membership fees determine the level of service you are guaranteed."
IP logged

"All things that are
Are with more spirit chased than enjoy'd" -Shakespeare
Pages: 1 2 [All] - (Top) Print 
Home / Microsoft / Microsoft DOS / Remove the content of Temp Directory!??? « previous next »
 


Login with username, password and session length

Old Forum Search | Forum Rules
Copyright © 2010 Computer Hope ® All rights reserved.
Powered by SMF 2.0 RC3 | SMF © 2006–2010, Simple Machines LLC
Page created in 0.15 seconds with 19 queries.