xcopy file, rename, suppress “Does xxx specify a f

2019-01-05 07:33发布

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.

19条回答
手持菜刀,她持情操
2楼-- · 2019-01-05 07:57

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.

查看更多
等我变得足够好
3楼-- · 2019-01-05 07:57

xcopy src dest /I

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

查看更多
Ridiculous、
4楼-- · 2019-01-05 08:00

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

查看更多
爱情/是我丢掉的垃圾
5楼-- · 2019-01-05 08:01

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. :-)

查看更多
够拽才男人
6楼-- · 2019-01-05 08:01

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.

查看更多
疯言疯语
7楼-- · 2019-01-05 08:02

I use

echo f | xcopy /f /y srcfile destfile

to get around it.

查看更多
登录 后发表回答