tags:

views:

7056

answers:

1

I need path to the folder that contains cmd file. With %0 I can get file name. But how to get folder name?

c:\temp\test.cmd >> test.cmd

P.S. My current directory != folder of the script.

+11  A: 

For the folder name and drive, you can use:

echo %~dp0

You can get a lot more information using different modifiers:

%~0         - expands %I removing any surrounding quotes (")
%~f0        - expands %I to a fully qualified path name
%~d0        - expands %I to a drive letter only
%~p0        - expands %I to a path only
%~n0        - expands %I to a file name only
%~x0        - expands %I to a file extension only
%~s0        - expanded path contains short names only
%~a0        - expands %I to file attributes of file
%~t0        - expands %I to date/time of file
%~z0        - expands %I to size of file

The modifiers can be combined to get compound results:
%~dp0       - expands %I to a drive letter and path only
%~nx0       - expands %I to a file name and extension only
%~fs0       - expands %I to a full path name with short names only

This is a copy paste from the "for /?" command on the prompt. Hope it helps.

Related

Top 10 DOS Batch tips (Yes, DOS Batch...) shows batchparams.bat (link to source as a gist):

C:\Temp>batchparams.bat c:\windows\notepad.exe
%~1     =      c:\windows\notepad.exe
%~f1     =      c:\WINDOWS\NOTEPAD.EXE
%~d1     =      c:
%~p1     =      \WINDOWS\
%~n1     =      NOTEPAD
%~x1     =      .EXE
%~s1     =      c:\WINDOWS\NOTEPAD.EXE
%~a1     =      --a------
%~t1     =      08/25/2005 01:50 AM
%~z1     =      17920
%~$PATHATH:1     =
%~dp1     =      c:\WINDOWS\
%~nx1     =      NOTEPAD.EXE
%~dp$PATH:1     =      c:\WINDOWS\
%~ftza1     =      --a------ 08/25/2005 01:50 AM 17920 c:\WINDOWS\NOTEPAD.EXE
Wadih M.
I've added batchparams.bat
J.F. Sebastian
Cool. Do I need a particular score to modify someone else's wiki post?
Wadih M.
@Wadih M.: Generally useful link http://stackoverflow.com/questions/18557/how-does-stackoverflow-work-the-unofficial-faq
J.F. Sebastian
@Wadih M.: In particular http://stackoverflow.com/questions/130654/how-does-reputation-work-on-stackoverflow
J.F. Sebastian
@Wadih M.: From the above link: "+750 to edit community 'wiki editable' posts"
J.F. Sebastian
Thanks guys! That is very helpfull!
Mike Chaliy