简体   繁体   English

批处理脚本:无法正确检查循环

[英]Batch Script: Can't get the loop to correctly check

Ok, I almost have this done but need some help getting the bugs out. 好的,我几乎完成了这项工作,但是需要一些帮助来解决错误。

The User Delete function works great and out puts the TEMP file like so: 用户删除功能非常有效,并且可以像下面这样放置TEMP文件:

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

":UserDeleteJoin" will output the following if i check it with an echo 如果我通过回显检查,“:UserDeleteJoin”将输出以下内容

Public,Default User,Default,All Users

The Full code that does what I have describe is as follows: 执行我所描述的完整代码如下:

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

So that takes care of the main issue, now only the rest of the script, which i initially got from "http://mstoneblog.wordpress.com/2010/12/08/user-profile-cleaning-script-for-windows-7/" which works great but I wanted to add the ability to only delete user profiles older than x number of days. 这样就解决了主要问题,现在只处理了脚本的其余部分,我最初是从“ http://mstoneblog.wordpress.com/2010/12/08/user-profile-cleaning-script-for-windows”获得的-7 /“效果很好,但我想添加仅删除x天以上的用户个人资料的功能。

So I have the initial user preserve function as what I need it keeping the regular users. 因此,我具有初始用户保留功能,可以保留常规用户。

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

The next chunk of script is unaltered but i'll post it to be complete. 下一个脚本块将保持不变,但我将其完整地发布。 This will include the line above and covers everything left after the :UserDeleteJoin function. 这将包括上面的行,并覆盖: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

Im pretty sure that the issue is with the error check but I can't really tell, cuz it only wants to hit the profiles "Trinity Tech Corp" and "Hell Bomb" and ignores all the other profiles despite what other settings are. 我非常确定问题出在错误检查上,但是我不能真正说出来,因为它只想打“ Trinity Tech Corp”和“ Hell Bomb”配置文件,而忽略其他所有配置文件,尽管有其他设置。 Can't seem to narrow down the issue more than that unfortunately, but that might be because i have been looking at it for WAY to long. 不幸的是,似乎无法缩小问题的范围,但这可能是因为我一直在研究WAY已有很长时间。

The part of the script that i added to the original script was as follow: 我添加到原始脚本中的脚本部分如下:

IF ERRORLEVEL=1 GOTO DELETECHECK

And

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

The output that is given to me when I run the script is as follows, which it should skip both of those folders, but it does not delete the other user profiles. 运行脚本时提供给我的输出如下,它应跳过这两个文件夹,但不会删除其他用户配置文件。

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

Other solutions: I was thinking this could be made a lot simpler is i could figure out how to get FORFILES to list all folders that are NOT older than 45 days then i could simply do another loop like i did for ":UserDeleteJoin" and would not have to edit the main part of the original script. 其他解决方案:我认为这可以简化很多,因为我可以弄清楚如何获取FORFILES列出不超过45天的所有文件夹,然后可以像对“:UserDeleteJoin”那样简单地进行另一个循环,并且不必编辑原始脚本的主要部分。 If anyone has any idea how I can do that i can avoid all the issues i am having now. 如果有人知道我该怎么办,我可以避免我现在遇到的所有问题。

Full Script 完整剧本

@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

From the original question: 从最初的问题:

I was thinking this could be made a lot simpler is i could figure out how to get FORFILES to list all folders that are NOT older than 45 days 我当时想这可以简化很多,因为我可以弄清楚如何获取FORFILES列出不超过45天的所有文件夹

You have already got the list of folders that are older then 45 days. 您已经有了45天之前的文件夹列表。 You can simply use FINDSTR to look for entries that are not in that list. 您可以简单地使用FINDSTR查找不在该列表中的条目。

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



Are you sure you want to delete "desktop.ini"? 您确定要删除“ desktop.ini”吗? Or are you just deleting it so that you only have folders to deal with? 还是只是删除它,以便只处理文件夹?

If you only want FORFILES to list folders, you can do the following: 如果只希望FORFILES列出文件夹,则可以执行以下操作:

forfiles /p %UsersPath% /m * /d -45 /c "cmd /c if @isdir=="TRUE" echo @file">%TEMP%\users.temp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM