Solved

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

Posted on 2009-07-08
7
546 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 69

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 69

Expert Comment

by:Qlemo
ID: 24807069
Confirmed.
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup" or a blinking cursor with black screen. A loop for Auto repair will start but fix nothing.  You will be panic as there are no back…
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…
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…

777 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