Go Back   TechArena Community > Technical Support > Computer Help > TroubleShoot 98
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , , ,

Sponsored Links



BAT file to delete all .tmp files in a certain folder

TroubleShoot 98


Reply
 
Thread Tools Search this Thread
  #1  
Old 02-06-2009
Larry
 
Posts: n/a
BAT file to delete all .tmp files in a certain folder

I think I asked about this a while ago but got hung up with it.

I have a certain folder, let's call it C:\Drafts, which periodically fills
up with lots of .tmp Word files which I delete by opening the folder, doing
a search for .tmp files, and deleting them. I want to automate this process
with a .bat file. I've never created a .bat file.

As a model for what I want to create, I have on my computer a .bat file,
called Cleanup Temp Files.bat, which deletes all the files in the temporary
folders down in Windows\System (it is vastly quicker and more effective than
Windows' own cleanup utility). Here it is:

Deltree /Y "%SYSTEMPARTITION%\Windows\Recent\"
Deltree /Y "%SYSTEMPARTITION%\Windows\Temp\"
Deltree /Y "%SYSTEMPARTITION%\Windows\Temporary Internet Files\"
Exit

What this does is delete the entire contents of each of these folders. I
don't want to do that. I just want to delete .tmp files in the C:\Drafts
folder. Can anyone show me how to do that?
Reply With Quote
  #2  
Old 02-06-2009
Don Phillipson
 
Posts: n/a
Re: BAT file to delete all .tmp files in a certain folder

Your CLIs do not delete only folder contents: DOS DELTREE
deletes the specified folder as well as its contents.
(If deleted, Windows System folders like c:\Windows\Temp are
created new at the next boot.
(It is usually unwise to delete \Temporary Internet Files. This is
why Windows has its own menu for this function, reached via
Control Panel or else Browser / Tools / Preferences.)

DOS commands are sellf-documenting, seen
via command and /?
i.e. from any DOS prompt
DEL /?
or DELTREE /?
produces a few lines explaining parameters.

A BATch file is merely an ASCII text file listing one
or several DOS commands. e.g.
DEL *.TMP
This deletes all files of type TMP in the logged directory (folder.)
Most libraries still have DOS manuals explaining DOS
utilities in more detail than MS provided in on-line help.
Reply With Quote
  #3  
Old 03-06-2009
Larry
 
Posts: n/a
Re: BAT file to delete all .tmp files in a certain folder

I can get a .bat file to delete files if I place it in the same folder as
the files I want to delete, like this:

DEL *.txt
Exit

But if I run this from another folder, like this:

DEL "C:\Document\Tests\*.txt"
Exit

it doesn't work.

Also, it doesn't work either way on .tmp files, which is what I want to
delete.

Also, how do I make the DOS window go away after it runs
Reply With Quote
  #4  
Old 03-06-2009
Larry
 
Posts: n/a
Re: BAT file to delete all .tmp files in a certain folder

One problem solved: If I run the .bat from a shortcut icon, the DOS window
goes away by itself. Have no idea why.

Reply With Quote
  #5  
Old 03-06-2009
Don Phillipson
 
Posts: n/a
Re: BAT file to delete all .tmp files in a certain folder

This is definitely an anomaly. Do you get no error message?
If the DOS window closes too fast, insert between these two
lines PAUSE on a line of its own, and you can read the error
message.

I would guess there is an input error here, e.g. default Win98 folder
name is My Documents not Documents. (Quotes are needed for
a folder name with a space, but not in your command line.)
Perhaps you entered \TESTS\ for \TEXTS. DOS is unforgiving,
i.e. one wrong keystroke disables or alters your command.

Command EXIT as in your sample batch file.
Reply With Quote
  #6  
Old 03-06-2009
Jeff Richards
 
Posts: n/a
Re: BAT file to delete all .tmp files in a certain folder

You should not need to use Exit. See here for details of closing the
window.
(Note the second line of the table)

What do you mean by "it doesn't work." Is there an error message? Do any
files get deleted?

I would guess that you have not named the folder correctly. If you type
that command (DEL "C:\Document\Tests\*.txt") at a DOS prompt from another
folder (eg, C:\Windows) does it delete those files, or is there an error
message.
Reply With Quote
  #7  
Old 03-06-2009
PCR
 
Posts: n/a
Re: BAT file to delete all .tmp files in a certain folder

If you R-Clk the shortcut, select Properties, & click the Program tab,
you will see that the "Close on Exit" box is checked. That is why it is
closing when done. The .bat, itself, would do the same under the same
conditions-- I've tested it! I wrote this .bat...

DEL C:\Windows\Temp\*.xxx

....& it DOES work just fine when clicked on the Desktop. It deleted two
..xxx files I had created in Temp. And it either closed when done or
didn't-- depending upon whether that "Close on Exit" box was checked. If
unchecked, a message was posted stating the bat was done & I should
click to close the window-- which may actually be the wisest approach
for a .bat that does a delete! Definitely leave it unchecked during
testing to read any error message that may result.

Answer Richards's questions-- what message do you get, especially? I'm
thinking possibly one/two of your *.tmp files may be "in use" by Word &
therefore cannot be deleted even in a DOS window.
Reply With Quote
  #8  
Old 03-06-2009
glee
 
Posts: n/a
Re: BAT file to delete all .tmp files in a certain folder

When I used a batch file in Win9x to delete temp files in the Temp folder, I simply
ended the batch with the line:
cls
with nothing further after it....no space, no return.

cls is the Clear Screen command, which apparently allows the command window to
close.
Reply With Quote
  #9  
Old 03-06-2009
Jeff Richards
 
Posts: n/a
Re: BAT file to delete all .tmp files in a certain folder

The simplest way to get the command window to close is to ensure the screen
is clear, and the simplest way to do that is to use the CLS command. There
are other options, but getting into the habit of putting a CLS at the end of
each batch file is the best way to go.

According to the table on the page I referenced, DOS 7 is the only version
that operates like this, which I wasn't aware of, but I guess I never
noticed that because having a CLS at the end of each batch file does no harm
with other versions of DOS which will close the screen without the need for
CLS.
Reply With Quote
  #10  
Old 03-06-2009
Jeff Richards
 
Posts: n/a
Re: BAT file to delete all .tmp files in a certain folder

EXIT will NOT close the command window in DOS 7 if the window still contains
text. It is essential to clear the window before the batch file finishes in
order to have the window close automatically. Please see the site I
referred the OP to for a description of command prompt window behaviour in
various versions of DOS.
Reply With Quote
  #11  
Old 03-06-2009
Larry
 
Posts: n/a
Re: BAT file to delete all .tmp files in a certain folder

I mean that the files do not get deleted.

I've been fiddling with it but still can't get it to work. I'll work on this
more tomorrow when I'm fresher.
Reply With Quote
  #12  
Old 04-06-2009
PCR
 
Posts: n/a
Re: BAT file to delete all .tmp files in a certain folder

That is normally true-- the foldere & all contents including subfolders
get delted. However, when a backslash is placed after the folder name,
the top folder itself is not deleted: ONLY the files & subfolders inside
get deleted...

C:\>DELTREE C:\WINDOWS\TEMP\folder\
Delete file "c:\WINDOWS\TEMP\folder\sp16704.exe"? [yn] y
Deleting c:\WINDOWS\TEMP\folder\sp16704.exe...
Delete file "c:\WINDOWS\TEMP\folder\sp16704.txt"? [yn] y
Deleting c:\WINDOWS\TEMP\folder\sp16704.txt...

C:\>DELTREE C:\WINDOWS\TEMP\folder
Delete directory "C:\WINDOWS\TEMP\folder" and all its subdirectories?
[yn] y
Deleting C:\WINDOWS\TEMP\folder...

If one cares to try it oneself-- be careful! DELTREE is a killer!
Reply With Quote
  #13  
Old 30-07-2009
Member
 
Join Date: Jul 2009
Posts: 1
Re: BAT file to delete all .tmp files in a certain folder

Personally, i use a PAUSE command, so I know the file ran and is complete. It may be an extra step, but it can pay off during creation of the batch file, maybe after a few command lines...
Reply With Quote
  #14  
Old 30-07-2009
PCR
 
Posts: n/a
Re: BAT file to delete all .tmp files in a certain folder

Alright. Very good, in case some day the .bat goes awry, you will see it
for sure. But still one must be careful as possible with Deltree.
Reply With Quote
Reply

  TechArena Community > Technical Support > Computer Help > TroubleShoot 98


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "BAT file to delete all .tmp files in a certain folder"
Thread Thread Starter Forum Replies Last Post
How to delete .exe file in Temp folder Baako Networking & Security 6 19-06-2011 09:55 AM
Error Deleting File or Folder. Cannot delete (FILE): Access denied FJK MediaCenter 9 23-02-2011 02:57 PM
How can I delete files in system32 folder? Renderman21 Operating Systems 3 23-07-2009 09:15 PM
DRM Error - Can't delete files in DRM folder Paul Media Player 0 16-01-2008 12:58 AM
failed/successfull audit delete folder and delete file and folder steef83 Windows Security 7 18-11-2006 01:23 AM


All times are GMT +5.5. The time now is 04:41 PM.