更改bat文件并运行

问题描述 投票:0回答:1

我想更改bat文件中的变量。

Bat 文件要求我插入用户名,例如

abcdef555
,然后用它替换文本文件末尾的
wwwww
并将修改后的行作为命令运行。

之前:

xmrig.exe -o xmr.2miners.com:2222 -u 44eEFY2Jx5R4yqNuffejZXQXbGgw1QjhwcwCiXYdh4aBhyc9ofYgzan442bPXRF6EgEydzMmrYJg3bJh7XEHMtnuPdqJbxm.wwwww -p x

之后:

xmrig.exe -o xmr.2miners.com:2222 -u 44eEFY2Jx5R4yqNuffejZXQXbGgw1QjhwcwCiXYdh4aBhyc9ofYgzan442bPXRF6EgEydzMmrYJg3bJh7XEHMtnuPdqJbxm.abcdef555 -p x
batch-file variable-assignment
1个回答
0
投票

批处理文件可以扩展为:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem Assign the first argument string passed to the batch file without
rem surrounding double quotes to the environment variable NameUser or
rem undefine the environment variable NameUser on batch file is run
rem without an argument string or with an empty argument string on
rem being currently defined at all.
set "NameUser=%~1"

rem Prompt the user of the batch file for the name if the batch file was
rem executed without passing the name as argument string to the batch file.
rem The prompt is repeated until the user of the batch file until the user
rem does not just press RETURN or ENTER without input of any string before.
:NamePrompt
if not defined NameUser set /P "NameUser=Enter user name: " || goto NamePrompt

rem Remove all double quotes from the user input string for
rem safe processing it further on the next command lines.
set "NameUser=%NameUser:"=%"

rem If the user input by mistake just double quotes, prompt the user again.
if not defined NameUser goto NamePrompt

rem Run the program with the name passed to the batch file as
rem first argument or entered by the user of the batch file.
xmrig.exe -o xmr.2miners.com:2222 -u "44eEFY2Jx5R4yqNuffejZXQXbGgw1QjhwcwCiXYdh4aBhyc9ofYgzan442bPXRF6EgEydzMmrYJg3bJh7XEHMtnuPdqJbxm.%NameUser%" -p x 
endlocal

以命令

rem
开头的行解释了命令行。这些行以及所有空行都可以删除,以达到批处理文件中的最少行数。

我建议阅读:如何阻止Windows命令解释器在错误的用户输入时退出批处理文件执行?这个答案详细解释了如何以故障安全和安全的方式提示用户使用

set /P
输入字符串也像这里所做的那样。

另请参阅:为什么在命令行上使用“set var = text”后没有带有“echo %var%”的字符串输出?引用的答案详细解释了为什么应在批处理中首选使用语法

set "variable=value"
文件来(重新)定义具有(新)值的环境变量。

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

  • call /?
    ...解释
    %~1
  • cmd /?
  • echo /?
  • endlocal /?
  • goto /?
  • if /?
  • rem /?
  • set /?
  • setlocal /?
© www.soinside.com 2019 - 2024. All rights reserved.