Erro de sintaxe no loop FOR no script de lote de limpeza browsercache

1

Estou tentando criar um script de limpeza de browsercache em lote, que pode ser executado por um administrador em um servidor para limpar todos os usuários do cache do navegador por demanda ou por agendamento.

No entanto, eu estou bem preso no loop principal. Ele está lançando de volta o erro "A sintaxe do comando está incorreta".

Tenho pesquisado todas as respostas e descobri que o problema é mais provável com as variáveis, mas não consegui fazê-lo funcionar.

Aqui está o código que eu reverti para onde eu recebi o erro, então talvez você possa me ajudar a corrigir isso.

Obrigado pela sua grande ajuda!

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
    
por Robert Burik 14.09.2016 / 15:06

1 resposta

1

Obrigado por todos, isso chegou a uma solução de trabalho. Sinta-se à vontade para usar este script para limpar o cache do navegador para todos os usuários:

@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
    
por 15.09.2016 / 14:50