# DOS Batch programming



## effie (May 25, 2007)

I've got a server which I would like to run a simple clean on once in a while.
What I'd like to clean is all Trash and Temp folders throughout the system as well as some specific extensions: .dmp, .tmp, .temp, .old, .bak etc..

I've successfully created a batch that cleans yup the folders but im having some problems with the files using the same code. At the same time, I'm logging all removed files into a .log file.
Here's my code so far:

For /F "tokens=2-4 delims=/- " %%A in ('date/T') do set var=***** %%C/%%A/%%B *****
set file=Clean.log
ECHO %var% >> %file%
ECHO DIRECTORIES CLEANED >> %file%
FOR /R /D %%D in (temp, trash) DO (
IF EXIST %%D (
ECHO %%D >> %file%
del %%D /q))
ECHO Complete!

ECHO FILES CLEANED >> %file%
FOR /R %%D in ("*.bak," *.chk, *.dir, *.dmp, "*.temp", "*.tmp", *.$$$, *.$db) DO (
ECHO %%D >> %file%
del "%%D")
ECHO Complete!
--------
Now for the problem... The for loop also deletes all "tmpl" files.. any files that have the "tmp*" in them as an extension 
I tried to set up an If or variable that seperates the tmp from the tmpl but it was unsuccessful.. Im mostly just familiar with batch coding but have general code understanding..

Is there a way to solve this problem? There might be other ways to accomplish this besides from a batch file but they are not familiar me. 

Thanx in advance!


----------



## cristobal03 (Aug 5, 2005)

Hey there, welcome to TSG. Did you copy/paste that code directly from the file? I see a couple things that seem incorrect. Also, this thread might be better off in the Development forum, so you may want to ask a moderator to move it for you.

chris.


----------



## effie (May 25, 2007)

Hi, yes this is more or less a copy/paste from the file.. I see a couple of errors here myself, but they dont seem to have any affect on the output.. They are just versions of me experiencing 
I thought I posted this on the development forum, guess I did a missed 
If its not a big deal, I will post this one on the dev forum and this one can be deleted/closed.


----------



## effie (May 25, 2007)

I've got a server which I would like to run a simple clean on once in a while.
What I'd like to clean is all Trash and Temp folders throughout the system as well as some specific extensions: .dmp, .tmp, .temp, .old, .bak etc..

I've successfully created a batch that cleans yup the folders but im having some problems with the files using the same code. At the same time, I'm logging all removed files into a .log file.
Here's my code so far:

For /F "tokens=2-4 delims=/- " %%A in ('date/T') do set var=***** %%C/%%A/%%B *****
set file=Clean.log
ECHO %var% >> %file%
ECHO DIRECTORIES CLEANED >> %file%
FOR /R /D %%D in (temp, trash) DO (
IF EXIST %%D (
ECHO %%D >> %file%
del %%D /q))
ECHO Complete!

ECHO FILES CLEANED >> %file%
FOR /R %%D in (*.bak, *.chk, *.dir, *.dmp, *.temp, *.tmp, *.$$$, *.$db) DO (
ECHO %%D >> %file%
del "%%D")
ECHO Complete!
--------
Now for the problem... The for loop also deletes all "tmpl" files.. any files that have the "tmp*" in them as an extension.
I tried to set up an If or variable that seperates the tmp from the tmpl but it was unsuccessful.. Im mostly just familiar with batch coding but have general code understanding..

Is there a way to solve this problem? There might be other ways to accomplish this besides from a batch file but they are not familiar me.

Thanx in advance!


----------



## Mosaic1 (Aug 17, 2001)

This example is labor intense. It checks the current drive. I am on a non networked system here. It checks the file extension of the file against all the extensions you want to process and if a match, then logs to results.txt
You'll use your own file extensions. I used htm and html because they illustrate this problem very well.


```
@echo off
cd \
If exist results.txt del results.txt
FOR /R %%D in (*.bak, *.chk, *.htm) DO (
If %%~xD==.htm echo %%D 
If  %%~xD==.bak echo %%D 
If  %%~xD==.chk echo %%D 
)>> Results.txt

Start notepad results.txt
```
You can also look for other utilities to get just the file extension you want and process.


----------



## Mosaic1 (Aug 17, 2001)

You could also use dir instead. Ther are probably other ways to do this more easily too.


----------



## EAFiedler (Apr 25, 2000)

Hi *effie*

Welcome to Tech Support Guy Forums!

I have merged your posts to one thread. Please do not create duplicate threads.
Continue replies here.
Thank you


----------



## effie (May 25, 2007)

Tnx for an excellent reply mosaid! Much cleaner and better code than the one I put together myself  I was looking for that right "if" command everywhere but could not find one that worked.. Could you explain this "~x" for me?


----------



## gurutech (Apr 23, 2004)

If you don't need to know which directories contained files that you deleted, you can use the following line to delete files:

DEL *.BAK /S
DEL *.TMP /S
etc...


----------



## effie (May 25, 2007)

gurutech said:


> If you don't need to know which directories contained files that you deleted, you can use the following line to delete files:
> 
> DEL *.BAK /S
> DEL *.TMP /S
> etc...


Tnx for the suggestion, but this still causes *.tmpl files to be deleted..
The solution above seems to work for me :up:
Im still puzzled by the "~x" command tho...


----------



## Mosaic1 (Aug 17, 2001)

Hi,

You're welcome.

What we are doing here is getting the file extension of the current file the loop is handling, and then comparing it to what we are actually looking for.

Have a look at the for command more closely.

Run this command :
*for /? > for.txt && start notepad for.txt*

Now find this section of the for.txt file:



> In addition, substitution of FOR variable references has been enhanced.
> You can now use the following optional syntax:
> 
> %~I - expands %I removing any surrounding quotes (")
> ...


* %~xI - expands %I to a file extension only*

We use the %% because it's a batch.

So let's say you have a file named what.tmp1, and you are using %%a as the variable.

%%~xa

That gives you .tmp1

So then we compare and if .tmp1 = .tmp, or if .tmp1=.bak or if .tmp1=.chk 
then the action is taken. Otherwise, nothing is done. In this case, you'd have no action taken.

The loop does this for every file found and compares the file extension to what you really want.

So you see, if the file the loop is dealing with at the moment has the .tmp extension, then the action will be taken because .tmp = .tmp

Hope it helps.

Mo


----------

