[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

parse a string in a batch file

Posted on 2006-06-29
3
Medium Priority
?
632 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 97

Accepted Solution

by:
Lee W, MVP earned 1000 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 1000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

by Batuhan Cetin Within the dynamic life of an IT administrator, we hold many information in our minds like user names, passwords, IDs, phone numbers, incomes, service tags, bills and the order from our wives to buy milk when coming back to home.…
This article provides a convenient collection of links to Microsoft provided Security Patches for operating systems that have reached their End of Life support cycle. Included operating systems covered by this article are Windows XP,  Windows Server…
Integration Management Part 2
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

868 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