好的,我几乎已经完成了这项工作,但需要一些帮助才能解决问题。
用户删除功能很有效,并且输出TEMP文件如下:
"All Users"
"Default"
"Default User"
"Public"
“:UserDeleteJoin”如果我用echo
检查它将输出以下内容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
答案 0 :(得分: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