How to get substring out with window batch?

Have the strings like the following:

str1=C:\test\abc\a.docx
str2=C:\test\cbd\xx\b.docx
str3=C:\test\ccc.txt
...
Is there any way with batch script to get only abc for str1, xx for str2, and test for str3 out?
jl66ConsultantAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewIT / Software Engineering ConsultantCommented:
Sure, use the FOR statement, like this:

@echo off
setlocal

set str1=C:\test\abc\a.docx
set str2=C:\test\cbd\xx\b.docx
set str3=C:\test\ccc.txt

for /f "tokens=3 delims=\" %%A in ("%str1%") do echo "%%A"
for /f "tokens=4 delims=\" %%A in ("%str2%") do echo "%%A"
for /f "tokens=2 delims=\" %%A in ("%str3%") do echo "%%A"

Open in new window

~bp
oBdACommented:
So you want the parent folder of a give file path?
Here's one way:
set str1=C:\test\abc\a.docx
for %%a in ("%str1%") do (set Parent=%%~dpa)
set Parent=%Parent:~0,-1%
for %%a in ("%Parent%") do (set Parent=%%~nxa)
echo Parent: %Parent%

Open in new window

Or implemented as a function:
@echo off
setlocal
set str1=C:\test\abc\a.docx
set str2=C:\test\cbd\xx\b.docx
set str3=C:\test\ccc.txt

call :GetParent Parent1 "%str1%"
echo Parent1: %Parent1%
call :GetParent Parent2 "%str2%"
echo Parent2: %Parent2%
call :GetParent Parent3 "%str3%"
echo Parent3: %Parent3%
REM ...

REM ***** Only functions below this line *****
goto :eof
:GetParent <Variable> <Path>
setlocal
for %%a in ("%~2") do (set __Parent=%%~dpa)
set __Parent=%__Parent:~0,-1%
for %%a in ("%__Parent%") do (set __Parent=%%~nxa)
endlocal&set %1=%__Parent%
goto :eof

Open in new window

Bill PrewIT / Software Engineering ConsultantCommented:
Doh, if indeed you are looking for a file's parent (rather than a substring) , then this would do that.

@echo off
setlocal

set str1=C:\test\abc\a.docx
set str2=C:\test\cbd\xx\b.docx
set str3=C:\test\ccc.txt

for %%A in ("%str1%") do (for %%B in ("%%~dpA.") do echo "%%~nB")
for %%A in ("%str2%") do (for %%B in ("%%~dpA.") do echo "%%~nB")
for %%A in ("%str3%") do (for %%B in ("%%~dpA.") do echo "%%~nB")

Open in new window

~bp
Bill PrewIT / Software Engineering ConsultantCommented:
And here is a subroutine approach added as well.

@echo off
setlocal

set str1=C:\test\abc\a.docx
set str2=C:\test\cbd\xx\b.docx
set str3=C:\test\ccc.txt

for %%A in ("%str1%") do (for %%B in ("%%~dpA.") do echo "%%~nB")
for %%A in ("%str2%") do (for %%B in ("%%~dpA.") do echo "%%~nB")
for %%A in ("%str3%") do (for %%B in ("%%~dpA.") do echo "%%~nB")

call :Parent %str1%
echo "%Parent%"
call :Parent %str2%
echo "%Parent%"
call :Parent %str3%
echo "%Parent%"
exit /b

:Parent [file-path]
  for %%A in ("%~1") do (for %%B in ("%%~dpA.") do set Parent=%%~nB)
  exit /b

Open in new window

~bp

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jl66ConsultantAuthor Commented:
Greatly appreciated it. for offering nice solution so quickly..
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.