# Solved: How to DEL files B4 a backup



## Dingus (Apr 21, 2002)

As my heading say. I want to delete some files before I run my backup program.
My backup program can run a batchfile first then run the backup.

I've been trying to write a batch file but it fails.

I tried :-

del/q C:\Documents and Settings\Me\Local Settings\Temporary Internet Files\*.*
del/q C:\Documents and Settings\Me\Local Settings\Temp\*.*

Can anyone help.


----------



## TheOutcaste (Aug 8, 2007)

Any path with a space must be put in quotes.
You can also use the userprofile variable:


```
del/q "%userprofile%\Local Settings\Temporary Internet Files\*.*"
del/q "%userprofile%\Local Settings\Temp\*.*"
```
You may want to add the /F switch, just in case any of the files might have the Read Only attribute set
You might also check the options in the backup program. You may be able to just exclude those folders.

HTH

Jerry


----------



## Dingus (Apr 21, 2002)

Thanks Outcast. I tried your suggestion using the quotes .

del/q "C:\Documents and Settings\Me\Local Settings\Temporary Internet Files\*.*"
But no luck. I also tried this but no luck either.

del/q "%userprofile%\Local Settings\Temporary Internet Files\*.*"


----------



## TheOutcaste (Aug 8, 2007)

That's what I get for being in a rush.
The actual path to the Temporary Internet Files is this:
"C:\Documents and Settings\Me\Local Settings\Temporary Internet Files\Content.IE5"
Inside this folder will be 4 randomly named folders, an index.dat file, and a hidden desktop.ini file.

Neither Windows Explorer nor the Command Prompt will normally show this folder.
In Windows Explorer you have to type the Content.IE5 part into the address bar, then the contents will be displayed.
From the command Prompt you need to specify the */AD* switch to force the *Dir* command to show the folder, and the same for the 4 folders it contains, so a *Dir /AD* will list them.

So to delete those files, you need to use the following:


```
Set _TIF=C:\Documents and Settings\Administrator\Local Settings\Temporary Internet Files\Content.IE5
PushD %_TIF%
For /F "tokens=*" %%I In ('Dir /B /aD') Do RD /Q /S "%%I%
attrib -H -S desktop.ini
Del desktop.ini
Del index.dat
[COLOR=Blue]cd %userprofile%\Cookies
Del /q *.*[/COLOR]
PopD
```
Add the part in blue to also delete cookies.
Unless you've just rebooted (or logged on), you'll most likely get the following error trying to delete the index.dat file (same for the index.dat file in the Cookies folder):

*The process cannot access the file because it is being used by another process.
*
If you want to delete the index.dat files, simply log off then log on before running the script, and do not run Internet Explorer, Windows Explorer, or open My Computer, as doing so locks that file.

If you have multiple user accounts and want to clear all of them, it would be simple to make a subroutine of the part of the script that deletes files and then just add a *Set _TIF=* statement for each profile and call the subroutine.

You can use a similar method to clear history as well.

HTH

Jerry


----------



## Dingus (Apr 21, 2002)

Phew. Thanks TheOutcast, I'll give it a try and repot back.


----------



## Dingus (Apr 21, 2002)

Thanks Jerry, worked great for content.ie5 but not for
C:\Documents and Settings\Me\Local Settings\Temp
As you say I do have multiple users, but you lost me with :-



> then just add a Set _TIF= statement for each profile and call the subroutine.


Your script knowledge left me waaaayyyyyy behind.

Can I use %userprofile% for all my users?


----------



## TheOutcaste (Aug 8, 2007)

Dingus said:


> Thanks Jerry, worked great for content.ie5 but not for
> C:\Documents and Settings\Me\Local Settings\Temp


So *del/q "%userprofile%\Local Settings\Temp\*.*"* did not work?
You never mentioned the temp folder after post#2 so I thought that had worked. You will get errors for any files that are in use, and Del won't remove folders (it will delete files from within folders if the /S switch is used). I'll have to add that to the script so it'll work like the Temp Internet files.


Dingus said:


> Can I use %userprofile% for all my users?


%userprofile% returns the path for the currently logged on user, so you can use that, but you would have to log into each account to run the file.



Dingus said:


> As you say I do have multiple users, but you lost me with :-
> 
> Your script knowledge left me waaaayyyyyy behind.


