windows 从批处理文件的目录复制文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19303155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 10:58:44  来源:igfitidea点击:

Copy file from batch file's directory

windowsbatch-filecommand-prompt

提问by Kyle Wright

I know only the very basics of writing batch files. I am trying to figure out how to write one that, given any directory it is in, would copy a file that is within the same directory and put it in a new location. I know how to copy the file and move it, but I don't know how to write the batch file to understand its directory and then grab a different file.

我只知道编写批处理文件的基础知识。我试图弄清楚如何编写一个,给定它所在的任何目录,将复制同一目录中的文件并将其放在新位置。我知道如何复制文件并移动它,但我不知道如何编写批处理文件以了解其目录,然后抓取不同的文件。

I read that %0 represents the directory the file is in, but then how can I append a file to that?

我读到 %0 表示文件所在的目录,但是我如何向该目录附加文件?

I've tried this:

我试过这个:

copy "%0\Move.txt" "C:\"

Maybe that was stupid but I'm a newbie. Help please?

也许那是愚蠢的,但我是一个新手。请帮忙?

回答by David Ruhmann

%0contains the full path and file name to the batch script.

%0包含批处理脚本的完整路径和文件名。

Use just %~dp0to get the path without the batch script file name.

%~dp0用于获取不带批处理脚本文件名的路径。

copy "%~dp0\Move.txt" "C:\"

Use the echocommand to view what a variable holds when you are having a problem.

echo当您遇到问题时,使用该命令查看变量保存的内容。

echo %0

From call /?

call /?

Substitution of batch parameters (%n) has been enhanced.  You can
now use the following optional syntax:

    %~1         - expands %1 removing any surrounding quotes (")
    %~f1        - expands %1 to a fully qualified path name
    %~d1        - expands %1 to a drive letter only
    %~p1        - expands %1 to a path only
    %~n1        - expands %1 to a file name only
    %~x1        - expands %1 to a file extension only
    %~s1        - expanded path contains short names only
    %~a1        - expands %1 to file attributes
    %~t1        - expands %1 to date/time of file
    %~z1        - expands %1 to size of file
    %~$PATH:1   - searches the directories listed in the PATH
                   environment variable and expands %1 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:

    %~dp1       - expands %1 to a drive letter and path only
    %~nx1       - expands %1 to a file name and extension only
    %~dp$PATH:1 - searches the directories listed in the PATH
                   environment variable for %1 and expands to the
                   drive letter and path of the first one found.
    %~ftza1     - expands %1 to a DIR like output line

In the above examples %1 and PATH can be replaced by other
valid values.  The %~ syntax is terminated by a valid argument
number.  The %~ modifiers may not be used with %*

回答by gh123man

If you are just trying to copy a file from the current directory to a new one, you may simply do this:

如果您只是想将文件从当前目录复制到新目录,您可以简单地执行以下操作:

copy "Move.txt" "C:\"

Not prefixing the file "Move.txt" means it is in the current directory.

没有前缀文件“Move.txt”意味着它在当前目录中。