0

好的,我几乎完成了这项工作,但需要一些帮助才能消除错误。

用户删除功能效果很好,并且输出 TEMP 文件如下:

"All Users"
"Default"
"Default User"
"Public"

":UserDeleteJoin" 如果我​​用回显检查它,将输出以下内容

Public,Default User,Default,All Users

完成我所描述的完整代码如下:

set UsersPath=%systemdrive%\Users\
DEL /F /Q /A:H %UsersPath%\desktop.ini 2>nul>nul
forfiles /p %UsersPath% /m * /d -45 /c "cmd /c echo @file">%TEMP%\users.temp
FOR /F "skip=1 tokens=1 delims=" %%P IN (%TEMP%\users.temp) DO CALL:UserDeleteJoin %%P
set userdelete=%userdelete:"=%
set userdelete=%userdelete:~0,-1%
echo %userdelete%
pause
GOTO:USERPRESERVE

:UserDeleteJoin
if UserDeleteJoin_Skip=="" (set userdelete=%1 & set UserDeleteJoin_Skip=1) ELSE (set userdelete=%1,%userdelete%)
goto:eof

这样就解决了主要问题,现在只有脚本的其余部分,我最初是从“http://mstoneblog.wordpress.com/2010/12/08/user-profile-cleaning-script-for-windows -7/" 效果很好,但我想添加仅删除超过 x 天的用户配置文件的功能。

所以我有初始用户保留功能,因为我需要它来保持普通用户。

set userpreserve="All Users,Default,Default User,Hell Bomb"

下一个脚本块没有改变,但我会发布它以完成。这将包括上面的行并涵盖 :UserDeleteJoin 函数之后剩下的所有内容。

USERPRESERVE
set userpreserve="All Users,Default,Default User,Hell Bomb"

FOR /f "tokens=*" %%a IN ('reg query "hklm\software\microsoft\windows nt\currentversion\profilelist"^|find /i "s-1-5-21"') DO CALL :REGCHECK "%%a"
GOTO VERIFY

:REGCHECK
set SPACECHECK=
FOR /f "tokens=3,4" %%b in ('reg query %1 /v ProfileImagePath') DO SET USERREGPATH=%%b %%c
FOR /f "tokens=2" %%d in ('echo %USERREGPATH%') DO SET SPACECHECK=%%d
IF ["%SPACECHECK%"]==[""] GOTO REGCHECK2
GOTO USERCHECK

:REGCHECK2
FOR /f "tokens=3" %%g in ('reg query %1 /v ProfileImagePath') DO SET USERREGPATH=%%g
GOTO USERCHECK

:USERCHECK
FOR /f "tokens=3 delims=\" %%e in ('echo %USERREGPATH%') DO SET USERREG=%%e
FOR /f "tokens=1 delims=." %%f IN ('echo %USERREG%') DO SET USERREGPARSE=%%f
ECHO %USERPRESERVE%|find /I "%USERREGPARSE%" > NUL
IF ERRORLEVEL=1 GOTO DELETECHECK
IF ERRORLEVEL=0 GOTO SKIP

:DELETECHECK
ECHO %userdelete%|find /I "%USERREGPARSE%" > NUL
IF ERRORLEVEL=1 GOTO SKIP
IF ERRORLEVEL=0 GOTO CL
GOTO:EOF

:SKIP
ECHO Skipping user clean for %USERREG%
GOTO :EOF

:CLEAN
ECHO Cleaning user profile for %USERREG%
::rmdir "C:\Users\%USERREG%" /s /q > NUL
ECHO Cleaning user registry for %USERREG%
::reg delete %1 /f
::IF EXIST "C:\Users\%USERREG%" GOTO RETRYCLEAN1
GOTO :EOF

我很确定问题出在错误检查上,但我无法确定,因为它只想点击配置文件“Trinity Tech Corp”和“Hell Bomb”,而忽略所有其他配置文件,尽管其他设置是什么。不幸的是,似乎无法缩小问题的范围,但这可能是因为我已经研究了很久。

我添加到原始脚本的部分脚本如下:

IF ERRORLEVEL=1 GOTO DELETECHECK

:DELETECHECK
ECHO %userdelete%|find /I "%USERREGPARSE%" > NUL
IF ERRORLEVEL=1 GOTO SKIP
IF ERRORLEVEL=0 GOTO CL
GOTO:EOF

当我运行脚本时给我的输出如下,它应该跳过这两个文件夹,但它不会删除其他用户配置文件。

Skipping user clean for Trinity Tech
Skipping user clean for Hell Bomb

其他解决方案:我认为这可以变得更简单,我可以弄清楚如何让 FORFILES 列出所有不超过 45 天的文件夹,然后我可以像为“:UserDeleteJoin”所做的那样简单地做另一个循环并且会不必编辑原始脚本的主要部分。如果有人知道我该怎么做,我可以避免我现在遇到的所有问题。

完整脚本

@ECHO OFF

set UsersPath=%systemdrive%\Users\
DEL /F /Q /A:H %UsersPath%\desktop.ini 2>nul>nul
forfiles /p %UsersPath% /m * /d -45 /c "cmd /c echo @file">%TEMP%\users.temp
FOR /F "skip=1 tokens=1 delims=" %%P IN (%TEMP%\users.temp) DO CALL:UserDeleteJoin %%P
set userdelete=%userdelete:"=%
set userdelete=%userdelete:~0,-1%
GOTO:USERPRESERVE

