someuser77 Posted September 19, 2005 Share Posted September 19, 2005 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! Link to comment Share on other sites More sharing options...
Noise Posted September 19, 2005 Share Posted September 19, 2005 SET SUBDIR=%~dp0ECHO %SUBDIR% Link to comment Share on other sites More sharing options...
DL. Posted September 19, 2005 Share Posted September 19, 2005 The current dir is stored in the variable %CD%.Using the command ECHO %CD% will show you what it is. Link to comment Share on other sites More sharing options...
someuser77 Posted September 19, 2005 Author Share Posted September 19, 2005 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. Link to comment Share on other sites More sharing options...
Yzöwl Posted September 20, 2005 Share Posted September 20, 2005 (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 directoryfor %%* in (.) do set MyDir=%%~n*:: adding safety factor for no directory, i.e a driveif not defined MyDir set MyDir=%CD:\=%:: telling you what it isecho/ 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 %~dp0for %%* in (.) do @echo %%~n*popd&endlocal&goto :eof</Edit> Edited September 20, 2005 by Yzöwl Link to comment Share on other sites More sharing options...
someuser77 Posted September 20, 2005 Author Share Posted September 20, 2005 thanks Yzöwl! Link to comment Share on other sites More sharing options...
emsr21 Posted December 7, 2008 Share Posted December 7, 2008 hi everyone i'm completely new at this...could someone possibly please explain Yzöwl's codefor %%* in (.) do @echo %%~n*i've referred to command-lind reference A-Zbut i don't understand especially those var.. %%*, (.), and %%~n*thnx Link to comment Share on other sites More sharing options...
Yzöwl Posted December 7, 2008 Share Posted December 7, 2008 Press 'Winkey + R' and in the dialog box enter:%comspec% /c For /?>%userprofile%\Desktop\ForDetails.txtThen you can read the text file now located on your desktop! Link to comment Share on other sites More sharing options...
jaclaz Posted December 7, 2008 Share Posted December 7, 2008 I'll try to add my small contribution to the work of the master 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 OFFCALL :EXPAND . DOTCALL :EXPAND .. DOUBLE DOTGOTO :EOF:EXPANDECHO %1 %2ECHO %~d1 (d=drive)ECHO %~dp1 (dp=drive and path)ECHO %~dpn1 (dpn=drive, path and name)ECHO %~n1 (n=name)GOTO :EOFOr, if you prefer:@ECHO OFFFOR %%A IN (.) DO (ECHO %%AECHO %%~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 variablethe . (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.htmlIn 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 filejaclaz Link to comment Share on other sites More sharing options...
Pbs Posted January 23, 2011 Share Posted January 23, 2011 (edited) i can't believe i can add to the masters both...for batch file's current dir:for %%A in ("%~dp0.") do echo %%~nAi 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 dirscall :getparentdir "d:\path\sub\file.ext"goto :eof:getparentdirfor %%A in ("%~dp1.") do echo %%~nAgoto :eofand use .. ["%~dp0.."] to get dir 2dirs up... my first post..Enjoy![edited to add quotes] Edited January 24, 2011 by Pbs Link to comment Share on other sites More sharing options...
jaclaz Posted January 23, 2011 Share Posted January 23, 2011 i can't believe i can add to the masters both...for batch file's current dir:for %%A in (%~dp0.) do echo %%~nAHmm.Try running your batch in a folder in a path with spaces. Then try running Yzöwl's original one, or, if you prefer, run this:@ECHO OFFfor %%A in (%~dp0.) do echo %%~nAfor %%* in (.) do @echo %%~n*Any difference? my first post..Welcome to the board. jaclaz Link to comment Share on other sites More sharing options...
Pbs Posted January 23, 2011 Share Posted January 23, 2011 (edited) oops, but this fixes that...i ran it in a long name dir, thought it had spaces...for %%A in ("%~dp0.") do echo %%~nAnobody'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 %%~nAit'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? lolyesterday i wrote 24->12hr + am/pm conversion completely inside a sqlite query...and dude, i've learned so much over the years from you it's silly...thanx so much for sharing... Edited January 24, 2011 by Pbs Link to comment Share on other sites More sharing options...
jaclaz Posted January 23, 2011 Share Posted January 23, 2011 Good. So you have a "fixed", "better" version of:for %%* in (.) do echo %%~n*consisting of:for %%A in ("%~dp0.") do echo %%~nA I see it as a "longer" version (even if by a few characters). jaclaz Link to comment Share on other sites More sharing options...
Pbs Posted January 23, 2011 Share Posted January 23, 2011 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] Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now