Jump to content

Recommended Posts

Posted

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!


Posted

The current dir is stored in the variable %CD%.

Using the command ECHO %CD% will show you what it is.

Posted (edited)

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
  • 3 years later...
Posted

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

Posted

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!

Posted

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.robvanderwoude.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

  • 2 years later...
Posted (edited)

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
Posted

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

Posted (edited)

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
Posted

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

Posted

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]

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...