浏览器缓存清除批处理脚本中的 FOR 循环语法错误

浏览器缓存清除批处理脚本中的 FOR 循环语法错误

我正在尝试批量创建一个浏览器缓存清除脚本,该脚本可以由服务器上的管理员运行,以按需求或按计划清除所有用户的浏览器缓存。

然而,我在主 for 循环中卡住了。它返回“命令语法不正确”错误。

我一直在寻找答案,我发现问题很可能出在变量上,但我无法让它发挥作用。

这是我恢复到第一次出现错误的位置的代码,所以也许您可以帮助我纠正它。

非常感谢您的帮助!

REM @echo off
CLS
SETLOCAL ENABLEDELAYEDEXPANSION

REM @@@@ creating listfile path @@@@
IF NOT EXIST "%SystemDrive%\Temp" MD "%SystemDrive%\Temp"
SET UserListFile=%SystemDrive%\Temp\userlist.txt

REM @@@@ DELeting already existing listfile @@@@
IF NOT EXIST %UserListFile% GOTO STARTOFSCRIPT
DEL %UserListFile%

:STARTOFSCRIPT

REM @@@@ writing userlist into file @@@@
DIR %SystemDrive%\Users\ /A:D /B /R > %UserListFile%

REM @@@@ start of main FOR loop @@@@
FOR /F "tokens=*" %%A IN (%UserListFile%) DO (
ECHO %%A

REM @@@@ Chrome cleaning part @@@@
IF NOT EXIST "%SystemDrive%\Users\%%A\AppData\Local\Google\Chrome\User Data\Default" GOTO NOCHROMEDEFAULT
ECHO Cleaning Chrome ...
CD "%SystemDrive%\Users\%%A\AppData\Local\Google\Chrome\User Data\Default"
DEL /Q /S /F "Application Cache"
DEL /Q /S /F "Cache"
DEL /Q /S /F "Media Cache"
:NOCHROMEDEFAULT
IF NOT EXIST "%SystemDrive%\Users\%%A\AppData\Local\Google\Chrome\User Data\Guest Profile" GOTO NOCHROMEGUEST
CD "%SystemDrive%\Users\%%A\AppData\Local\Google\Chrome\User Data\Guest Profile"
DEL /Q /S /F "Application Cache"
DEL /Q /S /F "Cache"
DEL /Q /S /F "Media Cache"
:NOCHROMEGUEST
SET PROFNUM=0
FOR /L %%G IN (1,1,10) DO (
SET /A PROFNUM+=1
IF NOT EXIST "%SystemDrive%\Users\%%A\AppData\Local\Google\Chrome\User^ Data\Profile^ %PROFNUM%" GOTO NOPROFNUM
CD "%SystemDrive%\Users\%%A\AppData\Local\Google\Chrome\User Data\Profile %PROFNUM%"
DEL /Q /S /F "Application Cache"
DEL /Q /S /F "Cache"
DEL /Q /S /F "Media Cache"
)
:NOPROFNUM

REM @@@@ Mozilla cleaning part @@@@
IF NOT EXIST "%SystemDrive%\Users\%%A\AppData\Local\Mozilla\Firefox\Profiles" GOTO NOMOZILLA
ECHO Cleaning Mozilla ...
SET DataDir=%SystemDrive%\Users\%%A\AppData\Local\Mozilla\Firefox\Profiles
DEL /Q /S /F "%DataDir%"  >NUL 2>&1
RD /S /Q "%DataDir%"  >NUL 2>&1
for /d %%X in (%SystemDrive%\Users\%%A\AppData\Roaming\Mozilla\Firefox\Profiles\*) do DEL /Q /S /F %%X\*sqlite  >NUL 2>&1
:NOMOZILLA

REM @@@@ Internet Explorer cleaning part @@@@
IF NOT EXIST "%SystemDrive%\Users\%%A\AppData\Local\Microsoft\Intern~1" GOTO NOEXPLR
ECHO Cleaning IE ...
SET DataDir=%SystemDrive%\Users\%%A\AppData\Local\Microsoft\Intern~1
DEL /Q /S /F "%DataDir%"  >NUL 2>&1
RD /S /Q "%DataDir%"  >NUL 2>&1
SET History=%SystemDrive%\Users\%%A\AppData\Local\Microsoft\Windows\History
DEL /Q /S /F "%History%"  >NUL 2>&1
RD /S /Q "%History%"  >NUL 2>&1
SET IETemp=%SystemDrive%\Users\%%A\AppData\Local\Microsoft\Windows\Tempor~1
DEL /Q /S /F "%IETemp%" >NUL 2>&1
RD /S /Q "%IETemp%" >NUL 2>&1
SET Cookies=%SystemDrive%\Users\%%A\AppData\Roaming\Microsoft\Windows\Cookies
DEL /Q /S /F "%Cookies%"  >NUL 2>&1
RD /S /Q "%Cookies%"  >NUL 2>&1
:NOEXPLR

REM @@@@ Opera cleaning part @@@@
IF NOT EXIST "%SystemDrive%\Users\%%A\AppData\Local\Opera\Opera" GOTO NOOPERA
ECHO Cleaning Opera ...
SET DataDir=%SystemDrive%\Users\%%A\AppData\Local\Opera\Opera
SET DataDir2=%SystemDrive%\Users\%%A\AppData\Roaming\Opera\Opera
DEL /Q /S /F "%DataDir%"  >NUL 2>&1
RD /S /Q "%DataDir%"  >NUL 2>&1
DEL /Q /S /F "%DataDir2%"  >NUL 2>&1
RD /S /Q "%DataDir2%"  >NUL 2>&1
:NOOPERA

REM @@@@ Safari cleaning part @@@@
IF NOT EXIST "%SystemDrive%\Users\%%A\AppData\Local\Applec~1\Safari" GOTO NOSAFARI
ECHO Cleaning Safari ...
SET DataDir=%SystemDrive%\Users\%%A\AppData\Local\Applec~1\Safari
SET DataDir2=%SystemDrive%\Users\%%A\AppData\Roaming\Applec~1\Safari
DEL /Q /S /F "%DataDir%\History"  >NUL 2>&1
RD /S /Q "%DataDir%\History"  >NUL 2>&1
DEL /Q /S /F "%DataDir%\Cache.db"  >NUL 2>&1
DEL /Q /S /F "%DataDir%\WebpageIcons.db"  >NUL 2>&1
DEL /Q /S /F "%DataDir2%"  >NUL 2>&1
RD /S /Q "%DataDir2%"  >NUL 2>&1
:NOSAFARI


REM @@@@ end of main FOR loop @@@@
)

