Extracting part of string from right to left

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
denkler
Posts: 2
Joined: 29 Mar 2012 03:19

Extracting part of string from right to left

#1 Post by denkler » 29 Mar 2012 03:41

Hi everybody,

I´m facing following little issue - google couldn´t solve it this time... :)

I have a string like this:

Code: Select all

SET var1=\folder1\...\folderN\[ApplicationName]\[Environment]\


Now I need to extract the string [ApplicationName] from this variable and set it into var2.

I am able to extract [Environment] by cutting this string from left to right with this code:

Code: Select all

rem ================================================== =========================
rem Pfad soweit zerlegen bis nur noch die Environment vorhanden ist
rem ================================================== =========================

:anfang
rem ================================================== =====
rem %TeilPfad% wird von links nach rechts bis zum nächsten "\" beschnitten
rem ================================================== =====
SET TeilPfad=%TeilPfad:*\=%
echo %TeilPfad%

rem ================================================== =====
rem Wenn TeilPfad keinen "\" mehr enthält stop - ansonsten weiter beschneiden
rem ================================================== =====
IF not "%TeilPfad%"=="%TeilPfad:*\=%" GOTO :anfang

SET Environment=%TeilPfad%


To extract [ApplicationName] I would need the function

Code: Select all

SET TeilPfad=%TeilPfad:*\=%

to work the other way around, from right to left... something like this (doesn´t work!!):

Code: Select all

SET TeilPfad=%TeilPfad:\*=%

as I know that there is a specific number of \ behind [ApplicationName].


Some ideas?

Cheers

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

Re: Extracting part of string from right to left

#2 Post by foxidrive » 29 Mar 2012 03:57

Any good?

Code: Select all

@echo off
SET var1=\folder1\folderN\[ApplicationName]\[Environment]\
for /f "delims=" %%a in ("%var1%.") do echo set varA=%%~nxa
for /f "delims=" %%a in ("%var1%..") do echo set varB=%%~nxa
pause


denkler
Posts: 2
Joined: 29 Mar 2012 03:19

Re: Extracting part of string from right to left

#3 Post by denkler » 29 Mar 2012 04:05

Awesome, way better than my approach!

Thank you very much, works perfect!

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Extracting part of string from right to left

#4 Post by dbenham » 29 Mar 2012 08:47

Agreed - a very good and clever solution. :)

I have one improvement: As written, it will not give the correct answer if var1 is not terminated with \. File/folder names cannot end with dot or space, so %%~nxa will trim the trailing dots such that both varA and varB will be set to [Environment].

Windows treats consecutive \\ the same as \. So the following will work regardless whether var1 is terminated by \ or not.

Code: Select all

@echo off
SET var1=\folder1\folderN\[ApplicationName]\[Environment]
for /f "delims=" %%a in ("%var1%\.") do echo set varA=%%~nxa
for /f "delims=" %%a in ("%var1%\..") do echo set varB=%%~nxa

Also, the above can be simplified (optimized?) to use a simple FOR instead of FOR /F

Code: Select all

@echo off
SET var1=\folder1\folderN\[ApplicationName]\[Environment]\
for %%a in ("%var1%\.") do echo set varA=%%~nxa
for %%a in ("%var1%\..") do echo set varB=%%~nxa


Dave Benham

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

Re: Extracting part of string from right to left

#5 Post by foxidrive » 29 Mar 2012 08:57

dbenham wrote:Also, the above can be simplified (optimized?) to use a simple FOR instead of FOR /F


Good thinking.

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

Re: Extracting part of string from right to left

#6 Post by Aacini » 29 Mar 2012 16:38

Code: Select all

@echo off
setlocal EnableDelayedExpansion
SET var1=\folder1\...\folderN\[ApplicationName]\[Environment]\
for %%a in (%var1:\= %) do (
   set var2=!lastButOne!
   set lastButOne=%%a
)

This will work regardless whether var1 is terminated by \ or not.

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

Re: Extracting part of string from right to left

#7 Post by foxidrive » 29 Mar 2012 16:45

Will it have issues with spaces in the folder names?

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

Re: Extracting part of string from right to left

#8 Post by Aacini » 29 Mar 2012 16:59

@foxidrive: Yes, but this is easily solved:

Code: Select all

@echo off
setlocal EnableDelayedExpansion
SET var1=\folder1\...\folderN\[Application Name]\[Environment]\
set var1=%var1: =/%
for %%a in (%var1:\= %) do (
   set var2=!lastButOne:/= !
   set lastButOne=%%a
)

This method may be easily generalized to get the Nth word from right to left.

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

Re: Extracting part of string from right to left

#9 Post by foxidrive » 29 Mar 2012 17:13

ok. Now it will have issues with & characters. :)

I do like your solution with replacing spaces with / and that could be handy in a number of ways.

Daves bottom solution works with or without a trailing \ too btw.

Post Reply