Solved

parse a string in a batch file

Posted on 2006-06-29
3
622 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 83

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

by Batuhan Cetin In this article I will be guiding through the process of removing a failed DC metadata from Active Directory (hereafter, AD) using the ntdsutil tool in a Windows Server 2003 environment. These steps are not necessary in a Win…
Recently, I had the need to build a standalone system to run a point-of-sale system. I’m running this on a low-voltage Atom processor, so I wanted a light-weight operating system, but still needed Windows. I chose to use Microsoft Windows Server 200…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

863 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

25 Experts available now in Live!

Get 1:1 Help Now