Jump to content

Welcome to MSFN Forum
Register now to gain access to all of our features. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more. This message will be removed once you have signed in.
Login to Account Create an Account



Photo

retrieve current directory name in batch

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
13 replies to this topic

#1
someuser77

someuser77

    Newbie

  • Member
  • 27 posts
  • Joined 11-May 05
hello. :)
i need to retrieve the current directory name from a batch file in windows 2000.
for example:
if i run the batch file in c:\dir\subdir (c:\dir\subdir\mybat.cmd) i will have a variable that holds the name "subdir" as its the location of the file.
thanks!


How to remove advertisement from MSFN

#2
Noise

Noise

    Windows Guru

  • Member
  • PipPipPip
  • 419 posts
  • Joined 27-February 04
SET SUBDIR=%~dp0
ECHO %SUBDIR%

#3
DL.

DL.

    Tweaker or whatever I happen to be focused on at the moment..

  • Member
  • PipPipPip
  • 489 posts
  • Joined 05-March 05
The current dir is stored in the variable %CD%.
Using the command ECHO %CD% will show you what it is.

#4
someuser77

someuser77

    Newbie

  • Member
  • 27 posts
  • Joined 11-May 05
thank you for your replies.
i am afraid your methods returns the path, i need only the last dir:
c:\dir\subdir\ -> subdir and not c:\dir\subdir or \dir\subdir.

#5
Yzöwl

Yzöwl

    Wise Owl

  • Super Moderator
  • 4,078 posts
  • Joined 13-October 04
Is this what you wanted?
for %* in (.) do @echo %~n*
Here it is in a batch, (cmd), file to make it easier to test
@echo off&setlocal enableextensions
:: retrieving  name of current directory
for %%* in (.) do set MyDir=%%~n*
:: adding safety factor for no directory, i.e a drive
if not defined MyDir set MyDir=%CD:\=%
:: telling you what it is
echo/ the current directory is %MyDir%
endlocal&goto :eof
<Edit>
After reading back your first post, it appears as if you are looking for the parent directory of the currently running batch file; this is not the same as the Current Directory and makes the previous response somewhat invalid. However the easiest way to do that is to first of all make the directory containing the batch file also the current directory
@echo off&setlocal enableextensions&pushd %~dp0
for %%* in (.) do @echo %%~n*
popd&endlocal&goto :eof
</Edit>

Edited by Yzöwl, 20 September 2005 - 06:33 AM.


#6
someuser77

someuser77

    Newbie

  • Member
  • 27 posts
  • Joined 11-May 05
thanks Yzöwl!

#7
emsr21

emsr21
  • Member
  • 1 posts
  • Joined 06-December 08
hi everyone :hello:
i'm completely new at this...
could someone possibly please explain Yzöwl's code

 
for %%* in (.) do @echo %%~n* 


i've referred to command-lind reference A-Z
but i don't understand especially those var.. :wacko:
%%*, (.), and %%~n*

thnx

#8
Yzöwl

Yzöwl

    Wise Owl

  • Super Moderator
  • 4,078 posts
  • Joined 13-October 04
Press 'Winkey + R' and in the dialog box enter:
%comspec% /c For /?>%userprofile%\Desktop\ForDetails.txt
Then you can read the text file now located on your desktop!

#9
jaclaz

jaclaz

    The Finder

  • Developer
  • 15,804 posts
  • Joined 23-July 04
I'll try to add my small contribution to the work of the master :thumbup

Since the dawn of DOS, the . (dot) means "current directory" and .. (double dot) means "directory above current".

Try putting this into a batch (like dotexpand.cmd):
@ECHO OFF
CALL :EXPAND . DOT
CALL :EXPAND .. DOUBLE DOT
GOTO :EOF

:EXPAND
ECHO %1 %2
ECHO %~d1 (d=drive)
ECHO %~dp1 (dp=drive and path)
ECHO %~dpn1 (dpn=drive, path and name)
ECHO %~n1 (n=name)
GOTO :EOF

Or, if you prefer:
@ECHO OFF
FOR %%A IN (.) DO (
ECHO %%A
ECHO %%~dA [d=drive]
ECHO %%~dpA [dp=drive and path]
ECHO %%~dpnA [dpn=drive, path and name]
ECHO %%~nA [n=name]
)

