简体   繁体   English

批处理脚本 - 将字符串替换为从变量接收到的另一个字符串

[英]Batch script - replace string for another string from received from variable

I have this batch script which has the objective of retrieving a URL from an input, save it to a variable.我有这个批处理脚本,其目的是从输入中检索 URL,并将其保存到变量中。 Then this URL should have a part of it, a 3-4 letters string replaced by another string which is inside a list in a file (lista.txt).然后这个 URL 应该有它的一部分,一个 3-4 个字母的字符串被另一个字符串替换,该字符串位于文件 (lista.txt) 的列表中。 Then, the script should open the Chrome browser and one tab for each new URL generated by the string replacement.然后,脚本应该为每个由字符串替换生成的新 URL 打开 Chrome 浏览器和一个选项卡。 I understand there are several other ways to do that, but I intend to keep using the batch file and check where my mistake is.我知道还有其他几种方法可以做到这一点,但我打算继续使用批处理文件并检查我的错误在哪里。 The script seem to be working until I get to the point where the string replacement by another string coming from a variable.该脚本似乎正在工作,直到我到达将字符串替换为来自变量的另一个字符串的地步。 This is the script:这是脚本:

@echo off
set BROWSER=chrome.exe
set /p URL=Type the URL:
echo.
for /f %%i in (Lista.txt) do (
    set URL=%%URL:%IBOV%=%i%%
    echo %URL%
    START %BROWSER% -new-tab "%URL%"
)
Pause

contents of lista.txt: lista.txt 的内容:

IBOV
GNDI3
USIM5
OIBR3
MEAL3
ETER3
COGN3
TASA4
BBDC4
ITUB4
SUZB3
VALE3
PETR4
RAIL3

Looks like classical delayed expansion problem.看起来像经典的延迟扩展问题。 Though I don't have preset the IBOV variable that you are using ,neither the content of Lista.txt I would suggest that this might help you:虽然我没有预设您正在使用的 IBOV 变量,但无论是 Lista.txt 的内容,我都建议这可能对您有所帮助:

@echo off
setlocal enableDelayedExpansion
set BROWSER=chrome.exe
set /p URL=Type the URL:
echo.
for /f %%i in (Lista.txt) do (
    set URL=!URL:IBOV=%%i!
    echo !URL!
    START %BROWSER% -new-tab "!URL!"
)
Pause

Also you are accessing the %%i token in a wrong way.此外,您以错误的方式访问%%i令牌。

Is there really an IBOV variable set before the script starts?在脚本启动之前是否真的设置了IBOV变量? You might need splitting or substring substitution.您可能需要拆分或子字符串替换。

CALL set "URL=%%URL:%IBOV%=%%i%%"
CALL    echo %%URL%%
CALL START "" %BROWSER% -new-tab "%%URL%%"

Please search SO for "delayed expansion" for an explanation.请搜索“延迟扩展”以获取解释。 The "problem" has been described hundreds of times.这个“问题”已经被描述了数百次。 The syntax shown should cure the echo .显示的语法应该可以解决echo Not so sure about the start but reading up on delayed expansion should provide pointers.start不太确定,但阅读delayed expansion应该提供指导。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM