Solved

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

Posted on 2009-07-08
7
548 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
[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
  • 2
  • 2
7 Comments
 
LVL 70

Accepted Solution

by:
Qlemo earned 65 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 60 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 70

Expert Comment

by:Qlemo
ID: 24807069
Confirmed.
0
Easy, flexible multimedia distribution & control

Coming soon!  Ideal for large-scale A/V applications, ATEN's VM3200 Modular Matrix Switch is an all-in-one solution that simplifies video wall integration. Easily customize display layouts to see what you want, how you want it in 4k.

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

While rebooting windows server 2003 server , it's showing "active directory rebuilding indices please wait" at startup. It took a little while for this process to complete and once we logged on not all the services were started so another reboot is …
Learn how to PXE Boot both BIOS & UEFI machines with DHCP Policies and Custom Vendor Classes
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

734 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