gpt4 book ai didi

batch-file - 如何使用命令提示符或批处理文件检查用户是否存在?

转载 作者:行者123 更新时间:2023-12-05 06:23:15 26 4
gpt4 key购买 nike

我正在尝试创建一个删除用户的脚本。我想检查输入的用户是否存在,以便我可以说它已删除或者用户不存在。

我已经尝试过之前在网上找到的一些方法,但都没有用。这是我目前所拥有的。

:DelUser
cls
echo You chose to delete a user
echo ==========================
net user
echo ==========================
set UserDel=What is the name of the user you want to delete?
echo deleting user %UserDel%.....
net user | find /i %UserDel% || goto UserNoExist
net user %UserDel% /delete
echo User %UserDel% is deleted
goto Users

:UserNoExist
echo This user does not exist
pause
goto DelUser

最佳答案

1) 您可以使用net user 命令的退出代码。如果用户存在,则返回 0。%ERRORLEVEL% 变量将具有退出代码。

2) 为了得到命令提示符的输入,你应该使用SET命令和/p

set /p UserDel=What is the name of the user you want to delete?

所以你的代码应该是这样的:

set /p UserDel=What is the name of the user you want to delete?
net user %UserDel%
if %ERRORLEVEL% EQU 0 (
net user %UserDel% /delete
echo User %UserDel% is deleted
) else (
echo This user does not exist
)

关于batch-file - 如何使用命令提示符或批处理文件检查用户是否存在?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58607192/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com