2016-12-27 315 views
1

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

到目前为止,我见过的所有例子都没有成功帮助我完成这个任务,因为他们通常需要一个已经给定的分割路径。

Ex。

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

我怎么能存储path这个给到一个变量?

任何帮助表示赞赏。

回答

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 
相关问题