A batch program to extract the substring

Microsoft Xbox 360 premium system
December 6, 2009 at 20:13:09
Specs: Windows XP
I am looking for a batch script to extract the substring from the string literals. The script should extract substring before the last '\' found in the string literals.Ex. When the user inputs c:\abc\test\test1 , the script should extract c:\abc\ in one variable and test1 in another variable.

See More: A batch program to extract the substring

Report •


#1
December 7, 2009 at 00:34:32
Perhaps:

<untested>

@ECHO OFF
set var=c:\abc\test\test1
for /f %%a in ("%var:"=%") do (
    set Endvar=%%~na
    set half=%%~dpa
)
if "%half:~-1%"=="\" set half="%half:~0,-1%"
for /f %%b in ("%half:"=%") do (
    set startvar=%%~dpb
)
set half=
echo %startvar%
echo %endvar%
pause


Batch Variable how to


Report •

#2
December 7, 2009 at 01:33:37
hi ,
in reply to response. The code is working fine and my problem is solved. Thankyou

Report •

#3
December 15, 2009 at 22:08:07
When ever the directory name contains space i.e instead of var=c:\abc\test\test1 if i use var=c:\program files\abc\test1 then now the above code do not display the desired output i.e c:\program files and other variable as test1. I mean when ever the directory name contains space there is a problem. I need to ignore the space during the computation . But currently, space is considered in computation. Please help me with the changes.

Report •

Related Solutions

#4
December 15, 2009 at 22:43:26
What exactly do you want? Do you just want the space removed without warning?


Batch Variable how to


Report •

#5
December 16, 2009 at 00:38:39
Suppose var=c:\program files\abc\test1 , it should extract as variable1 = c:\program files
variable2= test1

Report •

#6
December 16, 2009 at 01:12:27
well, my experience with lfn is *always* put quotes around path+filename. I've had my ass kicked too many times by microsoft's contribution to chaos of letting spaces into the pathstream. thanks Ms! (no use complaining, it is written..)
"USE THE QUOTES LUKE!"

Report •

#7
December 16, 2009 at 01:24:44
Hi nbrane,

The script above does take into account quotes(except for the "set"). Strangely enough the space does kill it.... Well, we both know there's a better way anyhow.

Here's another way of doing it(thanks to Razor2.3)....

@ECHO OFF
set var="c:\program files\abc\test1"
for /f "delims=" %%a in (%var%) do (
    set endvar=%%~na
    for %%b in ("%%~a\..") do set startvar=%%~dpb
)
echo %startvar%
echo %endvar%
pause


Batch Variable how to


Report •


Ask Question