我需要将程序的输出分配给使用MS批处理文件的变量。
所以在GNU Bash shell中,我将使用VAR=$(应用程序arg0 arg1)。我需要在Windows中使用批处理文件的类似行为。
比如set VAR=application arg0 arg1。
类似的问题
如何在批处理文件中将命令输出设置为变量 我如何得到一个命令的结果在一个变量在窗口? 在Windows批处理文件中使用命令的结果设置变量的值 将命令的输出设置为变量(使用管道) 将命令输出分配给批处理文件中的变量
我需要将程序的输出分配给使用MS批处理文件的变量。
所以在GNU Bash shell中,我将使用VAR=$(应用程序arg0 arg1)。我需要在Windows中使用批处理文件的类似行为。
比如set VAR=application arg0 arg1。
类似的问题
如何在批处理文件中将命令输出设置为变量 我如何得到一个命令的结果在一个变量在窗口? 在Windows批处理文件中使用命令的结果设置变量的值 将命令的输出设置为变量(使用管道) 将命令输出分配给批处理文件中的变量
当前回答
一些宏用于将命令的输出设置为变量/
在命令提示符中直接为
c:\>doskey assign=for /f "tokens=1,2 delims=," %a in ("$*") do @for /f "tokens=* delims=" %# in ('"%a"') do @set "%b=%#"
c:\>assign WHOAMI /LOGONID,my-id
c:\>echo %my-id%
带参数的宏
因为这个宏接受参数作为一个函数,我认为它是在批处理文件中使用的最整洁的宏:
@echo off
::::: ---- defining the assign macro ---- ::::::::
setlocal DisableDelayedExpansion
(set LF=^
%=EMPTY=%
)
set ^"\n=^^^%LF%%LF%^%LF%%LF%^^"
::set argv=Empty
set assign=for /L %%n in (1 1 2) do ( %\n%
if %%n==2 (%\n%
setlocal enableDelayedExpansion%\n%
for /F "tokens=1,2 delims=," %%A in ("!argv!") do (%\n%
for /f "tokens=* delims=" %%# in ('%%~A') do endlocal^&set "%%~B=%%#" %\n%
) %\n%
) %\n%
) ^& set argv=,
::::: -------- ::::::::
:::EXAMPLE
%assign% "WHOAMI /LOGONID",result
echo %result%
FOR /F宏
不像前面的宏那么容易读。
::::::::::::::::::::::::::::::::::::::::::::::::::
;;set "{{=for /f "tokens=* delims=" %%# in ('" &::
;;set "--=') do @set "" &::
;;set "}}==%%#"" &::
::::::::::::::::::::::::::::::::::::::::::::::::::
:: --examples
::assigning ver output to %win-ver% variable
%{{% ver %--%win-ver%}}%
echo 3: %win-ver%
::assigning hostname output to %my-host% variable
%{{% hostname %--%my-host%}}%
echo 4: %my-host%
宏使用临时文件
更容易阅读,它不是那么慢,如果你有一个SSD驱动器,但它仍然创建一个临时文件。
@echo off
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
;;set "[[=>"#" 2>&1&set/p "&set "]]==<# & del /q # >nul 2>&1" &::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
chcp %[[%code-page%]]%
echo ~~%code-page%~~
whoami %[[%its-me%]]%
echo ##%its-me%##
其他回答
/ /设置str = % myvar#: * = % / /这个替换所有“:” " " //findstr "子网掩码" my_log.txt > my_find.txt//在文件中搜索“字符串”并保存到新文件中 //“>”重制文件,如果使用“>>”添加在旧文件// im建议使用“>”
@echo off
ipconfig > my_log.txt
findstr "Subnet Mask" my_log.txt > my_find.txt
set /p myVar= < my_find.txt
echo %myVar%
set str=%myVar:*:=%
set str=%str:.= im_dot %
echo %str%
pause
作为前面答案的补充,管道可以在for语句中使用,由插入符号转义:
for /f "tokens=*" %%i in ('tasklist ^| grep "explorer"') do set VAR=%%i
一种方法是:
application arg0 arg1 > temp.txt
set /p VAR=<temp.txt
另一个原因是:
for /f %%i in ('application arg0 arg1') do set VAR=%%i
请注意,%%i中的第一个%用于转义后面的%,并且在批处理文件中而不是在命令行中使用上述代码时需要它。想象一下,你的test.bat有这样的东西:
for /f %%i in ('c:\cygwin64\bin\date.exe +"%%Y%%m%%d%%H%%M%%S"') do set datetime=%%i
echo %datetime%
我写了脚本,每5秒ping google.com,并记录当前时间的结果。在这里你可以找到变量“commandLineStr”的输出(带索引)
@echo off
:LOOPSTART
echo %DATE:~0% %TIME:~0,8% >> Pingtest.log
SETLOCAL ENABLEDELAYEDEXPANSION
SET scriptCount=1
FOR /F "tokens=* USEBACKQ" %%F IN (`ping google.com -n 1`) DO (
SET commandLineStr!scriptCount!=%%F
SET /a scriptCount=!scriptCount!+1
)
@ECHO %commandLineStr1% >> PingTest.log
@ECHO %commandLineStr2% >> PingTest.log
ENDLOCAL
timeout 5 > nul
GOTO LOOPSTART
在命令行中执行以下命令时:
for /f %%i in ('application arg0 arg1') do set VAR=%%i
我得到了错误:
%%i was unexpected at this time.
为了解决这个问题,我改为使用一个%符号,就像这样:
for /f %i in ('application arg0 arg1') do set VAR=%i
简介:
在批处理文件中使用%% 在批处理文件之外使用%(在命令行上)