我的批处理代码做错了什么?



我在学校的网络爱国者团队,我正试图为windows图像制作一些批处理脚本。我不能让我的代码像我想的那样工作,我想做的是让它运行,提示用户输入,再次要求输入,然后要么创建一个用户帐户,要么删除一个。下面是我的代码:

@ECHO off
xterm -e start
:start
echo 0a
echo Welcome to the Master Script For Windows CyberPatriot Completion
echo ----------------------------------------------------------------
echo What would you like to do first.
echo 1...User Add /w password pre-set (meets compexity requirements)
echo 2...User Delete (all files)
pause
set /P choice1= "Please make your choice: " 
if %choice1% == 1 (
GOTO useradd
)
if %choice1% == 2 (
GOTO userdelete
) 
:useradd
set /P choice2= "Please enter the name of the user you want to create (the password is pre-set 
to Password123456789!): "
net user %choice2% Password123456789! /add
:userdelete
set /P choice2= "Please enter the name of the user you want to delete (WARNING! ALL FILES WILL 
BE DELETED PLEASE CHECK FIRST): "
net user %choice2% /del

另外,如果你有任何批处理资源,语言建议,或者关于我应该/不应该做什么的建议,请让我知道。提前感谢你的帮助:)

可以使用以下批处理脚本:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
color 0A
:Begin
cls
echo Welcome to the master script for Windows CyberPatriot completion
echo ----------------------------------------------------------------
echo(
echo What would you like to do first?
echo(
echo 1 ... User Add with password preset (meets complexity requirements)
echo 2 ... User Delete (all files)
echo(
%SystemRoot%System32choice.exe /C 12 /N /M "Please make your choice:"
if errorlevel 2 goto UserDelete
if errorlevel 1 goto UserAdd
goto Begin
:UserAdd
echo(
echo Please enter the name of the user you want to create.
echo The password is pre-set to: Password123456789!
echo(
:PromptAdd
set "NameUser="
set /P "NameUser=User name: "
if not defined NameUser goto PromptAdd
set "NameUser=%NameUser:"=%"
if not defined NameUser goto PromptAdd
echo(
%SystemRoot%System32net.exe user "%NameUser%" Password123456789! /ADD
echo(
if errorlevel 1 goto PromptAdd
goto EndBatch
:UserDelete
echo(
echo Please enter the name of the user you want to delete.
echo WARNING! ALL FILES WILL BE DELETED PLEASE CHECK FIRST.
echo(
:PromptDelete
set "NameUser="
set /P "NameUser=User name: "
if not defined NameUser goto PromptDelete
set "NameUser=%NameUser:"=%"
if not defined NameUser goto PromptDelete
echo(
%SystemRoot%System32net.exe user "%NameUser%" /DELETE
echo(
if errorlevel 1 goto PromptDelete
:EndBatch
pause
color
endlocal

要了解所使用的命令及其工作原理,请打开命令提示符窗口,在其中执行以下命令,并完整而仔细地阅读显示的每个命令的帮助页。

  • choice /?
  • cls /?
  • color /?
  • echo /?
  • endlocal /?
  • goto /?
  • if /?
  • net /?
  • net user /?
  • pause /?
  • set /?
  • setlocal /?

参见:

  • 如何阻止Windows命令解释器在错误的用户输入上退出批处理文件执行?
  • 为什么在命令行上使用'set var = text'后没有'echo %var%'的字符串输出?
  • DosTips论坛主题:ECHO。不能给出文本或空行-使用ECHO/
  • 代替