Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

DOS variable set to current directory ONLY.  Not the entire path.

Posted on 2009-07-08
7
Medium Priority
?
556 Views
Last Modified: 2012-06-27
This returns the current directory, but with the entire path as well. How can I get JUST the current directory?

Say this is the path: C:\WINDOWS\system32\2008-02-29_1157 I'm working in.

"set currentdir=%cd%" would set the currentdir variable to "C:\WINDOWS\system32\2008-02-29_1157"  (if run from inside that directory, of course)

That's handy, but how would I set a variable to the actual current directory ONLY.  I need the variable set to: "2008-02-29_1157" only.

The reason for this is that I need to use tokens of that variable to rename files located in that directory.

0
Comment
Question by:americannb
  • 3
  • 2
  • 2
7 Comments
 
LVL 71

Accepted Solution

by:
Qlemo earned 260 total points
ID: 24806932
In a batch file:
for %%D in ("%cd%") do set currentdir=%%~nD
0
 
LVL 16

Assisted Solution

by:t0t0
t0t0 earned 240 total points
ID: 24807036
Yep, as Qlemo has shown, you have to 'cheat' dos into returning what is believes is a filename. In fact, because foldernames can also contain a dotted 'extension name' it is better to use the following:

   for /f "tokens=*" %%a in ('cd') do set currentdir=%%~nxa

or

   for %%a in ("%cd%") do set currentdir=%%~nxa
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 24807069
Confirmed.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 16

Expert Comment

by:t0t0
ID: 24807102
:-)
0
 

Author Closing Comment

by:americannb
ID: 31601264
All excellent answers and very helpful!
0
 

Author Comment

by:americannb
ID: 24807129
BAM!  That's the stuff, fellas!  I can actually use both answers in different situations!  And the different answers also helped me understand the logic which I can apply other places.  Thanks a bunch!!!


0
 
LVL 16

Expert Comment

by:t0t0
ID: 24807169
WOW! That was quick! Thank you, and glad we could help in other ways too.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

IF you are either unfamiliar with rootkits, or want to know more about them, read on ....
By default Outlook 2016 displays only one time zone in the Calendar. The following article explains how to display two time zones in one calendar view.
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

916 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