如何在Windows批处理脚本中对动态变量值进行子字符串化 [英] How do I substring a dynamic variable value in windows batch scripting

查看:315
本文介绍了如何在Windows批处理脚本中对动态变量值进行子字符串化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法在Windows批处理脚本中的forloop内部对动态变量进行子字符串化.

Not able to substring the dynamic variable inside forloop in Windows batch script.

我的git hub中具有以下格式的属性文件. "collectionName = TestCollectionRun.json = test"

I have the properties file in my git hub in the below format. "collectionName=TestCollectionRun.json=test"

所以我写了下面的代码来获取这个值,但是要求是我需要从集合名称中删除'.json'部分,使用下面的代码我无法设置/回显该值.您能帮上忙吗!

So I have written the below code to fetch this values.But the requirement is that I need to strip of the '.json' part from collection name.With the below code I am not able to set/echo that value.Can you please help on this!

@ECHO ON
:BEGIN

IF EXIST "test.properties" ECHO Found properties file, reading file..
SET props=test.properties
setlocal EnableDelayedExpansion

For /F "delims== tokens=1,2,3" %%G in (%props%) Do (
if "%%I" EQU "test" if "%%G" EQU "collectionName" SET collName=%%H(
SET finalCollName=%collName%:~0,-5
ECHO %finalCollName%
)
)
:END

我们需要ECHO返回"TestCollectionRun".当前它不返回任何内容.

We need the ECHO to return "TestCollectionRun".currently its not returning anything.

推荐答案

给出collectionName=TestCollectionRun.json=test的行内容,以下是我认为您打算做的事情的快速重写:

Given a line content of collectionName=TestCollectionRun.json=test, here's a quick rewrite of what I think you're tring to do:

@Echo Off
Set "props=test.properties"
If Not Exist "%props%" (
    Echo Properties file not found!
    Echo Exiting..
    Timeout /T 3 /NoBreak >NUL
    Exit /B
)
Echo Found properties file, reading file..
For /F "UseBackQ Tokens=1-3 Delims==" %%A In ("%props%") Do (
    If /I "%%C" == "test" If /I "%%A" == "collectionName" Echo %%~nB
)
Pause

如果您想对循环中的集合名称进行操作,则可能需要使用延迟扩展:

If you wanted to do something with the collection name within the loop then you would probably need to use delayed expansion:

@Echo Off
SetLocal DisableDelayedExpansion
Set "props=test.properties"
If Not Exist "%props%" (
    Echo Properties file not found!
    Echo Exiting..
    Timeout /T 3 /NoBreak >NUL
    Exit /B
)
Echo Found properties file, reading file..
For /F "UseBackQ Tokens=1-3 Delims==" %%A In ("%props%") Do (
    If /I "%%C" == "test" If /I "%%A" == "collectionName" (
        Set "collName=%%B"
        SetLocalEnableDelayedExpansion
        Echo !collName!
        Rem Perform substring task on the variable named collName
        Set "finalCollName=!collName%:~0,-5!"
        Echo !finalCollName!
        EndLocal
    )
)
Pause

请注意,如果您的字符串用双引号引起来(例如,在您的问题正文中),或者如果行内容不同(例如,以空格或制表符开头),这些答案将无效.

查看注释中的事实之后"问题,很明显,您根本不需要对变量进行子字符串处理,因此应使用发布的第一种方法:

Looking at your 'after the fact' question in the comments, it is clear that you do not need to substring the variable at all, so should use the first method posted:

Echo Found properties file, reading file..
For /F "UseBackQ Tokens=1-3 Delims==" %%A In ("%props%") Do (
    If /I "%%C" == "test" If /I "%%A" == "collectionName" (
        newman run "%%B" -e "%envName%" --insecure --reporters cli,htmlextra --reporter-htmlextra-export "newman\%BUILD_NUMBER%\%%~nB.html" --disable-unicode
    )
)
Pause

这假定%envName%%BUILD_NUMBER%之前都已正确定义.

This assumes that both %envName% and %BUILD_NUMBER% have been previously defined correctly.

这篇关于如何在Windows批处理脚本中对动态变量值进行子字符串化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