In other words:
the * (asterisk) in this case is just a "normal" character assigned to a FOR variable
the . (dot) is the "special" meaning of "current directory"
the %% (double percent) is needed because the variable is run from a batch (from command line use a single %)
the ~ (tilde) is the character for variable expansion. (NT and later only)
the n is one of the available expansion "names". (NT and later only)

See this:
http://www.robvander....com/ntfor.html

In addition, substitution of FOR variable references has been enhanced.
You can now use the following optional syntax:%~i - expands %i removing any surrounding quotes (")
%~fi - expands %i to a fully qualified path name
%~di - expands %i to a drive letter only
%~pi - expands %i to a path only
%~ni - expands %i to a file name only
%~xi - expands %i to a file extension only
%~si - expanded path contains short names only
%~ai - expands %i to file attributes of file
%~ti - expands %i to date/time of file
%~zi - expands %i to size of file


jaclaz

#10
Pbs

Pbs
  • Member
  • 3 posts
  • Joined 23-January 11
i can't believe i can add to the masters both...
for batch file's current dir:
for %%A in ("%~dp0.") do echo %%~nA
:D
i had been using a longer method so i'm killin myself for not seeing yours sooner..
also, for say any file, just add \. to it's path only [for %1 use %p1\.]and use it's name in for loop result..[%~n* ]
so you could have it as a sub-routine and pass paths to it in your batch to get parent dir..[like say,dir of a bunch of mp3s artist dirs

call :getparentdir "d:\path\sub\file.ext"
goto :eof

:getparentdir
for %%A in ("%~dp1.") do echo %%~nA
goto :eof
and use .. ["%~dp0.."] to get dir 2dirs up... :D

my first post..:)
Enjoy!

[edited to add quotes]

Edited by Pbs, 23 January 2011 - 07:09 PM.


#11
jaclaz

jaclaz

    The Finder

  • Developer
  • 15,804 posts
  • Joined 23-July 04

i can't believe i can add to the masters both...
for batch file's current dir:

for %%A in (%~dp0.) do echo %%~nA

Hmm.

Try running your batch in a folder in a path with spaces. :whistle:

Then try running Yzöwl's original one, or, if you prefer, run this:
@ECHO OFF
for %%A in (%~dp0.) do echo %%~nA
for %%* in (.) do @echo %%~n*

Any difference? ;)

my first post..

Welcome to the board. :)

jaclaz

#12
Pbs

Pbs
  • Member
  • 3 posts
  • Joined 23-January 11
oops, but this fixes that...i ran it in a long name dir, thought it had spaces...
for %%A in ("%~dp0.") do echo %%~nA
:D
nobody's perfect...lol
[it *was* 5am when i wrote it! :)]

was trying to re-encode my music to my phone in diff dirs and keep the artist\album\song.ext structure..

and just tried this, so if you want to go more than 2 dirs up, use
for %%A in (".\..\..\..") do echo %%~nA
goes up 4 dirs.. etc to go up as many as you want...
or relative to batch file itself:
for %%A in ("%~dp0.\..\..\..") do echo %%~nA

it's:
.=1 file's dir
..=2 parent dir [or.\..]
.\..\..=3 up [the parent dirs parent dir]
.\..\..\..=4 up etc etc..

that one surprised me even..nice n short too..
from cmdline try it out:
for %a in (".\..\..") do echo %~na
[since not starting with a file, no leading .]
was there smart juice in my orange juice this morning or what? lol
yesterday i wrote 24->12hr + am/pm conversion completely inside a sqlite query...:D

and dude, i've learned so much over the years from you it's silly...thanx so much for sharing...

Edited by Pbs, 24 January 2011 - 05:40 AM.


#13
jaclaz

jaclaz

    The Finder

  • Developer
  • 15,804 posts
  • Joined 23-July 04
Good. :)

So you have a "fixed", "better" version of:
for %%* in (.) do echo %%~n*
consisting of:
for %%A in ("%~dp0.") do echo %%~nA

:unsure:

I see it as a "longer" version (even if by a few characters). :whistle:

jaclaz

#14
Pbs

Pbs
  • Member
  • 3 posts
  • Joined 23-January 11
ahh, but 2 diff things, mine is relative to batch file, other is relative to workingdir...
i use a pvr program that runs batches from a diff dir so makes all the diff to me..[always had to work around that]