Link to home
Start Free TrialLog in
Avatar of fraunkd
fraunkd

asked on

Script - Remove Temp Files For ALL Profiles

I have a terminal server with cookies, temp, and temporary internet files that I would like to remove for ALL profiles within documents and settings.

I’d like to create a script that will run each day to remove the files.

The bat file I have created only removes the temp files from the profile of the user that’s currently logged on – and skipping the rest of the profiles within documents and settings as desired.

Below is my script.  Please let me know the best way to write this to where all profiles are cleaned rather than only the profile of the logged on user.  Thanks.

del "C:\documents and settings\%username%\cookies\*.*" /Q
del "C:\documents and settings\%username%\Local Settings\temp\*.*" /Q
del "C:\documents and settings\%username%\Local Settings\temporary internet files\*.*" /Q
ASKER CERTIFIED SOLUTION
Avatar of GaGirrl
GaGirrl

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bill Prew
Bill Prew

Give this BAT file a try.  It will loop over all folders in your docs & settings folder.  Since there may be some folders in there you don't want to purge I added the capability for a skip list.

Notice the DEL lines have ECHO in front of them right now.  This will let you test this out without deleting anything, it will just echo the DEL command that would have been executed to the scree so you can check it out.  Once you get it perfect, remove the ECHOs in front of the DEL commands.

~bp
@echo off
setlocal EnableDelayedExpansion
 
REM Set path to base users directory
set "BaseDir=C:\documents and settings"
 
REM Set list of any "user" folders not to purge
set SkipDirs="All Users","Default User","NetworkService","LocalService"
 
REM Loop through all user folders in base directory
for /D %%A in ("%BaseDir%\*") do (
  REM Check if in list to skip
  set SkipFlag=FALSE
  for %%B in (%SkipDirs%) do (
    if "%%nA" == "%%~B" set SkipFlag=TRUE
  )
 
  REM If not skipped, delete desired files
  if "!SkipFlag!" == "FALSE" (
    ECHO del "%BaseDir%\%%A\cookies\*.*" /Q
    ECHO del "%BaseDir%\%%A\Local Settings\temp\*.*" /Q
    ECHO del "%BaseDir%\%%A\Local Settings\temporary internet files\*.*" /Q
  )
)

Open in new window

Avatar of fraunkd

ASKER

billprew

When I remove ECHO, I receive the line below multiple times:

The filename, directory name, or volume label syntax is incorrect.
Avatar of fraunkd

ASKER

Below is what worked best:

https://www.experts-exchange.com/questions/20794666/Batch-file-to-delete-temporary-internet-files-and-others.html

cls
SET SRC1=%SYSTEMDRIVE%\Documents and Settings
SET SRC2=Local Settings\Temporary Internet Files\Content.IE5
SET SRC3=Local Settings\Temp
SET SRC4=Local Settings\History
SET SRC5=%SYSTEMROOT%\Temp
SET SRC6=Cookies


echo About to delete files from Internet Explorer "Temporary Internet files"
FOR /D %%X IN ("%SRC1%\*") DO FOR /D %%Y IN ("%%X\%SRC2%\*.*") DO RMDIR /S /Q "%%Y"
echo About to delete files from "Local settings\temp"
FOR /D %%X IN ("%SRC1%\*") DO FOR /D %%Y IN ("%%X\%SRC3%\*.*") DO RMDIR  /S /Q "%%Y"
FOR /D %%X IN ("%SRC1%\*") DO FOR  %%Y IN ("%%X\%SRC3%\*.*") DO DEL /F /S /Q "%%Y"
echo About to delete files from "Windows\Temp"
cd\
%systemdrive%
cd /d %SystemRoot%\temp
del /F /Q *.*
@echo Y|RD /S ""
@echo.
@echo.
echo About to delete files from "Local Settings\History"
FOR /D %%X IN ("%SRC1%\*") DO FOR  %%Y IN ("%%X\%SRC4%\*.*") DO DEL /F /S /Q "%%Y"

FOR /D %%X IN ("%SRC1%\*") DO FOR  %%Y IN ("%%X\%SRC4%\today*.*") DO DEL /F /S /Q "%%Y"

FOR /D %%X IN ("%SRC1%\*") DO FOR  %%Y IN ("%%X\%SRC4%\*.*") DO DEL /F /S /Q "%%Y"

echo About to delete files from "%SYSTEMROOT%\Temp"
FOR /D %%X IN ("%SRC1%\*") DO FOR  %%Y IN ("%%X\%SRC5%\*.*") DO DEL /F /S /Q "%%Y"

echo About to delete files from "Cookies"
FOR /D %%X IN ("%SRC1%\*") DO FOR  %%Y IN ("%%X\%SRC6%\*.*") DO DEL /F /S /Q "%%Y"

@echo            Please review any errors if they exist
@echo.
@echo.

Avatar of fraunkd

ASKER

This is what worked best:

cls
SET SRC1=%SYSTEMDRIVE%\Documents and Settings
SET SRC2=Local Settings\Temporary Internet Files\Content.IE5
SET SRC3=Local Settings\Temp
SET SRC4=Local Settings\History
SET SRC5=%SYSTEMROOT%\Temp
SET SRC6=Cookies


echo About to delete files from Internet Explorer "Temporary Internet files"
FOR /D %%X IN ("%SRC1%\*") DO FOR /D %%Y IN ("%%X\%SRC2%\*.*") DO RMDIR /S /Q "%%Y"
echo About to delete files from "Local settings\temp"
FOR /D %%X IN ("%SRC1%\*") DO FOR /D %%Y IN ("%%X\%SRC3%\*.*") DO RMDIR  /S /Q "%%Y"
FOR /D %%X IN ("%SRC1%\*") DO FOR  %%Y IN ("%%X\%SRC3%\*.*") DO DEL /F /S /Q "%%Y"
echo About to delete files from "Windows\Temp"
cd\
%systemdrive%
cd /d %SystemRoot%\temp
del /F /Q *.*
@echo Y|RD /S ""
@echo.
@echo.
echo About to delete files from "Local Settings\History"
FOR /D %%X IN ("%SRC1%\*") DO FOR  %%Y IN ("%%X\%SRC4%\*.*") DO DEL /F /S /Q "%%Y"

FOR /D %%X IN ("%SRC1%\*") DO FOR  %%Y IN ("%%X\%SRC4%\today*.*") DO DEL /F /S /Q "%%Y"

FOR /D %%X IN ("%SRC1%\*") DO FOR  %%Y IN ("%%X\%SRC4%\*.*") DO DEL /F /S /Q "%%Y"

echo About to delete files from "%SYSTEMROOT%\Temp"
FOR /D %%X IN ("%SRC1%\*") DO FOR  %%Y IN ("%%X\%SRC5%\*.*") DO DEL /F /S /Q "%%Y"

echo About to delete files from "Cookies"
FOR /D %%X IN ("%SRC1%\*") DO FOR  %%Y IN ("%%X\%SRC6%\*.*") DO DEL /F /S /Q "%%Y"

@echo            Please review any errors if they exist
@echo.
@echo.

==> When I remove ECHO, I receive the line below multiple times:

What did it look like before you removed the ECHO?

(although I guess it's moot at this point)

~bp
Can sombody show me how to make this for windows 7
regards Jan sejer