Solved

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

Posted on 2009-07-08
7
544 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 68

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 68

Expert Comment

by:Qlemo
ID: 24807069
Confirmed.
0
Are end users causing IT problems again?

You’ve taken the time to design and update all your end user’s email signatures, only to find out they’re messing up the HTML, changing the font and ruining the imagery. What can you do to prevent this? Find out how you can save your signatures from end users today.

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
In this article, I will show you HOW TO: Perform a Physical to Virtual (P2V) Conversion the easy way from a computer backup (image).
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

746 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

13 Experts available now in Live!

Get 1:1 Help Now