Return the piece of string

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
eapmartins
Posts: 2
Joined: 16 Nov 2012 14:36

Return the piece of string

#1 Post by eapmartins » 16 Nov 2012 15:04

Hi,

I need to get some words of variable. For example:

<systemProperties xmi:id="Property" name="JAZZ_HOME" value="file:///E:/IBM/CLM/QM3_0_1_3/server/conf" required="false"/>

The value that I need to get is E:/IBM/CLM/QM3_0_1_3/server/conf, So I trying to find the position of E and the position of conf.

Code: Select all

@echo off & setlocal enableextensions enabledelayedexpansion
set a_="E:/MyDocuments/Folder/conf" required
set b_=required
set instr=-1

for /l %%i in (0,1,255) do (
set str=!a_:~%%i!
if defined str (
    echo !str!|findstr /b /l "%b_%">nul
    if !errorlevel! EQU 0 if !instr! EQU -1 set instr=%%i
    )
)
echo The first position of "%b_%" in "%a_%" is %instr%

set str="E:/IBM/CLM/QM3_0_1_3/server/conf" required
set str=%str:~0,5%
echo.%str%

endlocal & goto :EOF


Here I can get the 5 characters after position 0, I'd like to do something like that. So I can get the values until the required word.

Code: Select all

set str=E:/IBM/CLM/QM3_0_1_3/server/conf
set str=%str:~0,%instr%%
echo.%str%

aGerman
Expert
Posts: 4743
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Return the piece of string

#2 Post by aGerman » 16 Nov 2012 19:15

Since the file URI belongs to the value property your key word should also be "value" instead of "required". All you have to reach is getting the next token (substring after the equal sign).

Code: Select all

@echo off & setlocal EnableExtensions DisableDelayedExpansion

set "a_=<systemProperties xmi:id="Property" name="JAZZ_HOME" value="file:///E:/IBM/CLM/QM3_0_1_3/server/conf" required="false"/>"

setlocal EnableDelayedExpansion
for %%i in (!a_!) do (
  endlocal
  if errorlevel 1 (if not defined str set "str=%%~i") else set "substr=%%~i"
  setlocal EnableDelayedExpansion
  if "!substr!"=="value" cmd /c exit /b 1
)

echo !str:~8!
pause


regards
aGerman

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Return the piece of string

#3 Post by Aacini » 16 Nov 2012 19:54

Another flavor of the same recipe... :mrgreen:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

set "a_=<systemProperties xmi:id="Property" name="JAZZ_HOME" value="file:///E:/IBM/CLM/QM3_0_1_3/server/conf" required="false"/>"

for %%a in (!a_!) do (
   if "!lastToken!" neq "value" (
      set "lastToken=%%a"
   ) else (
      set "value=%%~a" & goto continue
   )
)
:continue
echo !value:~8!


Antonio

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Return the piece of string

#4 Post by foxidrive » 16 Nov 2012 21:09

More of the same:

Code: Select all

@echo off
set "a=<systemProperties xmi:id="Property" name="JAZZ_HOME" value="file:///E:/IBM/CLM/QM3_0_1_3/server/conf" required="false"/>"

for /f "tokens=1,* delims=/" %%a in ("%a%") do (
for /f ^delims^=^" %%c in ("%%b") do echo "%%c"
)
pause

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Return the piece of string

#5 Post by Aacini » 17 Nov 2012 11:12

A shorter method... :D

Code: Select all

@echo off
set "a_=<systemProperties xmi:id="Property" name="JAZZ_HOME" value="file:///E:/IBM/CLM/QM3_0_1_3/server/conf" required="false"/>"
for /F ^tokens^=9^ delims^=:^" %%a in ("%a_%") do echo E:%%a
... and a larger one! :wink:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

set "a_=<systemProperties xmi:id="Property" name="JAZZ_HOME" value="file:///E:/IBM/CLM/QM3_0_1_3/server/conf" required="false"/>"

set numTokens=-1
for %%a in (!a_:"/=" !) do (
   if !numTokens! lss 0 (
      set numTokens=0
   ) else if not defined lastToken (
      set "lastToken=%%a"
   ) else (
      set "!lastToken!=%%~a"
      set /A numTokens+=1
      set token[!numTokens!]=!lastToken!
      set lastToken=
   )
)

REM Show all tokens
for /L %%i in (1,1,%numTokens%) do (
   for %%t in (!token[%%i]!) do echo %%t=!%%t!
)
echo/

echo !value:~8!

eapmartins
Posts: 2
Joined: 16 Nov 2012 14:36

Re: Return the piece of string

#6 Post by eapmartins » 17 Nov 2012 11:40

Hi All, thank you very much. All scripts is working as I need, now I need to understand better about the commands. :D

Post Reply