如何使用批处理编程访问子字符串?另外,如何使用批处理操作字符串以将其用作整数值?
How to access a substring using Batch programming? Also, how to manipulate a string to use it as integer value using batch programming?
我输入后
netsh wlan show interfaces
netsh wlan show interfaces > interface.txt
在我系统的命令提示符中,我得到了当前的连接配置文件。我有兴趣从 interface.txt
文件中找出 ':
' 部分之后的 'Profile
' 部分的字符串部分。
我希望程序使用 findstr
搜索 'Profile',然后在同一行中提取字符串部分,以便可以重复使用生成的代码。我该怎么做?
此外,如何使用 text
文件中的 string
数据将其作为 integer
数据进行处理?
示例输出:
for /f "tokens=1,* delims=:" %%i in ('netsh wlan show interfaces^|find " Profile"') do set p=%%j
echo %p: =%
for /f "tokens=1,* delims=:" %%i in ('netsh wlan show interfaces^|find " Receive rate"') do set r=%%j
echo %r:~1%
EDIT npocmaka 的评论提醒我,某些值(例如 "Physical Address")包含冒号。编辑代码以完成这些值。
注意:值有一个起始 space;通过在分隔符中添加 space 可以避免这种情况,但另一方面,某些描述有多个单词 ("Network Type"),因此这是有问题的。所以我决定让他们进入 "postprocess" 变量。
您可以使用 FOR
和 :
作为分隔符简单地拆分您的字符串。说你的刺痛就像
SET yourstring = something: somethingelse even more stuff and you want to access somethingelse
那么你可以使用这个:
REM this is to get everithing on the right of :
FOR /F "tokens=2 delims=:" %%X IN ("%yourstring%") DO SET substring=%%X
REM this is to get the first token after the first white space
FOR /F "tokens=1 delims= " %%X IN ("%substring%") DO SET yourvalue=%%X
现在 %yourvalue% 将包含 somethingelse.
回答你的第二个问题:
如果您希望将表示数字的字符串视为整数,则必须使用 SET /A varname=%stringWithTheInteger%
。 /A
允许您对变量进行算术运算。
我输入后
netsh wlan show interfaces
netsh wlan show interfaces > interface.txt
在我系统的命令提示符中,我得到了当前的连接配置文件。我有兴趣从 interface.txt
文件中找出 ':
' 部分之后的 'Profile
' 部分的字符串部分。
我希望程序使用 findstr
搜索 'Profile',然后在同一行中提取字符串部分,以便可以重复使用生成的代码。我该怎么做?
此外,如何使用 text
文件中的 string
数据将其作为 integer
数据进行处理?
示例输出:
for /f "tokens=1,* delims=:" %%i in ('netsh wlan show interfaces^|find " Profile"') do set p=%%j
echo %p: =%
for /f "tokens=1,* delims=:" %%i in ('netsh wlan show interfaces^|find " Receive rate"') do set r=%%j
echo %r:~1%
EDIT npocmaka 的评论提醒我,某些值(例如 "Physical Address")包含冒号。编辑代码以完成这些值。
注意:值有一个起始 space;通过在分隔符中添加 space 可以避免这种情况,但另一方面,某些描述有多个单词 ("Network Type"),因此这是有问题的。所以我决定让他们进入 "postprocess" 变量。
您可以使用 FOR
和 :
作为分隔符简单地拆分您的字符串。说你的刺痛就像
SET yourstring = something: somethingelse even more stuff and you want to access somethingelse
那么你可以使用这个:
REM this is to get everithing on the right of :
FOR /F "tokens=2 delims=:" %%X IN ("%yourstring%") DO SET substring=%%X
REM this is to get the first token after the first white space
FOR /F "tokens=1 delims= " %%X IN ("%substring%") DO SET yourvalue=%%X
现在 %yourvalue% 将包含 somethingelse.
回答你的第二个问题:
如果您希望将表示数字的字符串视为整数,则必须使用 SET /A varname=%stringWithTheInteger%
。 /A
允许您对变量进行算术运算。