問題描述
從變量中刪除括號(CLI‑Windows) (Remove Brackets from Variable (CLI‑Windows))
根據下面的代碼,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
參考解法
方法 1:
for /f "delims=()" %%a in ("%output%") do (echo %%a)
方法 2:
Managed to solve the issue ussing the delims, in line with the token function. Code can be seen below.
@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