在批处理文件中创建相对路径

时间:2010-12-02 05:42:09

标签: path batch-file xcopy

我有一个批处理文件,我不知道它将会是什么驱动器字母,因为我将会移动很多。

例如: adobe文件位于:J:\ Files \ New folder \ USB \ Adob​​

批处理文件从以下位置执行:J:\ Files \ New folder \ USB \ USBSTICK

所以我尝试了代码:

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

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

4 个答案:

答案 0 :(得分: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.

答案 1 :(得分:0)

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

示例:

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

修改

以防万一...

            is Adob\* or Adob\*.* ??

答案 2 :(得分:0)

根据我对您的层次结构的理解,这也可行:

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

由于J:\Files\New folder\USB是一个公共前缀,并且您正在运行J:\Files\New folder\USB\USBSTICK的批处理文件,无论您正在处理哪个驱动器号,这都应该有效。

答案 3 :(得分:0)

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

如果路径始终是固定的,但只有驱动器号可能会有所不同,则可以使用相对路径:

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

如果批处理文件始终存在于与Adob相同的目录中的USBSTICK中,则从任何驱动器调用批处理程序将按预期工作。

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

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

当您为其提供正确的路径时,从任何驱动器和位置调用批处理程序将按预期工作:

xcopybatch J:\Files\New folder\USB\Adob
相关问题