:ENDOFSCRIPT

DEL /Q /S /F "%UserListFile%"  >NUL 2>&1
TIMEOUT /T 5  >NUL 2>&1

答案1

谢谢大家,这已经找到了一个可行的解决方案。请随意使用此脚本为所有用户清理浏览器缓存:

@echo off
CLS
SETLOCAL ENABLEDELAYEDEXPANSION

REM @@@@ creating listfile path @@@@
IF NOT EXIST "%SystemDrive%\Temp" MD "%SystemDrive%\Temp"
SET "UserListFile=%SystemDrive%\Temp\userlist.txt"

REM @@@@ DELeting already existing listfile @@@@
IF NOT EXIST %UserListFile% GOTO STARTOFSCRIPT
DEL %UserListFile%

:STARTOFSCRIPT

REM @@@@ writing userlist into file @@@@
DIR %SystemDrive%\Users\ /A:D /B /R > %UserListFile%

REM @@@@ start of main FOR loop @@@@
FOR /F "tokens=*" %%A IN (%UserListFile%) DO (
REM ECHO %%A
CALL :BROWSERCLEAR "%%A"
)

GOTO ENDOFSCRIPT

:BROWSERCLEAR
SET UsrDir=%1
SET UsrDir=%UsrDir:"=%
%SystemDrive%

REM @@@@ Chrome cleaning part @@@@
IF NOT EXIST "%SystemDrive%\Users\%UsrDir%\AppData\Local\Google\Chrome\User Data\Default\" GOTO NOCHROMEDEFAULT
ECHO Cleaning Chrome in %UsrDir% ...
CD "%SystemDrive%\Users\%UsrDir%\AppData\Local\Google\Chrome\User Data\Default\"
DEL /Q /S /F "Application Cache"  >NUL 2>&1
DEL /Q /S /F "Cache"  >NUL 2>&1
DEL /Q /S /F "Media Cache"  >NUL 2>&1
:NOCHROMEDEFAULT
:NOCHROMEDEFAULT2
IF NOT EXIST "%SystemDrive%\Users\%UsrDir%\AppData\Local\Google\Chrome\User Data\Guest Profile\" GOTO NOCHROMEGUEST
CD "%SystemDrive%\Users\%UsrDir%\AppData\Local\Google\Chrome\User Data\Guest Profile\"
DEL /Q /S /F "Application Cache"  >NUL 2>&1
DEL /Q /S /F "Cache"  >NUL 2>&1
DEL /Q /S /F "Media Cache"  >NUL 2>&1
:NOCHROMEGUEST
:NOCHROMEGUEST2
FOR /L %%G IN (0,1,10) DO (
IF NOT EXIST "%SystemDrive%\Users\%UsrDir%\AppData\Local\Google\Chrome\User Data\Profile %%G\" GOTO NOPROFNUM
CD "%SystemDrive%\Users\%UsrDir%\AppData\Local\Google\Chrome\User Data\Profile %%G\"
DEL /Q /S /F "Application Cache"  >NUL 2>&1
DEL /Q /S /F "Cache"  >NUL 2>&1
DEL /Q /S /F "Media Cache"  >NUL 2>&1
:NOPROFNUM
:NOPROFNUM2
)

