xcopy 文件、重命名、抑制“xxx 是否指定文件名...”信息
这看起来很简单,也许我只是忽略了 正确的标志,但是我如何在一个命令中将文件从一个目录复制到另一个目录并在目标目录中重命名?这是我的命令:
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
。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(24)
我习惯于
绕过它。
I use
to get around it.
不要使用
xcopy
,而是使用copy
,它没有这个问题。xcopy
通常用于执行多个文件/文件夹的递归复制,或者当您需要它提供的验证/提示功能时。对于单个文件副本,copy
命令工作得很好。Don't use the
xcopy
, usecopy
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, thecopy
command works just fine.另一种选择是使用目标通配符。请注意,这仅在源文件名和目标文件名相同的情况下才有效,因此虽然这不能解决OP的具体示例,但我认为值得分享。
例如:
将在目标目录中创建文件“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:
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 extensionXCOPY 中有某种未记录的功能。你可以使用:
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. :-)
只需访问 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.
因此,有一个简单的解决方法。诚然,这很尴尬,但确实有效。
如果新文件(文件名)已经存在,xcopy 将不会提示查找目标是目录还是文件。如果您在 xcopy 命令之前对新文件名进行简单的回显,它将覆盖空文件。例子
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
仅当文件不存在于目标中或存在(使用新名称)但较旧时,我才尝试使用新名称复制文件时遇到同样的问题。解决方案是在目标文件名末尾添加
*
字符。例子: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:这是比尔的回答。
只是为了让其他人真正清楚。
如果您要将一个文件从一个位置复制到另一个位置并且您希望创建完整的目录结构,请使用以下命令:
是的,在末尾添加反斜杠文件名,它不会询问您它是文件还是目录。因为源中只有一个文件,所以它会假设它是一个文件。
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:
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.
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
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
我遇到了类似的问题,robocopy 和 xcopy 都没有帮助,因为我想抑制注释并使用不同的目标文件名。我发现
工作符合我的要求。
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
working as per my requirements.
回到最初的问题:
可以使用两个命令来完成,例如:
只需将“\..”附加到目标文件的路径,即可创建目标目录(如果目标目录尚不存在)。在本例中
,这是的父目录
不存在的目录
至少对于WIN7 mkdir 不关心该目录是否
真的存在。
Back to the original question:
could be done with two commands eg:
By simply appending "\.." to the path of the destination file the destination directory is created if it not already exists. In this case
which is the parent directory of
the non-existing directory
At least for WIN7 mkdir does not care if the directory
really exists.
如果您只想复制文件并在目的地更改其名称,正确的做法是:
它会正常工作
The right thing to do if you wanna copy just file and change it's name at destination is :
And it's Gonna work fine
我建议用
robocopy
而不是copy
或xcopy
。用作命令或在客户端或服务器上的 GUI 中使用。容忍网络暂停,并且您可以选择在按文件属性复制副本时忽略文件属性。哦,它支持多核机器,因此文件彼此“并行”复制而不是顺序复制要快得多。robocopy
可以在 MS TechNet 上找到。I suggest
robocopy
instead ofcopy
orxcopy
. 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.对于复制大文件,带有 /J 开关的 xopy 是一个不错的选择。在这种情况下,只需通过管道传输 F 表示文件或使用 D 表示目录。此外,您可以将作业保存在数组中以供将来参考。例如:
感谢 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:
Thanks to Chand with a bit modifications:
https://stackoverflow.com/users/3705330/chand
在目标路径末尾添加星号(*)可以跳过D和F的争议。
示例:
Place an asterisk(*) at the end of the destination path to skip the dispute of D and F.
Example:
复制文件时使用 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"
解决办法,使用重命名...并将其命名为一些隐秘的名称,然后将其重命名为正确的名称
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"
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\"
当使用单个文件时,我使用这两个命令。
要将文件复制到另一个现有目录,请使用
copy
要将现有文件复制到并创建新目录,请使用
xcopy
要抑制
xcopy
“文件或目录”提示,请在响应中回显。因此,对于文件复制,请在f
中回显。注意标志
/y
在这两个命令中都起作用,以抑制确认覆盖现有目标文件。MS 文档:复制,xcopy
When working with single files , I use both commands.
To copy a file to another existing directory, use
copy
To copy an existing file to and create new directories, use
xcopy
To suppress the
xcopy
'file or directory' prompt, echo in the response. So for a file copy echo inf
.Note flag
/y
works in both commands to suppress the confirmation to overwrite the existing destination file.MS Docs: copy, xcopy
从 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.
由于您实际上并未更改文件名,因此您可以从目标位置取出文件名,不会有任何问题。
当保证目标目录存在并且源同样可以是文件或目录时,此方法效果很好。
Since you're not actually changing the filename, you can take out the filename from the destination and there will be no questions.
This approach works well when the destination directory is guaranteed to exist, and when the source may equally be a file or directory.
xxxxxxxxxxxx 是否指定文件名
或目标上的目录名称
Does xxxxxxxxxxxx specify a file name
or directory name on the target
您不能指定它始终是一个文件。如果您不需要 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
?