:UserDeleteJoin
if UserDeleteJoin_Skip=="" (set userdelete=%1 & set UserDeleteJoin_Skip=1) ELSE (set userdelete=%1,%userdelete%)
goto:eof

:USERPRESERVE
set userpreserve="All Users,Default,Default User,Hell Bomb"

FOR /f "tokens=*" %%a IN ('reg query "hklm\software\microsoft\windows nt\currentversion\profilelist"^|find /i "s-1-5-21"') DO CALL :REGCHECK "%%a"
GOTO VERIFY

:REGCHECK
set SPACECHECK=
FOR /f "tokens=3,4" %%b in ('reg query %1 /v ProfileImagePath') DO SET USERREGPATH=%%b %%c
FOR /f "tokens=2" %%d in ('echo %USERREGPATH%') DO SET SPACECHECK=%%d
IF ["%SPACECHECK%"]==[""] GOTO REGCHECK2
GOTO USERCHECK

:REGCHECK2
FOR /f "tokens=3" %%g in ('reg query %1 /v ProfileImagePath') DO SET USERREGPATH=%%g
GOTO USERCHECK

:USERCHECK
FOR /f "tokens=3 delims=\" %%e in ('echo %USERREGPATH%') DO SET USERREG=%%e
FOR /f "tokens=1 delims=." %%f IN ('echo %USERREG%') DO SET USERREGPARSE=%%f
ECHO %USERPRESERVE%|find /I "%USERREGPARSE%" > NUL
IF ERRORLEVEL=1 GOTO DELETECHECK
IF ERRORLEVEL=0 GOTO SKIP

:DELETECHECK
FOR /f "tokens=3 delims=\" %%e in ('echo %USERREGPATH%') DO SET USERREG=%%e
FOR /f "tokens=1 delims=." %%f IN ('echo %USERREG%') DO SET USERREGPARSE=%%f
ECHO %userdelete%|find /I "%USERREGPARSE%" > NUL
IF ERRORLEVEL=1 GOTO SKIP
IF ERRORLEVEL=0 GOTO CL
GOTO:EOF

:SKIP
ECHO Skipping user clean for %USERREG%
GOTO :EOF

:CLEAN
ECHO Cleaning user profile for %USERREG%
::rmdir "C:\Users\%USERREG%" /s /q > NUL
ECHO Cleaning user registry for %USERREG%
::reg delete %1 /f
::IF EXIST "C:\Users\%USERREG%" GOTO RETRYCLEAN1
GOTO :EOF

:RETRYCLEAN1
ECHO Retrying clean of user profile %USERREG%
rmdir "C:\Users\%USERREG%" /s /q > NUL
IF EXIST "C:\Users\%USERREG%" GOTO RETRYCLEAN2
GOTO :EOF

:RETRYCLEAN2
ECHO Retrying clean of user profile %USERREG%
rmdir "C:\Users\%USERREG%" /s /q > NUL
GOTO :EOF

:VERIFY
FOR /f "tokens=*" %%g IN ('reg query "hklm\software\microsoft\windows nt\currentversion\profilelist"^|find /i "s-1-5-21"') DO CALL :REGCHECKV "%%g"
GOTO REPORT

:REGCHECKV
set SPACECHECKV=
FOR /f "tokens=3,4" %%h in ('reg query %1 /v ProfileImagePath') DO SET USERREGPATHV=%%h %%i
FOR /f "tokens=2" %%j in ('echo %USERREGPATHV%') DO SET SPACECHECKV=%%j
IF ["%SPACECHECKV%"]==[""] GOTO REGCHECKV2
GOTO USERCHECKV

:REGCHECKV2
FOR /f "tokens=3" %%k in ('reg query %1 /v ProfileImagePath') DO SET USERREGPATHV=%%k
GOTO USERCHECKV

:USERCHECKV
FOR /f "tokens=3 delims=\" %%l in ('echo %USERREGPATHV%') DO SET USERREGV=%%l
FOR /f "tokens=1 delims=." %%m IN ('echo %USERREGV%') DO SET USERREGPARSEV=%%m
ECHO %USERPRESERVE%|find /I "%USERREGPARSEV%" > NUL
IF ERRORLEVEL=1 GOTO VERIFYERROR
IF ERRORLEVEL=0 GOTO :EOF

:VERIFYERROR
SET USERERROR=YES
GOTO :EOF

:REPORT
IF [%USERERROR%]==[YES] (
        set RESULT=FAILURE
)       ELSE (
        set RESULT=SUCCESS
)

Pause
:EXIT
exit

:EOF
4

1 回答 1

1

从原来的问题:

我在想这可以变得更简单,因为我可以弄清楚如何让 FORFILES 列出所有不超过 45 天的文件夹

您已经获得了 45 天之前的文件夹列表。您可以简单地使用 FINDSTR 来查找不在该列表中的条目。

forfiles /p %UsersPath% /m * /c "cmd /c echo @file" | findstr /vixg:"%TEMP%\users.temp"



您确定要删除“desktop.ini”吗?还是您只是删除它,以便您只有文件夹要处理?

如果您只希望 FORFILES 列出文件夹,则可以执行以下操作:

forfiles /p %UsersPath% /m * /d -45 /c "cmd /c if @isdir=="TRUE" echo @file">%TEMP%\users.temp
于 2012-06-24T14:17:22.790 回答