Solved

parse a string in a batch file

Posted on 2006-06-29
3
626 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 96

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 85

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Numerous times I have been asked this questions that what is it that makes my machine log on so slow, there have been cases where computers took 23 minute exactly after taking password and getting to the desktop. Interesting thing was the fact th…
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…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

691 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