2010-12-02 133 views
6

我有一个批处理文件,我不知道它会是什么驱动器号,因为我将会移动很多。在批处理文件中创建一个相对路径

例如: 的Adobe文件驻留在:j:\文件\新建文件夹\ USB \ Adob​​

批处理文件执行从:j:\文件\新建文件夹\ USB \ USBSTICK

所以我试了代码:

xcopy /s /y "%~dp0\..\..\USB\Adob\*" "C:\Program Files\" 

但它不会复制文件。我怎样才能让它动态?

+2

获取驱动器盘符的简单方法是“echo!cd:〜1!”。这应该回显CD(当前目录)变量中的第一个字符。 – 2013-02-24 01:18:41

回答

0

尝试不使用“\”,因为它是%〜dp0的一部分。我的意思是,%〜dp0的内容总是以“\”结尾。

例子:

xcopy /s /y "%~dp0..\..\USB\Adob\*" "C:\Program Files\" 

编辑

以防万一......

  is Adob\* or Adob\*.* ?? 
+0

仍然没有快乐0文件被复制 – Mike 2010-12-02 06:17:08

0

我从您的层次结构的了解,这也将工作:

xcopy /s /y "..\Adob\*" "C:\Program Files\" 

由于J:\Files\New folder\USB是一个常见前缀,并且您正在运行J:\Files\New folder\USB\USBSTICK的批处理文件,所以无论您处理的驱动器号是什么,都应该可以正常工作。

0

我不确定我是否完全理解您的问题。但在我看来,根据我的理解,有几种解决方案可以解决问题。

如果路径始终是固定的,但只有盘符可能会有所不同,你可以只使用相对路径:

xcopy /s /y "..\Adob\*" "C:\Program Files\" 

从任何驱动器调用批处理程序,然后将如预期所提供的批处理文件总是存在于与Adob相同的目录中的USBSTICK中。

如果源路径可以改变,只需用一个参数替换不同的部分,并调用使用正确的路径批处理文件:

xcopy /s /y "%1\*" "C:\Program Files\" 

从任何驱动器中调用批处理程序,然后位置会工作作为预计当您提供正确的路径时:

xcopybatch J:\Files\New folder\USB\Adob 
2

由于驱动器号似乎是您的方案的相对部分。我相信这应该对你更好,除非我误解了你。

xcopy /s /y "%~d0\Files\New folder\USB\Adob\*" "C:\Program Files\" 

更多的变量,你可以使用请按照下列步骤操作:
从CMD,输入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 
%~$PATH:I - searches the directories listed in the PATH 
       environment variable and expands %I to the 
       fully qualified name of the first one found. 
       If the environment variable name is not 
       defined or the file is not found by the 
       search, then this modifier expands to the 
       empty string 

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 
%~dp$PATH:I - searches the directories listed in the PATH 
       environment variable for %I and expands to the 
       drive letter and path of the first one found. 
%~ftzaI  - expands %I to a DIR like output line 

In the above examples %I and PATH can be replaced by other valid 
values. The %~ syntax is terminated by a valid FOR variable name. 
Picking upper case variable names like %I makes it more readable and 
avoids confusion with the modifiers, which are not case sensitive.