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
Solved

parse a string in a batch file

Posted on 2006-06-29
3
624 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 84

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Preface Having the need * to contact many different companies with different infrastructures * do remote maintenance in their network required us to implement a more flexible routing solution. As RAS, PPTP, L2TP and VPN Client connections are no…
ADCs have gained traction within the last decade, largely due to increased demand for legacy load balancing appliances to handle more advanced application delivery requirements and improve application performance.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

809 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