Sign up ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I use this script to clean history, cookies and cache(Temporary Internet Files) for all users AND it should also clean the temp dir BUT there seems to be something wrong.

2 things get mixed up I think, the %temp% variable (= D:\TEMP in my environment) AND the users temp dir in the %userprofile%

:: Works on Win XP  -and-  on Win 7

@echo off

Set "RegKey=HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"
set "regkey2=HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\shell folders"

call:getspecialfolders "Cache, History, Cookies"

For /f "tokens=*" %%? in (
 'Reg.exe QUERY "%RegKey%" ^|findstr /ric:"\S-1-5-21-[0-9]*-[0-9]*-[0-9]*-[0-9]*$"'
 ) do (
 For /f "tokens=2,*" %%A in (
 'Reg.exe QUERY "%%?" /v ProfileImagePath ^|find /i "ProfileImagePath"'
 ) do call:Go %%B

start ""/w "%windir%\system32\RunDll32.exe" InetCpl.cpl,ClearMyTracksByProcess 255

:end ***

   call Set "Target=%*"
   If EXIST "%Target%" call:Clear "%Target%"
exit /b 0

REM echo.&echo.%~1\%$$Cache%
   pushD "%~1\%$$Cache%" &&(
   rmdir /S /Q .

REM echo.&echo.%~1\%$$History%\History.IE5
REM    pushD "%~1\%$$History%\History.IE5" &&(
REM    rmdir /S /Q .
REM    popD)2>C:\test1_History_IE.txt

REM echo.&echo.%~1\%$$History%
   pushD "%~1\%$$History%" &&(
   rmdir /S /Q .

REM echo.&echo.%~1\%$$Cookies%
   pushD "%~1\%$$Cookies%" &&(
   rmdir /S /Q .

   pushD "%~1\%$$temp%" &&(
   rmdir /S /Q .
exit /b 0

   Set "FoldersToClear=%~1"

   For %%* in (%FoldersToClear%) Do (
     For /f "tokens=2,*" %%A in (
     'reg.exe query "%regkey2%" /v %%* ^|find /i "%%~*"'
     ) do Call:sf1 "%%~B" "%%~*"
   Call:sf2 "%temp%" "temp" "%userprofile%"
exit /b 0

   Call set "sf=%~1"
   Call set "$$%~2=%%sf:%userprofile%\=%%"
exit /b 0

   Call set "sf=%~1"
   call Set "usr=%~dpns3"
   Call set "$$%~2=%%sf:%usr%\=%%"
exit /b 0

BUT somehow I can't get the last "temp part" to function so it cleans the %temp% (D:\Temp in my environment) and also to find all "temp dir's" in the %userprofile%.

ie. this for instance does work for %temp%:

PushD "%Temp%" && (
ATTRIB -S -H -R -A /D /S & (
For /f "Tokens=*" %%* in ('dir "%Temp%" /B') Do (
RD "%Temp%\%%*" /S /Q || Del /F /S /Q "%Temp%\%%*"))&PopD)2>c:\test0b_TEMP.txt

and this ie. works for the "user(s) temp":

::Set Search directory to "Documents and Settings" folder
(Set Target=%AllUsersProfile:~0,-10%)

title,Finding the Temp subfolders in %Target%&COLOR 9E

If EXIST "%Target%",(
  For /f "Tokens=*" %%* in ('dir "%Target%" /B') Do (
   cd/D "%target%\%%*\Local Settings\Temp" && (
   ATTRIB -S -H -R -A /D /S >nul & (
  For /f "Tokens=*" %%* in ('dir /B') Do (
   RD "%%*" /S /Q ||Del /F "%%*" )))>nul)

I hope some one can help me out in fixing the script, I think it's in the :sf2 and/or in combination with the %temp% part, somehow 2 things get mixed-up ("users temp" en "environment temp").

share|improve this question
possible related question:… – mailq Sep 8 '11 at 15:28
this is what I got/get with "my" script (nothing changed yet): C:\Documents and Settings\admin-lksvdd\D:\TEMP – APOC Sep 9 '11 at 10:52
Script works when: Default windows temp = used. So windows default set back to: %UserProfile%\Local Settings\Temp (for currentuser) and %SystemRoot%\Temp (system variable). Now how to fix the script so it will still work when none windows defaults are used (i.e. D:\TEMP) ... That's when things go horribly wrong :-( – APOC Sep 9 '11 at 10:55

1 Answer 1

up vote 0 down vote accepted

This seems to bee a work-around:

instead of

   pushD "%~1\%$$temp%" &&(
   rmdir /S /Q .


IF "%$$temp%"=="%$$temp:*:=%" (SET "tmppath=%~1\%$$temp%") ELSE SET "tmppath=%$$temp%"
   pushD "%tmppath%" &&(
   rmdir /S /Q .

credits go out to: Andriy M

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.