Search code examples
batch-filesearchdelete-filetemp

Search & delete temp files/folder (script's got a failure)


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").


Solution

  • 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.