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:50

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

查看更多
做个烂人
3楼-- · 2019-01-05 07:51

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

查看更多
4楼-- · 2019-01-05 07:52

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.

查看更多
太酷不给撩
5楼-- · 2019-01-05 07:53

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
查看更多
不美不萌又怎样
6楼-- · 2019-01-05 07:54

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.

查看更多
来,给爷笑一个
7楼-- · 2019-01-05 07:56

Use copy instead of xcopy when copying files.

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

查看更多
登录 后发表回答