REM @@@@ Mozilla cleaning part @@@@
IF NOT EXIST "%SystemDrive%\Users\%UsrDir%\AppData\Local\Mozilla\Firefox\Profiles\" GOTO NOMOZILLA
ECHO Cleaning Mozilla in %UsrDir% ...
SET DataDir=%SystemDrive%\Users\%UsrDir%\AppData\Local\Mozilla\Firefox\Profiles\
DEL /Q /S /F "%DataDir%"  >NUL 2>&1
RD /S /Q "%DataDir%"  >NUL 2>&1
for /d %%X in (%SystemDrive%\Users\%UsrDir%\AppData\Roaming\Mozilla\Firefox\Profiles\*) do DEL /Q /S /F %%X\*sqlite  >NUL 2>&1
:NOMOZILLA
:NOMOZILLA2

REM @@@@ Internet Explorer cleaning part @@@@
IF NOT EXIST "%SystemDrive%\Users\%UsrDir%\AppData\Local\Microsoft\Intern~1\" GOTO NOEXPLR
ECHO Cleaning IE in %UsrDir% ...
SET DataDir=%SystemDrive%\Users\%UsrDir%\AppData\Local\Microsoft\Intern~1\
DEL /Q /S /F "%DataDir%"  >NUL 2>&1
RD /S /Q "%DataDir%"  >NUL 2>&1
SET History=%SystemDrive%\Users\%UsrDir%\AppData\Local\Microsoft\Windows\History\
DEL /Q /S /F "%History%"  >NUL 2>&1
RD /S /Q "%History%"  >NUL 2>&1
SET IETemp=%SystemDrive%\Users\%UsrDir%\AppData\Local\Microsoft\Windows\Tempor~1\
DEL /Q /S /F "%IETemp%" >NUL 2>&1
RD /S /Q "%IETemp%" >NUL 2>&1
SET Cookies=%SystemDrive%\Users\%UsrDir%\AppData\Roaming\Microsoft\Windows\Cookies\
DEL /Q /S /F "%Cookies%"  >NUL 2>&1
RD /S /Q "%Cookies%"  >NUL 2>&1
:NOEXPLR
:NOEXPLR2

REM @@@@ Opera cleaning part @@@@
IF NOT EXIST "%SystemDrive%\Users\%UsrDir%\AppData\Local\Opera\Opera\" GOTO NOOPERA
ECHO Cleaning Opera in %UsrDir% ...
SET DataDir=%SystemDrive%\Users\%UsrDir%\AppData\Local\Opera\Opera\
SET DataDir2=%SystemDrive%\Users\%UsrDir%\AppData\Roaming\Opera\Opera\
DEL /Q /S /F "%DataDir%"  >NUL 2>&1
RD /S /Q "%DataDir%"  >NUL 2>&1
DEL /Q /S /F "%DataDir2%"  >NUL 2>&1
RD /S /Q "%DataDir2%"  >NUL 2>&1
:NOOPERA
:NOOPERA2

REM @@@@ Safari cleaning part @@@@
IF NOT EXIST "%SystemDrive%\Users\%UsrDir%\AppData\Local\Applec~1\Safari\" GOTO NOSAFARI
ECHO Cleaning Safari in %UsrDir% ...
SET DataDir=%SystemDrive%\Users\%UsrDir%\AppData\Local\Applec~1\Safari\
SET DataDir2=%SystemDrive%\Users\%UsrDir%\AppData\Roaming\Applec~1\Safari\
DEL /Q /S /F "%DataDir%\History"  >NUL 2>&1
RD /S /Q "%DataDir%\History"  >NUL 2>&1
DEL /Q /S /F "%DataDir%\Cache.db"  >NUL 2>&1
DEL /Q /S /F "%DataDir%\WebpageIcons.db"  >NUL 2>&1
DEL /Q /S /F "%DataDir2%"  >NUL 2>&1
RD /S /Q "%DataDir2%"  >NUL 2>&1
:NOSAFARI
:NOSAFARI2

:ENDOFSCRIPT
DEL /Q /S /F "%UserListFile%"  >NUL 2>&1

相关内容