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

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 \调试\哟。配置指定文件名 或目标上的目录名(F =文件,D =目录)?

我想压制这个提示;答案总是F。


当前回答

回到最初的问题:

 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\"

真的存在。

其他回答

所以,有一个简单的解决办法。这是公认的尴尬,但它有效。 如果新文件(文件名)已经存在,xcopy将不会提示查找目标是否为目录或文件。如果在xcopy命令前加上对新文件名的简单回显,它将覆盖空文件。例子

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

另一种选择是使用目的地通配符。注意,这仅适用于源文件名和目标文件名相同的情况,所以虽然这不能解决OP的特定示例,但我认为值得分享。

例如:

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

将创建文件" wheel .config. conf "的副本。在目标目录中,不提示输入文件或目录。

更新:正如@chapluck提到的:

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

回到最初的问题:

 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\"

真的存在。

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

Xcopy /f /y "bin\development\example.exe" “TestConnectionExternal \ bin \调试\ NewName.exe *”

一切都会好的

工作周围,使用重命名…并命名它一些神秘的名字,然后重命名它的正确名称

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"

纯粹的“警察.htm”“基督-我们的逾越节.htm”