Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 从变量(CLI窗口)中删除括号_String_Batch File_Command Line Interface - Fatal编程技术网

String 从变量(CLI窗口)中删除括号

String 从变量(CLI窗口)中删除括号,string,batch-file,command-line-interface,String,Batch File,Command Line Interface,根据以下代码,Var的输出为(COM19)。我在想什么命令可以用来移除括号?尝试使用sed,但理想情况下需要一个不需要在Windows上额外安装的解决方案 @echo off setlocal wmic path win32_pnpentity get caption /format:list > output.txt for /f "tokens=4" %%a in ('find /i "USB Serial Port" output.txt') do set var=%%a pau

根据以下代码,Var的输出为(COM19)。我在想什么命令可以用来移除括号?尝试使用sed,但理想情况下需要一个不需要在Windows上额外安装的解决方案

@echo off
setlocal

wmic path win32_pnpentity get caption /format:list > output.txt
for /f "tokens=4" %%a in ('find /i "USB Serial Port" output.txt') do set var=%%a

pause

goto :EOF

根据代币功能,使用delims解决了问题。代码可以在下面看到

@echo off
setlocal

wmic path win32_pnpentity get caption /format:list > output.txt
for /f "tokens=2 delims=()" %%a in ('find /i "USB Serial Port" output.txt') do set var=%%a

pause

“尝试使用sed,但理想情况下需要一个不需要额外安装的解决方案”。这意味着您正在运行Windows。对吗?这可能有助于其他人决定您所拥有的工具。是的,很抱歉造成混淆。我使用的windows
::
是一个标签命令,名称为
,因为
指定了标签。因此,以
开头的行上不会运行任何命令。我知道这一点。有问题的代码在purposeoutput上被注释掉,Output不是一个变量,而是一个文本文件。如果将“%output%”替换为output.txt,上述代码是否仍然有效?是。您可以对(“%path+output.txt”)do@(echo%%a)中的/f“usebackq delims=()”%%a执行以下语法
我尝试将如下所示的命令添加到我的代码中,因为在(“%var%”do(echo%%a)
中为/f“delims=()”%%a执行
时,您必须使用
语法
不带
usebackq
'
usebackq
引用变量。您可以执行/?的
查看详细信息。您的
%var%
是一个变量,而不是一个文件。您的更新后的变量可以在('wmic path win32_pnpentity get caption/format:list^ | find/i“USB串行端口“')中的%%a('wmic path win32_pnpentity get caption/format:list^ | find/i“USB串行端口“')do set var=%a的一种语法中组合。因此无需创建output.txt。
@echo off
setlocal

wmic path win32_pnpentity get caption /format:list > output.txt
for /f "tokens=2 delims=()" %%a in ('find /i "USB Serial Port" output.txt') do set var=%%a

pause