AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / computer / 问题 / 1592249
Accepted
batchcoding____s
batchcoding____s
Asked: 2020-10-11 05:48:52 +0800 CST2020-10-11 05:48:52 +0800 CST 2020-10-11 05:48:52 +0800 CST

如何使带有空格的cmd读取输入作为不同的术语/变量?

  • 772

当我希望我的批次获得输入时,我使用:

set /p myvar=Type here:

通过运行代码并使用空格输入类似这样的内容:

这是输入。

我可以将四个单词分别分配为一些变量吗,例如

1=This
2=is
3=the
4=input.

这样我就可以根据需要使用这些变量( %1%, %2%, %3%, %4% )?

或者还有其他输入命令吗?

windows-8.1 command-line
  • 2 2 个回答
  • 566 Views

2 个回答

  • Voted
  1. Io-oI
    2020-10-11T07:02:35+08:002020-10-11T07:02:35+08:00
    @echo off && title <nul 
    
    title .\%~nx0 && color 0a
    cd /d "%~dp0" && mode 50,10
    setlocal enabledelayedexpansion
    
    >con: echo/ & set /p "_myvar=Enter your Input:"
    
    set "_myvar_0=%_myvar: =" & set/a "_i+=1+0" & set "_myvar_!_i!=%"
    <con: ^< nul rem./ && for /l %%l in (0 1 !_i!)do echo=!_myvar_%%~l! 
    
    endlocal & goto=%:EOF
    

    您可以尝试使用数组,方法是在 dostips.com /by @Aacini上发布

    原始布局代码如下所示:

    set "_myvar=%_myvar: ="&set /a i+=1&set "_myvar[!i!]=%"

    我更喜欢替换[ ]一个简单的_

    另外,更改i+=为_i+=1+0,不需要预定义的set命令:set i=0

    set "_myvar_0=%_myvar: =" & set/a "_i+=1+0" & set "_myvar_!_i!=%"

    1.您可以使用由空格定义的分隔符访问所有事件。

    set "%_myvar: ="      ==>      "%_myvar:space="

    2.该方法使用一个计数器,其中限制是存储在 中的总数!_i!。

    set /a _i+=1+0

    3.使用for /L循环,您可以访问从0到极限的所有事件!_i!:

    for /l %%l in (0 1 !_i!)do...

    4.通过%%l将循环中的变量与分配给变量的名称连接起来_myvar_,就可以得到每次出现的值...

    for /l %%l in (0 1 !_i!)do echo=!_myvar_%%~l!

    在此处输入图像描述

    • 2
  2. Best Answer
    T3RR0R
    2020-10-12T04:07:02+08:002020-10-12T04:07:02+08:00

    数组定义的另一种宏方法:

    @Echo off
    rem /* Array macro definition occurs prior to delayed expansion being enabled */
     Set "DefUserArr=Set "#{i}=0"& Set /P "String=Enter #:"& For %%G in (!String!)Do (Set /a "#{i}=!#{i}!+1" & Set "#[!#{i}!]=%%G")"
    rem /* Enable delayed expansion in order to expand the macro */
     Setlocal EnableDelayedExpansion
    rem /* Supply the variable name to be used for the Array group using substring modification as the macro is expanded */
     %DefUserArr:#=Value%
    rem /* Access the Group elements using the reference variable */
     Set Value[
    rem /* Groups begin from a '1' index and group count is referenced via the Groupname{i} variable */
     Set Value{i}
     Endlocal
    

    示例输入输出:

    C:\Users\tcdou>inputA
    Enter Value:one two&<> three^!^!
    Value[1]=one
    Value[2]=two&<>
    Value[3]=three!!
    Value{i}=3
    
    

    当然,如果您的脚本是为您自己以外的用户编写的,您可能至少要测试是否输入了输入,并且在使用之前定义了“数组”。下面是一个稍微更健壮的版本,它演示了一个?ArrDef测试这个的宏:

    @Echo off & Setlocal DISABLEdelayedexpansion
    rem /* Array macro definition occurs prior to delayed expansion being enabled */
     Set "DefUserArr=Set "String="&For %%n in (1 2)Do if %%n==2 ((If /I Not "!Switch!" == "-A" (For /F "Tokens=1,2 Delims==" %%v in ('2^> Nul Set #[')Do Set "%%~v=" & Set "#{i}=0")) & Set /P "String=Enter #:"& For %%G in (!String!)Do (Set /a "#{i}=!#{i}!+1" & Set "#[!#{i}!]=%%~G"))Else Set Switch="
     Set "?ArrDef=For %%n in (1 2)Do if %%n==2 ( If "!#{i}!" == "" ( For %%G in (!Actions!)Do %%~G )Else If "!#{i}!" == "0" ( For %%G in (!Actions!)Do %%~G ))Else Set Actions="
    rem /* Enable delayed expansion in order to expand the macro */
     Setlocal EnableDelayedExpansion
    rem /* Supply the variable name to be used for the Array group using substring modification as the macro is expanded */
    :Input1
     %DefUserArr:#=Value%
    rem /* test if array defined */
     %?ArrDef:#=Value% "Echo/Inital input required" "Goto :Input1"
    rem /* Access the Group elements using the reference variable */
     Set Value[
    rem /* Groups begin from a '1' index and group count is referenced via the Groupname{i} variable */
     Set Value{i}
    rem /* append to the array using the -A switch */
     %DefUserArr:#=Value%-A
     Set Value[
     Set Value{i}
    rem /* Using the DefUserArr macro without the -A switch will remove any preexisting values for the supplied group name. */
    :Input3
     %DefUserArr:#=Value%
     %?ArrDef:#=Value% "Echo/New input required" "Goto :Input3"
     Set Value[
     Set Value{i}
     Endlocal & Endlocal
    
    • 2

相关问题

  • 如何在 macOS 的终端中切换切换(连续性)?

  • Windows 上是否有可以通过 CMD 或 Powershell 提供加密安全随机字节的设备或可执行文件?

  • Python 的“pass”参数的批处理等价物是什么?

  • 禁用后无法启用 Microsoft Print to PDF

  • Windows 8.1 powercfg.exe 电源设置命令行工具显示高 CPU 使用率

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何减少“vmmem”进程的消耗?

    • 11 个回答
  • Marko Smith

    从 Microsoft Stream 下载视频

    • 4 个回答
  • Marko Smith

    Google Chrome DevTools 无法解析 SourceMap:chrome-extension

    • 6 个回答
  • Marko Smith

    Windows 照片查看器因为内存不足而无法运行?

    • 5 个回答
  • Marko Smith

    支持结束后如何激活 WindowsXP?

    • 6 个回答
  • Marko Smith

    远程桌面间歇性冻结

    • 7 个回答
  • Marko Smith

    子网掩码 /32 是什么意思?

    • 6 个回答
  • Marko Smith

    鼠标指针在 Windows 中按下的箭头键上移动?

    • 1 个回答
  • Marko Smith

    VirtualBox 无法以 VERR_NEM_VM_CREATE_FAILED 启动

    • 8 个回答
  • Marko Smith

    应用程序不会出现在 MacBook 的摄像头和麦克风隐私设置中

    • 5 个回答
  • Martin Hope
    CiaranWelsh 如何减少“vmmem”进程的消耗? 2020-06-10 02:06:58 +0800 CST
  • Martin Hope
    Jim Windows 10 搜索未加载,显示空白窗口 2020-02-06 03:28:26 +0800 CST
  • Martin Hope
    v15 为什么通过电缆(同轴电缆)的千兆位/秒 Internet 连接不能像光纤一样提供对称速度? 2020-01-25 08:53:31 +0800 CST
  • Martin Hope
    fixer1234 “HTTPS Everywhere”仍然相关吗? 2019-10-27 18:06:25 +0800 CST
  • Martin Hope
    andre_ss6 远程桌面间歇性冻结 2019-09-11 12:56:40 +0800 CST
  • Martin Hope
    Riley Carney 为什么在 URL 后面加一个点会删除登录信息? 2019-08-06 10:59:24 +0800 CST
  • Martin Hope
    zdimension 鼠标指针在 Windows 中按下的箭头键上移动? 2019-08-04 06:39:57 +0800 CST
  • Martin Hope
    jonsca 我所有的 Firefox 附加组件突然被禁用了,我该如何重新启用它们? 2019-05-04 17:58:52 +0800 CST
  • Martin Hope
    MCK 是否可以使用文本创建二维码? 2019-04-02 06:32:14 +0800 CST
  • Martin Hope
    SoniEx2 更改 git init 默认分支名称 2019-04-01 06:16:56 +0800 CST

热门标签

windows-10 linux windows microsoft-excel networking ubuntu worksheet-function bash command-line hard-drive

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve