xcopy 文件、重命名、抑制“xxx 是否指定文件名...”信息

发布于 2024-09-05 22:02:19 字数 628 浏览 2 评论 0 原文

这看起来很简单,也许我只是忽略了 正确的标志,但是我如何在一个命令中将文件从一个目录复制到另一个目录并在目标目录中重命名?这是我的命令:

if exist "bin\development\whee.config.example"
  if not exist "TestConnectionExternal\bin\Debug\whee.config"
    xcopy "bin\development\whee.config.example"
          "TestConnectionExternal\bin\Debug\whee.config"

它每次都会提示我以下内容:

TestConnectionExternal\bin\Debug\whee.config 是否指定文件名 或目标上的目录名称(F = 文件,D = 目录)?

我想抑制这个提示;答案始终是F

This seems pretty simple and maybe I'm just overlooking the proper flag, but how would I, in one command, copy a file from one directory to another and rename it in the destination directory? Here's my command:

if exist "bin\development\whee.config.example"
  if not exist "TestConnectionExternal\bin\Debug\whee.config"
    xcopy "bin\development\whee.config.example"
          "TestConnectionExternal\bin\Debug\whee.config"

It prompts me with the following every time:

Does TestConnectionExternal\bin\Debug\whee.config specify a file name
or directory name on the target (F = file, D = directory)?

I want to suppress this prompt; the answer is always F.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(24

甜柠檬 2024-09-12 22:02:19

我习惯于

echo f | xcopy /f /y srcfile destfile

绕过它。

I use

echo f | xcopy /f /y srcfile destfile

to get around it.

乙白 2024-09-12 22:02:19

不要使用xcopy,而是使用copy,它没有这个问题。

xcopy 通常用于执行多个文件/文件夹的递归复制,或者当您需要它提供的验证/提示功能时。对于单个文件副本,copy 命令工作得很好。

Don't use the xcopy, use copy instead, it doesn't have this issue.

xcopy is generally used when performing recursive copies of multiple files/folders, or when you need the verification/prompting features it offers. For single file copies, the copy command works just fine.

迟到的我 2024-09-12 22:02:19

另一种选择是使用目标通配符。请注意,这仅在源文件名和目标文件名相同的情况下才有效,因此虽然这不能解决OP的具体示例,但我认为值得分享。

例如:

xcopy /y "bin\development\whee.config.example" "TestConnectionExternal\bin\Debug\*" 

将在目标目录中创建文件“whee.config.example”的副本,而不提示输入文件或目录。

更新:正如 @chapluck 所提到的:

您可以将 "* " 更改为 "[newFileName].*"。它保留文件扩展名,但允许重命名。或者更黑客: "[newFileName].[newExt]*" 更改扩展名

Another option is to use a destination wildcard. Note that this only works if the source and destination filenames will be the same, so while this doesn't solve the OP's specific example, I thought it was worth sharing.

For example:

xcopy /y "bin\development\whee.config.example" "TestConnectionExternal\bin\Debug\*" 

will create a copy of the file "whee.config.example" in the destination directory without prompting for file or directory.

Update: As mentioned by @chapluck:

You can change "* " to "[newFileName].*". It persists file extension but allows to rename. Or more hacky: "[newFileName].[newExt]*" to change extension

始终不够爱げ你 2024-09-12 22:02:19

XCOPY 中有某种未记录的功能。你可以使用:

xcopy "bin\development\whee.config.example" "c:\mybackup\TestConnectionExternal\bin\Debug\whee.config*"

我今天刚刚测试了它。 :-)

There is some sort of undocumented feature in XCOPY. you can use:

xcopy "bin\development\whee.config.example" "c:\mybackup\TestConnectionExternal\bin\Debug\whee.config*"

i tested it just today. :-)

魂ガ小子 2024-09-12 22:02:19

只需访问 http://technet.microsoft.com/en-us/library/ bb491035.aspx

主要问题是“...如果目标不包含现有目录并且不以反斜杠结尾 (),则会显示以下消息:...

目标是否指定文件名
或目标上的目录名称
(F = 文件,D = 目录)?

您可以使用 /i 命令行选项抑制此消息,如果源是多个文件或一个文件,则 xcopy 会假定目标是一个目录。目录。

我花了一些时间,但只需要 RTFM。

Just go to http://technet.microsoft.com/en-us/library/bb491035.aspx

Here's what the MAIN ISSUE is "... If Destination does not contain an existing directory and does not end with a backslash (), the following message appears: ...

Does destination specify a file name
or directory name on the target
(F = file, D = directory)?

You can suppress this message by using the /i command-line option, which causes xcopy to assume that the destination is a directory if the source is more than one file or a directory.

Took me a while, but all it takes is RTFM.

停滞 2024-09-12 22:02:19

因此,有一个简单的解决方法。诚然,这很尴尬,但确实有效。
如果新文件(文件名)已经存在,xcopy 将不会提示查找目标是目录还是文件。如果您在 xcopy 命令之前对新文件名进行简单的回显,它将覆盖空文件。例子

echo.>newfile.txt
xcopy oldfile.txt newfile.txt /Y

So, there is a simple fix for this. It is admittedly awkward, but it works.
xcopy will not prompt to find out if the destination is a directory or file IF the new file(filename) already exists. If you precede your xcopy command with a simple echo to the new filename, it will overwrite the empty file. Example

echo.>newfile.txt
xcopy oldfile.txt newfile.txt /Y
半仙 2024-09-12 22:02:19

仅当文件不存在于目标中或存在(使用新名称)但较旧时,我才尝试使用新名称复制文件时遇到同样的问题。解决方案是在目标文件名末尾添加 * 字符。例子:

xcopy "C:\src\whee.config.txt" "C:\dest\bee.config.txt*" /D /Y

I met same issue when try to copy file with new name only if file does not exist in destination or exist (with new name), but is older. The solution is to add * char at end of destination file name. Example:

xcopy "C:\src\whee.config.txt" "C:\dest\bee.config.txt*" /D /Y
爱,才寂寞 2024-09-12 22:02:19

这是比尔的回答。

只是为了让其他人真正清楚。

如果您要将一个文件从一个位置复制到另一个位置并且您希望创建完整的目录结构,请使用以下命令:

xcopy "C:\Data\Images\2013\08\12\85e4a707-2672-481b-92fb-67ecff20c96b.jpg" "C:\Target Data\\Images\2013\08\12\85e4a707-2672-481b-92fb-67ecff20c96b.jpg\" 

是的,在末尾添加反斜杠文件名,它不会询问您它是文件还是目录。因为源中只有一个文件,所以它会假设它是一个文件。

This is from Bills answer.

Just to be really clear for others.

If you are copying ONE file from one place to another AND you want the full directory structure to be created, use the following command:

xcopy "C:\Data\Images\2013\08\12\85e4a707-2672-481b-92fb-67ecff20c96b.jpg" "C:\Target Data\\Images\2013\08\12\85e4a707-2672-481b-92fb-67ecff20c96b.jpg\" 

Yes, put a backslash at the end of the file name and it will NOT ask you if it's a file or directory. Because there is only ONE file in the source, it will assume it's a file.

小兔几 2024-09-12 22:02:19

XCOPY 在目标末尾添加 * 来复制文件,无论它们是否存在于目标中
XCOPY 在目标末尾带有 \ 来复制文件夹和内容(无论目标中是否存在) 或者

用于

文件的 RoboForm SOURCE DEST FILE
RoboForm 文件夹的 SOURCE DEST

XCOPY with * at the end of the target to copy files whether they exist or not in destination
XCOPY with \ at the end of the target to copy folders and contents whether exist or not in destination

Alternatively

RoboForm SOURCE DEST FILE for files
RoboForm SOURCE DEST for folders

梦晓ヶ微光ヅ倾城 2024-09-12 22:02:19

xcopy src dest /I

REM 这假设 dest 是一个文件夹,如果不存在,则会创建它

xcopy src dest /I

REM This assumes dest is a folder and will create it, if it doesnt exists

冷默言语 2024-09-12 22:02:19

我遇到了类似的问题,robocopy 和 xcopy 都没有帮助,因为我想抑制注释并使用不同的目标文件名。我发现

type filename.txt > destfolder\destfilename.txt

工作符合我的要求。

I had a similar issue and both robocopy and xcopy did not help, as I wanted to suppress the comments and use a different destination filename. I found

type filename.txt > destfolder\destfilename.txt

working as per my requirements.

揽清风入怀 2024-09-12 22:02:19

回到最初的问题:

 xcopy "bin\development\whee.config.example" "TestConnectionExternal\bin\Debug\whee.config"

可以使用两个命令来完成,例如:

mkdir "c:\mybackup\TestConnectionExternal\bin\Debug\whee.config\.."
xcopy "bin\development\whee.config.example" "c:\mybackup\TestConnectionExternal\bin\Debug\whee.config\"

只需将“\..”附加到目标文件的路径,即可创建目标目录(如果目标目录尚不存在)。在本例中

"c:\mybackup\TestConnectionExternal\bin\Debug\"

,这是的父目录
不存在的目录

"c:\mybackup\TestConnectionExternal\bin\Debug\whee.config\.."

至少对于WIN7 mkdir 不关心该目录是否

"c:\mybackup\TestConnectionExternal\bin\Debug\whee.config\"

真的存在。

Back to the original question:

 xcopy "bin\development\whee.config.example" "TestConnectionExternal\bin\Debug\whee.config"

could be done with two commands eg:

mkdir "c:\mybackup\TestConnectionExternal\bin\Debug\whee.config\.."
xcopy "bin\development\whee.config.example" "c:\mybackup\TestConnectionExternal\bin\Debug\whee.config\"

By simply appending "\.." to the path of the destination file the destination directory is created if it not already exists. In this case

"c:\mybackup\TestConnectionExternal\bin\Debug\"

which is the parent directory of
the non-existing directory

"c:\mybackup\TestConnectionExternal\bin\Debug\whee.config\.."

At least for WIN7 mkdir does not care if the directory

"c:\mybackup\TestConnectionExternal\bin\Debug\whee.config\"

really exists.

何以笙箫默 2024-09-12 22:02:19

如果您只想复制文件并在目的地更改其名称,正确的做法是:

xcopy /f /y“bin\development\example.exe”
“TestConnectionExternal\bin\Debug\NewName.exe*”

它会正常工作

The right thing to do if you wanna copy just file and change it's name at destination is :

xcopy /f /y "bin\development\example.exe"
"TestConnectionExternal\bin\Debug\NewName.exe*"

And it's Gonna work fine

爱已欠费 2024-09-12 22:02:19

我建议用 robocopy 而不是 copyxcopy。用作命令或在客户端或服务器上的 GUI 中使用。容忍网络暂停,并且您可以选择在按文件属性复制副本时忽略文件属性。哦,它支持多核机器,因此文件彼此“并行”复制而不是顺序复制要快得多。 robocopy 可以在 MS TechNet 上找到。

I suggest robocopy instead of copy or xcopy. Used as command or in GUI on clients or servers. Tolerant of network pauses and you can choose to ignore file attributes when copying of copy by file attributes. Oh, and it supports multi-core machines so files are copied much faster in "parallel" with each other instead of sequentially. robocopy can be found on MS TechNet.

执手闯天涯 2024-09-12 22:02:19

对于复制大文件,带有 /J 开关的 xopy 是一个不错的选择。在这种情况下,只需通过管道传输 F 表示文件或使用 D 表示目录。此外,您可以将作业保存在数组中以供将来参考。例如:

$MyScriptBlock = {
    Param ($SOURCE, $DESTINATION) 
    'F' | XCOPY $SOURCE $DESTINATION /J/Y 
    #DESTINATION IS FILE, COPY WITHOUT PROMPT IN DIRECT BUFFER MODE
}
JOBS +=START-JOB -SCRIPTBLOCK $MyScriptBlock -ARGUMENTLIST $SOURCE,$DESTIBNATION
$JOBS | WAIT-JOB | REMOVE-JOB

感谢 Chand,稍加修改:
https://stackoverflow.com/users/3705330/chand

For duplicating large files, xopy with /J switch is a good choice. In this case, simply pipe an F for file or a D for directory. Also, you can save jobs in an array for future references. For example:

$MyScriptBlock = {
    Param ($SOURCE, $DESTINATION) 
    'F' | XCOPY $SOURCE $DESTINATION /J/Y 
    #DESTINATION IS FILE, COPY WITHOUT PROMPT IN DIRECT BUFFER MODE
}
JOBS +=START-JOB -SCRIPTBLOCK $MyScriptBlock -ARGUMENTLIST $SOURCE,$DESTIBNATION
$JOBS | WAIT-JOB | REMOVE-JOB

Thanks to Chand with a bit modifications:
https://stackoverflow.com/users/3705330/chand

墨离汐 2024-09-12 22:02:19

在目标路径末尾添加星号(*)可以跳过D和F的争议。

示例:

xcopy“compressedOutput.xml”“../../执行
脚本/APIAutomation/Libraries/rerunlastfailedbuild.xml*"

Place an asterisk(*) at the end of the destination path to skip the dispute of D and F.

Example:

xcopy "compressedOutput.xml" "../../Execute
Scripts/APIAutomation/Libraries/rerunlastfailedbuild.xml*"

南风几经秋 2024-09-12 22:02:19

复制文件时使用 copy 而不是 xcopy。

