gpt4 book ai didi

regex - 从批处理脚本中的字符串中提取数字

转载 作者:可可西里 更新时间:2023-11-01 09:47:38 26 4
gpt4 key购买 nike

我一直在努力编写一个脚本,该脚本将从驱动器的其他属性中找到驱动器索引号。脚本如下:

@echo off
REM batch file to load Veracrypt
Set "driveIndex="
for /f "skip=1 tokens=1 delims= " %%a in ('wmic diskdrive where "model ='WD Elements 1078 USB Device'" get index') do SET driveIndex=%%a & goto reportLetter

:reportLetter
if not defined driveIndex (
echo Error Occured!
pause
exit
) else (
echo \Device\Harddisk%driveIndex:~0%\Partition3
pause
exit
)

但是,脚本的输出是 \Device\Harddisk1\Partition3。我尝试了很长时间,但可以让脚本提供以下输出:\Device\Harddisk1\Partition3

谁能告诉我如何更正代码以获得所需的输出?

最佳答案

试试这个

DO SET "driveIndex=%%a"

你的线路

... do set driveIndex=%%a & goto ...

被解释为 set driveIndex=%%a<space>& goto ... ,这是 \Device\Harddisk1 \Partition3 中的附加空间所在的位置来自。

当然你可以这样写:

... do set driveIndex=%%a& goto ... 

但更好的语法是:

... do set "driveIndex=%%a" & goto ...

这消除了任何不需要的空格。

注1:set对空间非常挑剔。 set var = hello创建一个名为 var<space> 的变量值为 <space>hello<space> .

注2:

set var="value"将值设置为 "value"

set "var=value"将值设置为 value .此语法使您可以完全(和可见)控制变量名称及其值。

关于regex - 从批处理脚本中的字符串中提取数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30157642/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com