Solved

parse a string in a batch file

Posted on 2006-06-29
3
621 Views
Last Modified: 2008-03-03
i am passing a string to a batch file ex: c:\dir1\dir2\dir3
i need to get last folder which is dir3 and assign it to a variable within a batch file. How can this be done?
0
Comment
Question by:TransBind
3 Comments
 
LVL 95

Accepted Solution

by:
Lee W, MVP earned 250 total points
ID: 17015499
If the directory depth is consistant, then you can use:

for /f "tokens=4 delims=\" %%a in ("c:\dir1\dir2\dir3") do set Dir3=%%a
0
 
LVL 82

Assisted Solution

by:oBdA
oBdA earned 250 total points
ID: 17016152
You can do that with the path expansion (enter "help for" for details); %~nxI will give you the *n*ame and the e*x*tension of the variable %*I*, which in your case is a folder name. For this to work, the string you pass in the command line may not end with a backslash, so if one exists at the end, it's stripped off:

@echo off
set Directory=%1
REM *** If present, remove a trailing backslash for the path expansion to work:
if "%Directory:~-1%"=="\" set Directory=%Directory:~0,-1%
REM *** Retrieve the last element of the path and set the variable LastFolder accordingly:
for %%a in (%Directory%) do set LastFolder=%%~nxa
echo LastFolder=%LastFolder%
0
 
LVL 7

Author Comment

by:TransBind
ID: 17020301
Thank you ...
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I've always wanted to allow a user to have a printer no matter where they login. The steps below will show you how to achieve just that. In this Article I'll show how to deploy printers automatically with group policy and then using security fil…
On July 14th 2015, Windows Server 2003 will become End of Support, leaving hundreds of thousands of servers around the world that still run this 12 year old operating system vulnerable and potentially out of compliance in many organisations around t…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now