Hope it wasn't too dusty back there
These two files are even more complicated, but I'll try not to stir up too much dust.
Two files, the first is run when logged onto a user account to clear just that account, the second can be run from any Computer Administrator account to clear all user accounts.

*#1*
This file must be run in each account. I've added a bunch of checking to make sure it switches to the correct folder. See notes below.


```
@Echo Off
Set _UserLS=%userprofile%\Local Settings
Set _TIF=Temporary Internet Files\Content.IE5
PushD "%_UserLS%"
IF /I "%CD%"=="%userprofile%\Local Settings" Goto OK0
Echo No Local Settings folder for %userprofile% found. Please check Username spelling
Pause
PopD
GOTO :EOF
:OK0
CD Temp
IF /I "%CD%"=="%userprofile%\Local Settings\Temp" Goto OK1
Echo No Temp folder found under %userprofile%\Local Settings. Nothing Deleted
Pause
PopD
GOTO :OK2
:OK1
[COLOR=Blue]REM For /F "tokens=*" %%I In ('Dir /B /AD') Do @Echo %%I[/COLOR]
For /F "tokens=*" %%I In ('Dir /B /AD') Do RD /Q /S "%%I"
DEL /q *.*
:OK2
CD "%_UserLS%\%_TIF%"
IF /I "%CD%"=="%_UserLS%\%_TIF%" Goto OK3
Echo No %_TIF% folder found under %_UserLS%. Nothing Deleted
Pause
GOTO :Cookies
:OK3
[COLOR=Blue]REM For /F "tokens=*" %%I In ('Dir /B /AD') Do @Echo %%I[/COLOR]
For /F "tokens=*" %%I In ('Dir /B /AD') Do RD /Q /S "%%I"
attrib -H -S desktop.ini
Del desktop.ini
Del index.dat
:Cookies
CD "%userprofile%\Cookies"
IF /I "%CD%"=="%userprofile%\Cookies" Goto OK3
Echo No Cookies folder found under %userprofile%. Nothing Deleted
Pause
PopD
GOTO :EOF
:OK3
Del /q *.*
PopD
```
*#2*

This file can be run from any Admin account. You'll have to edit the lines shown in red substituting the actual user names on your system. Add or delete lines as needed but be sure everything up to the space after Echo is identical.

*I've added all the error checking because without it, if you mistype a user name, or the Local Settings folder doesn't exist in the profile, this script will delete everything in the folder it is run from. I made that mistake running it from the root of the C: drive. Good thing it was a Virtual Machine

Might be a good idea to remark out the first Echo Off, all the Del lines, and the 2 For loops under the 2 blue lines until you are sure it's switching to the correct folder for each user. When run from a command prompt, the prompt will change to the folder it switched to as the script runs.

The blue lines can be unremarked and will list the folders that will be deleted. You can also remove the /Q switch from the RD and Del lines, this will cause it to prompt you before it deletes anything*.


```
@Echo Off
Set _Profiles=c:\Documents and Settings
Set _TIF=Temporary Internet Files\Content.IE5
If Exist %~dp0user.tmp Del %~dp0user.tmp
[COLOR=Red]>>%~dp0user.tmp Echo Administrator
>>%~dp0user.tmp Echo Me
>>%~dp0user.tmp Echo Dick
>>%~dp0user.tmp Echo Jane[/COLOR]
For /F "Tokens=*" %%I In ('Type %~dp0user.tmp') Do (
Echo Clearing Folders for %%I
Call :ClearFiles %_Profiles%\%%I)
Del %~dp0user.tmp
Goto :EOF
::===================================::
::   -   S u b r o u t i n e s   -   ::
::===================================::
:ClearFiles
PushD "%*\Local Settings"
IF /I "%CD%"=="%*\Local Settings" Goto OK0
Echo No Local Settings folder for %* found. Please check Username spelling
Pause
PopD
GOTO :EOF
:OK0
CD Temp
IF /I "%CD%"=="%*\Local Settings\Temp" Goto OK1
Echo No Temp folder found under %*\Local Settings. Nothing Deleted
Pause
PopD
GOTO :OK2
:OK1
[COLOR=Blue]REM For /F "tokens=*" %%I In ('Dir /B /AD') Do @Echo %%I[/COLOR]
For /F "tokens=*" %%I In ('Dir /B /AD') Do RD /Q /S "%%I"
DEL /q *.*
:OK2
CD "%*\Local Settings\%_TIF%"
IF /I "%CD%"=="%*\Local Settings\%_TIF%" Goto OK3
Echo No %_TIF% folder found under %*\Local Settings. Nothing Deleted
Pause
GOTO :Cookies
:OK3
[COLOR=Blue]REM For /F "tokens=*" %%I In ('Dir /B /AD') Do @Echo %%I[/COLOR]
For /F "tokens=*" %%I In ('Dir /B /AD') Do RD /Q /S "%%I"
attrib -H -S desktop.ini
Del desktop.ini
Del index.dat
:Cookies
CD "%*\Cookies"
IF /I "%CD%"=="%*\Cookies" Goto OK3
Echo No Cookies folder found under %*\. Nothing Deleted
Pause
PopD
GOTO :EOF
:OK3
Del /q *.*
PopD
```
HTH

