多选项批处理文件

Multiple Option Batch File

我正在尝试为我们的办公室制作一个简单的信使程序。 我不确定我错过了什么,但每次我尝试 运行 选项代码时,它只会向我的计算机发送消息,而不是我要向其发送消息的计算机。如果我没有简单的选项,我可以向另一台计算机发送消息。 请帮忙!

我只想选择一个数字,然后让程序将该选择粘贴到提示中。 例如用户选择了 1,即 a 人(a 人的代码是 /server:ECRCL)。代码“/server:ECRCL”现在显示在消息代码中“c”的提示中。 (%c% %m%)*

这是我的代码:

@echo off
Title Doodle's Messenger Program
echo Messenger 
chdir /
echo Select a Person
echo 1) Person a
echo 2) Person b
echo 3) Person c
echo 4) Person d

:START
echo ========================================
set /p c=Choose the number of the person you would like to message: 
if %c%=="1" echo /server:ECRCL
if %c%=="2" echo /server:ECRCB
if %c%=="3" echo /server:ECRCCI
if %c%=="4" echo /server:ECRCCO

:A 
set /p m=Message: 
msg * %c% %m% 
GoTo A 

:1
: echo %/server:ECRCL /time:300 /v /w%

:2
:/server:ECRCB

:3
:/server:ECRCCI

:4
:/server:ECRCCO

以下脚本确实适用于消息传递,但我想让其他人更容易键入 1234

@echo Copy one of the following computers you would like to message and paste it where it asks for the computer name.
@echo person a - /server:ECRCL
@echo person b - /server:ECRCB
@echo person c - /server:ECRCCI
@echo person d - /server:ECRCCO

:START
set /p c=Enter Selection:

:A
set /p m=Message:
msg * %c% %m%

GoTo A

下面是一个可能对您有所帮助的示例。

@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Title Doodle's Messenger Program

Set "msg=%SYSTEMROOT%\System32\msg.exe"
If %PROCESSOR_ARCHITECTURE:~-2% Equ 86 If Defined PROCESSOR_ARCHITEW6432 Set "msg=%SYSTEMROOT%\SysNative\msg.exe"
For %%G In ("%msg%") Do If "%%~aG" Lss "-" (Set "msg=") Else If "%%~aG" GEq "d" Set "msg="
If Not Defined msg (Echo Error! The system file msg.exe is missing.
    "%__AppDir__%timeout.exe" /T 3 /NoBreak 1> NUL
    GoTo :EOF)

:Start
Echo(
Echo Messenger Selection List
Echo 1. Viktor [//ECRCL]
Echo 2. Rashi  [//ECRCB]
Echo 3. Kian   [//ECRCCI]
Echo 4. Cedric [//ECRCCO]
Echo(
"%__AppDir__%choice.exe" /C 1234 /N /M "Enter the number for the user you would like to message." 
If %ErrorLevel% Equ 1 Set "Svr=ECRCL"
If %ErrorLevel% Equ 2 Set "Svr=ECRCB"
If %ErrorLevel% Equ 3 Set "Svr=ECRCCI"
If %ErrorLevel% Equ 4 Set "Svr=ECRCCO"

:AskMessage
Set "m="
Set /P "m=Please type your message, then press the enter key>"
If Not Defined m (
    Echo Empty messages are not allowed!
    "%__AppDir__%timeout.exe" /T 2 /NoBreak 1> NUL
    GoTo AskMessage
)

%msg% * /server:%svr% %m%

GoTo Start

正如 Compo 所说,choice 命令是这种菜单样式的选项,但是可以通过将索引变量与关联变量组合在一起,以更直接的方式使用 choice 返回的值来执行所需的命令,使用 For 循环元变量延迟扩展以从索引收件人号码中提取服务器 ID。

@Echo Off& Setlocal DisableDelayedExpansion
rem // Macro Definitions
 Set "MSG=%SYSTEMROOT%\System32\msg.exe * "
 Set "List/D/V=Set "#$L=0"&Set "$$L="&For %%n in (1 2)Do if %%n==2 (For %%G in (!$L!)Do (Set "%%~G" > Nul &Set /A "#$L+=1"))Else Set $L="
 Set "Div=---------------------------------------------------------------------------------------------"
 Set "MENU=Echo/!Div!&(For /F "Tokens=1,2 Delims==[" %%X in ('Set $V') Do Set /A "CNT=!#%%~X!" & Set "Array=%%~Y") & Set "Choices=" & (For %%A in (!ARRAY!)Do For /L %%U in (1 1 !CNT!)Do Set "Choices=!Choices!%%U"&For %%S in ("!%%~A[%%U]!")Do Echo/%%~A[%%U] %%~S !%%~S!)&Echo/[E]xit&Echo/!Div!"
rem // ACTION/E Macro to utilise the value of an Array Element
 Set "ACTION/E=For /F "Delims=" %%I in ('Choice /N /C:!Choices!E')Do IF "%%I"=="E" (Endlocal&Endlocal&Goto :Eof)Else For %%A in ("!Array![%%I]")Do $M !%%~A!"
rem // ACTION/S Macro to utilise a value assigned to the value of an Array Element (Sub value)
 Set "ACTION/S=For /F "Delims=" %%I in ('Choice /N /C:!Choices!E')Do IF "%%I"=="E" (Endlocal&Endlocal&Goto :Eof)Else For %%A in ("!Array![%%I]")Do For %%O in ("!%%~A!")Do $M !%%~O!"
 Setlocal EnableExtensions EnableDelayedExpansion
rem // Define associated variables between recipients and servers to enable Access to the values from indexed variables
 %List/D/V:$L=Users%"recipient[1]=Person 1" "recipient[2]=Person 2" "recipient[3]=Person 3" "recipient[4]=Person 4"
 %List/D/V:$L=Servers%"Person 1=/server:ECRCL" "Person 2=/server:ECRCB" "Person 3=/server:ECRCCI" "Person 4=/server:ECRCCO"
:Loop
CLS
 %MENU:$V=Users%
rem // examples of different command usage for ACTION macros. $M is substituted with the required command/s to executed on the value retrieved from the array
rem ::: %ACTION/E:$M=Echo/%
 Set "Input="&Set /P "input=Enter Message: "
rem ::: if not "!input!"=="" ((If /I "!input!"=="E" (Exit /B 0)Else Echo/Select Recipient & %ACTION/S:$M=Echo/!Msg!% !input! 2> Nul) || Echo/!Msg! not installed)Else Echo/Input required
 if not "!input!"=="" ((If /I "!input!"=="E" (Exit /B 0)Else Echo/Select Recipient & %ACTION/S:$M=!Msg!% !input! 2> Nul) || Echo/!Msg! not installed)Else Echo/Input required
Pause
Goto :Loop

上面的内容已经过修改,使宏对于不同的菜单用法更加通用。 ACTION 宏的 $M 子字符串替换为要对通过选择索引

的选择返回的值执行的命令