例如
复制“bin\development\whee.config.example”
“TestConnectionExternal\bin\Debug\whee.config”

Use copy instead of xcopy when copying files.

e.g.
copy "bin\development\whee.config.example"
"TestConnectionExternal\bin\Debug\whee.config"

故笙诉离歌 2024-09-12 22:02:19

解决办法,使用重命名...并将其命名为一些隐秘的名称,然后将其重命名为正确的名称

C:

CD "C:\Users\Public\Documents\My Web Sites\AngelFire~Zoe\"

XCopy /D /I / V /Y "C:\Users\Public\Documents\My Web Sites\HostGator ~ ZoeBeans\cop.htm"

任 "cop.htm" "christ-our-passover.htm"

Work Around, use ReName... and Name it some Cryptic Name, then ReName it to its Proper Name

C:

CD "C:\Users\Public\Documents\My Web Sites\AngelFire~Zoe\"

XCopy /D /I /V /Y "C:\Users\Public\Documents\My Web Sites\HostGator ~ ZoeBeans\cop.htm"

Ren "cop.htm" "christ-our-passover.htm"

杀お生予夺 2024-09-12 22:02:19

xcopy 将允许您将单个文件复制到指定的文件夹中,但它不允许您定义目标名称。如果您需要目标名称,只需在复制之前重命名即可。

任“bin\development\whee.config.example”whee.config

xcopy /R/Y“bin\development\whee.config”
“TestConnectionExternal\bin\调试\”

xcopy will allow you to copy a single file into a specifed folder it just wont allow you to define a destination name. If you require the destination name just rename it before you copy it.

ren "bin\development\whee.config.example" whee.config

xcopy /R/Y "bin\development\whee.config"
"TestConnectionExternal\bin\Debug\"

来世叙缘 2024-09-12 22:02:19

当使用单个文件时,我使用这两个命令。

要将文件复制到另一个现有目录,请使用copy

copy srcPath\srcFile existingDir\newFile

要将现有文件复制到创建新目录,请使用xcopy

xcopy srcPath\srcFile newDirectoryPath\newFile

要抑制 xcopy “文件或目录”提示,请在响应中回显。因此,对于文件复制,请在 f 中回显。

echo f | xcopy srcPath\srcFile newDirectoryPath\newFile

注意标志 /y 在这两个命令中都起作用,以抑制确认覆盖现有目标文件。


MS 文档:复制xcopy

When working with single files , I use both commands.

To copy a file to another existing directory, use copy

copy srcPath\srcFile existingDir\newFile

To copy an existing file to and create new directories, use xcopy

xcopy srcPath\srcFile newDirectoryPath\newFile

To suppress the xcopy 'file or directory' prompt, echo in the response. So for a file copy echo in f.

echo f | xcopy srcPath\srcFile newDirectoryPath\newFile

Note flag /y works in both commands to suppress the confirmation to overwrite the existing destination file.


MS Docs: copy, xcopy

哽咽笑 2024-09-12 22:02:19

从 Windows 11 22H2(或围绕该版本)开始,MS 添加了 /-I 开关来自动执行“F = 文件”答案。
这就是OP多年前理所当然地期望存在的东西。

As of Windows 11 22H2 (or around that build) MS added the /-I switch to automate the "F = file" answer.
This is what the OP rightfully expected to exist all those years ago.

欢烬 2024-09-12 22:02:19

由于您实际上并未更改文件名,因此您可以从目标位置取出文件名,不会有任何问题。

xcopy bin\development\whee.config.example TestConnectionExternal\bin\Debug\  /Y

当保证目标目录存在并且源同样可以是文件或目录时,此方法效果很好。

Since you're not actually changing the filename, you can take out the filename from the destination and there will be no questions.

xcopy bin\development\whee.config.example TestConnectionExternal\bin\Debug\  /Y

This approach works well when the destination directory is guaranteed to exist, and when the source may equally be a file or directory.

丶视觉 2024-09-12 22:02:19

xxxxxxxxxxxx 是否指定文件名
或目标上的目录名称

(F = file, D = directory)? D

if a File : (echo F)
if a Directory (echo D)

Does xxxxxxxxxxxx specify a file name
or directory name on the target

(F = file, D = directory)? D

if a File : (echo F)
if a Directory (echo D)
摘星┃星的人 2024-09-12 22:02:19

您不能指定它始终是一个文件。如果您不需要 xcopy 的其他功能,为什么不直接使用常规 copy 呢?

You cannot specify that it's always a file. If you don't need xcopy's other features, why not just use regular copy?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文