如何从CMD文件路径获取文件夹路径 [英] How to get folder path from file path with CMD

查看:1754
本文介绍了如何从CMD文件路径获取文件夹路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要包含cmd文件的文件夹的路径。
使用%0我可以得到文件名。但是如何获取文件夹名称?



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



PS

对于文件夹名称和驱动器,您可以使用:

  echo%〜dp0 

您可以使用不同的修饰符获得更多信息:

 %〜I  - )
%〜fI - 将%I扩展为完全限定路径名
%〜dI - 将%I扩展到仅驱动器盘符
%〜pI - 将%I扩展为仅路径
%〜nI - 将%I扩展为仅文件名
%〜xI - 仅将%I扩展为文件扩展名
%〜sI - 扩展路径仅包含短名称
%〜aI - 将%I扩展到文件的文件属性
%〜tI - 将%I扩展到文件的日期/时间
%〜zI - 将%I扩展到文件的大小

修改器可以组合得到复合结果:
%〜dpI - 将%I扩展到驱动器号和路径
%〜nxI - 将%I扩展到文件名和扩展名
%〜fsI - 将%I扩展为仅包含短名称的完整路径名

复制粘贴从for /?命令提示符。希望它有帮助。



相关



十大DOS批次提示(是,DOS批次...) / a>显示 batchparams.bat (以源代码作为提示的链接):

  C:\ Temp> batchparams.bat c:\windows\\\
otepad.exe
%〜1 = c:\windows \\\
otepad.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


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.

解决方案

For the folder name and drive, you can use:

echo %~dp0

You can get a lot more information using different modifiers:

%~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

The modifiers can be combined to get compound results:
%~dpI       - expands %I to a drive letter and path only
%~nxI       - expands %I to a file name and extension only
%~fsI       - 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

这篇关于如何从CMD文件路径获取文件夹路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