多选项批处理文件

问题描述

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

我只想选择一个数字,然后让程序将该选择粘贴到提示中。 例如,用户选择了1,即人a(人a的代码为/ server:ECRCL)。现在,味精代码中的“ c”提示上将显示代码“ / server:ECRCL”。 (%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子字符串替换为要对通过所选索引的选择选择返回的值执行的命令