Jerry


----------



## Dingus (Apr 21, 2002)

Hi Outcaste. Some feedback for you 
I've tried the second as I want to del all of the rubbish for ALL users before I run the backups.
Unfortunately the batch file keeps failling. Do the del locations need to be unhidden in tools/folder options?

As you said I need the files deleted and not the directories.
The paths I need are

C:\Documents and Settings\Linda\Local Settings\Temporary Internet Files\Content.IE5
C:\Documents and Settings\Linda\Local Settings\Temporary Internet Files
C:\Documents and Settings\Linda\Local Settings\Temp

Linda in this case but there are more users. 

I should also say that this will run unattended so no input from me if a yes or no is needed.


----------



## TheOutcaste (Aug 8, 2007)

Dingus said:


> Unfortunately the batch file keeps failling.


How is it failing? What is the error message?
You will most likely not be able to delete the index.dat file in either the *C:\Documents and Settings\username\Local Settings\Temporary Internet Files\Content.IE5* folder, or *C:\Documents and Settings\username\Cookies folder *for the account(s) that is(are) logged in when the batch file runs. So if you switch users and log into each user, then run the batch file, you won't be able to delete the index.dat files for any of them, making it seem as if nothing was deleted.
Cookies will be deleted, but some cookies are also in the *index.dat* file, so they may also appear not to have been deleted for the account you are logged in under when you run the batch file.



Dingus said:


> Do the del locations need to be unhidden in tools/folder options?


No, this won't make a difference


Dingus said:


> C:\Documents and Settings\Linda\Local Settings\Temporary Internet Files


There are actually no files in this folder, other than desktop.ini. The files you see when you view this folder in My Computer/Windows Explorer are actually in the 4 hidden folders under *C:\Documents and Settings\username\Local Settings\Temporary Internet Files\Content.IE5* and in the *index.dat* file in the *Content.IE5* folder.
You can delete the desktop.ini file from this folder and then you will no longer see the files that are in the index.dat file or the folders under Content.IE5; meaning the folder will appear empty even though the hidden folders are not, so I would recommend to leave this one.

Your best bet is to simply exclude these folders in the backup program, then they won't be backed up.

Jerry


----------



## Dingus (Apr 21, 2002)

> Your best bet is to simply exclude these folders in the backup program, then they won't be backed up.


What an idiot, why didn't I think of that.

Outcast, sorry to have wasted your time but many thanks anyway. I'll do just that.


----------



## TheOutcaste (Aug 8, 2007)

Guess we make a pair, since I didn't think of it until I'd just about finished that post

Go ahead and Mark this Solved.

Jerry


----------



## Dingus (Apr 21, 2002)

One simple question. Where can I get info on the script stuff you know? I'd like to learn it.


----------



## TheOutcaste (Aug 8, 2007)

Here's a few links. The built in help is a good resource, especially the help for *For*, *IF*, *Set*, and *Call*
Type *Help* in a Command Prompt for a partial list of commands. *command* /? will bring up help for that command.

DOS and VB Scripting Links
Command-line reference A-Z
Using batch parameters
Windows 95/98/ME Batch file Tutorial (Still a good basic reference for WinNT/2K/XP)
Batch File Functions for NT4/2000/XP/2003
Rob van der Woude's Scripting Pages

Browsing this forum and studying the solutions, and trying to find _different_ solutions can be a good learning tool as well.

Jerry


----------

