string that is related with delims=, i need right strings

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
nnnmmm
Posts: 141
Joined: 26 Aug 2017 06:11

string that is related with delims=, i need right strings

#1 Post by nnnmmm » 26 Aug 2017 08:57

Code: Select all

LET AA$="12 345/67890"
CALL www(AA$,"/",BB$)
PRINT BB$
-----------------------
BB$ becomes 67890


how can i write a script like this?
would you point me to a site?
length of AA$ is totally arbitray, please do not make any %x:~7,6% kind of thing

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: string that is related with delims=, i need right strings

#2 Post by Compo » 26 Aug 2017 09:20

Possibly:

Code: Select all

@Echo Off

Set "AA$=12 345/67890"
Set "BB$=%AA$:*/=%
Echo %BB$%

Timeout -1

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

Re: string that is related with delims=, i need right strings

#3 Post by aGerman » 26 Aug 2017 09:21

Seems you want to get the 2nd token delimited by a slash.

Code: Select all

@echo off
set "aa=12 345/67890"
for /f "tokens=2 delims=/" %%i in ("%aa%") do set "bb=%%i"
echo %bb%
pause


Using a function-like syntax:

Code: Select all

@echo off
set "aa=12 345/67890"
call :www aa "/" bb
echo %bb%
pause
exit /b

:www
setlocal EnableDelayedExpansion
for /f "tokens=2 delims=%~2" %%i in ("!%~1!") do (
  endlocal
  set "%~3=%%i"
  exit /b
)


Steffen

nnnmmm
Posts: 141
Joined: 26 Aug 2017 06:11

Re: string that is related with delims=, i need right strings

#4 Post by nnnmmm » 26 Aug 2017 12:23

Code: Select all

LET AA$="1/2 3/4/5/67890"
CALL www(AA$,"/",BB$)
PRINT BB$
-----------------------
BB$ becomes 67890


my sincere apology, it was supposed to be read from the right side of the string... i think it is related to LPOSR in basic.

Code: Select all

c:\abc def\12\mdd\abc.exe

i need abc.exe to be read from a full pathname string.

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

Re: string that is related with delims=, i need right strings

#5 Post by aGerman » 26 Aug 2017 13:20

You want to extract the filename out of a path?

Code: Select all

for %%i in ("c:\abc def\12\mdd\abc.exe") do set "fname=%%~nxi"

Steffen

nnnmmm
Posts: 141
Joined: 26 Aug 2017 06:11

Re: string that is related with delims=, i need right strings

#6 Post by nnnmmm » 26 Aug 2017 23:38

>You want to extract the filename out of a path?
no, full path names + options and things are already there for executions, but the paths are too long to display with echo, and directory names dont tell much about anything in general.

only file names and their options are good enough to tell their properties without editing each title manually as to what they are.

and a whole menu is not about files, it is mixed with many different things.

so i hope to know the exact extraction of a string in this case, from a distinct maker to the end.

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

Re: string that is related with delims=, i need right strings

#7 Post by aGerman » 27 Aug 2017 05:03

There is no "official" way in batch to extract a token from the end of a string. If the number of tokens that are delimited by a certain character is always the same you can use a FOR /F loop. If that isn't the case it's getting complicated. Batch is very limited so we try to use any undocumented side effect and undefined behaviour. No idea where to sort in the following technique that is based on the string replacement syntax but uses command concatenation inside.

Code: Select all

@echo off
set "aa=1/2 3/4/5/67890"

set "bb=%aa:/=" & set "bb=%"

echo %bb%
pause

Steffen

nnnmmm
Posts: 141
Joined: 26 Aug 2017 06:11

Re: string that is related with delims=, i need right strings

#8 Post by nnnmmm » 29 Aug 2017 02:49

write again

nnnmmm
Posts: 141
Joined: 26 Aug 2017 06:11

Re: string that is related with delims=, i need right strings

#9 Post by nnnmmm » 29 Aug 2017 03:25

Code: Select all

@echo off
set aa=1/2 3/4/5/67890
set bb=%aa:/= & set bb=%
echo %bb%
it worked well.

@echo off
set mark=/

set aa=1/2 3/4/5/67890
set bb=%aa:%mark%= & set bb=%
echo %bb%

how can i move mark out of it ?

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

Re: string that is related with delims=, i need right strings

#10 Post by Aacini » 29 Aug 2017 07:41

Code: Select all

@echo off
setlocal EnableDelayedExpansion

set "aa=1/2 3/4/5/67890"
set "mark=/"

for %%b in ("!aa:%mark%=" "!") do set "bb=%%~b"

echo %bb%

Antonio

nnnmmm
Posts: 141
Joined: 26 Aug 2017 06:11

Re: string that is related with delims=, i need right strings

#11 Post by nnnmmm » 05 Sep 2017 01:48

Code: Select all

@ECHO OFF
SETLOCAL
SET IN1=1/2 3/4/5S D6  7890
SET IN1=1234567890
SET IN2=5

CALL "LIB-STRING1.BAT" GET_STR_PIVOT_TO_END1 IN1 IN2 ANS
ECHO %IN1% %ANS%
PAUSE

i am stuck here, why am i not getting the answer "67890"


Code: Select all

LIB-STRING1.BAT has
CALL :%1 %*
EXIT /B

:GET_STR_PIVOT_TO_END1
(@ECHO OFF
 Setlocal EnableDelayedExpansion
   SET "INP=!%~2!"
   SET "PIVOT=!%~3!"
   FOR %%U IN ("!INP:PIVOT=" "!") DO SET "ANS=%%~U"
)
(Endlocal
   SET %~4=%ANS%
)
EXIT /B

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: string that is related with delims=, i need right strings

#12 Post by Squashman » 05 Sep 2017 12:20

You are missing percent symbols around the variable PIVOT and there is no need to put your function on a parenthesized block.

nnnmmm
Posts: 141
Joined: 26 Aug 2017 06:11

Re: string that is related with delims=, i need right strings

#13 Post by nnnmmm » 05 Sep 2017 17:59

Code: Select all

%PIVOT% in FOR %%U IN ("!INP:%PIVOT%=" "!") DO SET "ANS=%%~U" 

interferes with Setlocal EnableDelayedExpansion

Post Reply