搜索与删除临时文件/文件夹(脚本失败)
我使用这个脚本来清理所有用户的历史记录、cookie 和缓存(临时 Internet 文件),它还应该清理临时目录,但似乎有问题。
我认为有两件事混淆了,%temp%
变量(= 我的环境中的 D:\TEMP) 以及 %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
但不知怎的,我无法让最后一个“临时部分”发挥作用,因此它会清理%temp%(我的环境中的D:\ Temp),并在%userprofile%中找到所有“临时目录”。
ie. 例如,这个适用于 %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
这个 ie. 适用于“用户临时” :
::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)
)
我希望有人可以帮助我修复脚本,我认为它在 :sf2 中和/或与 %temp% 部分结合使用,不知怎的,现在有两件事混淆了(“users temp”en “环境温度”)。
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").
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,这次我认为有一种方法可以解决这个问题,即在继续清理之前添加检查变量是否包含相对路径或绝对路径。这个想法是测试字符串中是否存在冒号 (
:
)。如果存在冒号,则路径之前无法转换为相对路径,因此应按原样使用,而不预先添加配置文件路径,否则应在继续之前附加配置文件路径。如果您愿意,您可以测试和使用以下基本示例:
在您的特定情况下,我可能会应用这样的方法:
而不是
我会尝试
如您所见,临时变量用于存储实际路径被处理。据我所知,仅替换清除临时文件夹的部分就足够了,但您可以看到,如果需要,该方法也可以轻松应用于其他文件夹。
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:
In your particular situation I would probably apply the method like this:
instead of
I would try
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.
看起来您的 sf2 子例程正在尝试通过切断路径的开头来获取 Temp 文件夹的相对路径,该路径本质上是用户配置文件路径:
其中 sf包含 Temp 文件夹和
usr
用户配置文件。因此,您将得到简单的Local Settings\Temp
,而不是C:\Documents and Settings\APOC\Local Settings\Temp
。但有一个问题。尽管为 sf 变量分配了正确的路径,但另一方面,由于某种原因,为 usr 变量分配了短命名变体 用户配置文件路径。也就是说,它接收的不是像
C:\Documents and Settings\APOC
这样的内容,而是像C:\DOCUME~1\APOC
这样的内容。这是有问题的行:因此,当执行先前引用的行时,预期的替换永远不会发生,因为
C:\Documents and Settings\APOC
与C:\DOCUME~1 不匹配\APOC
,自然。因此,$$temp
变量以完整路径而不是相对路径结束,因此,引用$$temp
没有按预期完成工作。简而言之,我认为您根本不需要那个
sf2
例程。sf1
例程似乎与sf2
相同,只是传递了一组不同的参数。所以,我建议你尝试这个:
It seems like your
sf2
subroutine is trying to get a relative path of the Temp folder by cutting off the beginning of the path, which is essentially the user profile path:where
sf
contains the Temp folder andusr
the user profile. So instead of, for instance,C:\Documents and Settings\APOC\Local Settings\Temp
you would get simplyLocal Settings\Temp
.But there's a problem. Though the
sf
variable is assigned with the proper path, theusr
variable, on the other hand, for some reason is assigned with the short named variant of the user profile path. That is, instead of something likeC:\Documents and Settings\APOC
it receives something likeC:\DOCUME~1\APOC
. Here's the offending line:So, when the formerly quoted line is executed, the expected substitution never happens, because
C:\Documents and Settings\APOC
doesn't matchC:\DOCUME~1\APOC
, naturally. As a result, the$$temp
variable ends up with the complete path instead of the relative path, and because of that, the relevant parts of your code that reference$$temp
don't do their job as expected.In short, I think you don't need that
sf2
routine at all. Thesf1
routine seems to do just the same assf2
, only with a different set of parameters to pass. So, instead ofI'd suggest you to try this:
我稍微改变了 temp 的行,这是正确的方法吗? (它确实有效,但格式正确吗?)
+额外的“Del / f / s / q”用于可能的硬/只读文件(和附加属性)
顺便说一下
脚本中的“temp”部分似乎仍然没有 100% 顺利,我将 Windows 中的默认临时设置更改为测试目的的默认路径,并收到以下 3 个错误:
错误的屏幕截图 | url: http://img199.imageshack.us/img199/9127/errorif.jpg
使用 Windows 默认临时路径运行脚本:
[user] %UserProfile%\Local Settings\Temp and [system] %SystemRoot%\Temp
我不知道为什么会收到这些错误,当我将两个临时参数设置为 D:\TEMP 时,我收到 3 次相同类型的错误,但比 D:\TEMP 而不是可用/活动用户\本地设置\Temp。也许问题出在当地人~1???
(虽然临时目录确实被清理了,但为什么它说找不到路径?)
I changed the line for temp a little, is this a correct way? (it does work but is it properly formated?)
+Extra "Del /f /s /q" for possible hard/read-only files (and additional attrib)
By the way
There still seems something not going 100% in the script with the "temp"-part, I changed my default temp settings in windows to the default paths voor testing purposes and I get these 3 error's:
screenshot of the error | url: http://img199.imageshack.us/img199/9127/errorif.jpg
script run with the windows default temp-path's:
[user] %UserProfile%\Local Settings\Temp and [system] %SystemRoot%\Temp
I'm not sure why I get these error's, when I set both temp parameters to D:\TEMP, I get 3 times the same sort of error but than for D:\TEMP instead of available/active users \local settings\Temp. Perhaps the problem is in locals~1???
(The temp-directories do get cleaned though, but why does it say it can't find the path?)