CMD:仅使用文件名

时间:2016-12-27 23:27:20

标签: batch-file variables cmd path directory

我正在创建一个程序,允许用户搜索'用于文件的计算机,如果是文件exists,它将在新目录中创建start该文件的快捷方式。可以使用任何和所有扩展。问题是,为了start某些扩展名(例如.jar),我需要文件的path

到目前为止,我所见过的所有例子都没有成功地帮助我解决这个问题,因为它们通常需要一条已经分开的路径。

实施例。

(dir %Filename%.%extension% /b /s /a:-D)

我如何才能将仅将存储的path存储到变量中?

感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

如果您使用for循环来处理dir命令的输出,批处理将自动知道路径和文件名。从那里开始,只需使用众所周知的%~dp语法来获取路径。

for /f "delims=" %%A in ('dir /b /s /a:-d') do echo %~dpA

其中d获取驱动器,p获取路径。

其他选项(来自for /?):

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