Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6562945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:46:14+00:00 2026-05-25T13:46:14+00:00

I use this script to clean history, cookies and cache (Temporary Internet Files) for

  • 0

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.

Two 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 ***


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

:Clear
REM echo.&echo.%~1\%$$Cache%
   pushD "%~1\%$$Cache%" &&(
   rmdir /S /Q .
   popD)2>C:\test1_TEMP_IE.txt

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 .
   popD)2>C:\test1_History.txt

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

ECHO.&echo.%~1\%$$temp%
   pushD "%~1\%$$temp%" &&(
   rmdir /S /Q .
   popD)2>C:\test1_Temp.txt
exit /b 0

:getspecialfolders
   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

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

:sf2
   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 to also find al “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. work 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 fixing the script, I think it’s in the :sf2 and/or in combination with the %temp% part, somehow 2 things get mixed-up now (“users temp” en “environment temp”).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T13:46:14+00:00Added an answer on May 25, 2026 at 1:46 pm

    All right, this time I think there is a way to fix this, namely, by adding a check whether the variable contains a relative or an absolute path, directly before proceeding with the cleanup. The idea is to test for the presence of the colon (:) in the string. If the colon is present then the path could not be converted to a relative path previously and so should be used as is, without pre-pending the profile path, otherwise the profile path should be attached before going on.

    Here’s a basic example that you can test and play with, if you like:

    @ECHO OFF
    SET somepath=D:\TEMP
    CALL :checkpath
    SET "somepath=Local Settings\Temp"
    CALL :checkpath
    PAUSE
    GOTO :EOF
    
    :checkpath
    IF "%somepath%"=="%somepath:*:=%" (ECHO Relative path) ELSE ECHO (Absolute path)
    

    In your particular situation I would probably apply the method like this:

    instead of

    …
    ECHO.&echo.%~1\%$$temp%
       pushD "%~1\%$$temp%" &&(
       rmdir /S /Q .
       popD)2>C:\test1_Temp.txt
    …
    

    I would try

    …
    IF "%$$temp%"=="%$$temp:*:=%" (SET "tmppath=%~1\%$$temp%") ELSE SET "tmppath=%$$temp%"
    ECHO.&echo.%tmppath%
       pushD "%tmppath%" &&(
       rmdir /S /Q .
       popD)2>C:\test1_Temp.txt
    …
    

    As you can see, a temporary variable is used to store the actual path to be processed. I understand it is enough to replace only the part where the temporary folder is being cleared, but you can see that the method can be easily applied to other folders as well, if needed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this .bat script which I use to maven package my application. Problem
I'm puzzled with this test script: #!perl use strict; use warnings; use encoding 'utf8';
This question is simple. What function would I use in a PHP script to
I want to use a temp directory that will be unique to this build.
I use this script to navigate with keyboard arrow keys. But the problem is
I am converting a linux script from http://www.perlmonks.org/index.pl?node_id=217166 specifically this: #!/usr/bin/perl -w use strict;
I see this often in the build scripts of projects that use autotools (autoconf,
I use this tool called Lazy C++ which breaks a single C++ .lzz file
I use this line in my .htaccess file to automatically add a trailing slash
Can I use this approach efficiently? using(SqlCommand cmd = new SqlCommand(GetSomething, new SqlConnection(Config.ConnectionString)) {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.