如何将命令值批量分配给变量 [英] How to assign command value to variable in batch

查看:39
本文介绍了如何将命令值批量分配给变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想执行一个命令并将它的值赋给批处理文件中的一个变量.

I want to execute one command and assign it's value to a variable within a batch file.

我们知道在 Windows 命令提示符下运行 hostname 命令会给出 PC 名称.我想使用主机名命令并将其值分配给批处理文件中的变量.

We know that running hostname command on a windows command prompt gives the PC name. I want to use hostname command and assign it's value to a variable within a batch file.

在谷歌上搜索之后,我尝试使用以下方法,这些似乎都不起作用:

After googling about it, I've tried using below methods, none of these seem to work:

set CONTROLLER=hostname
set CONTROLLER=%hostname%
set CONTROLLER=%%hostname%%
set CONTROLLER=!hostname!

请多多指教.

推荐答案

我们可以通过下面的命令轻松获取主机名/计算机名

We can easily get the hostname/computer name through below command

set host=%COMPUTERNAME%
echo %host%

这篇关于如何将命令值批量分配给变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