2

I'm attempting to use xcopy to copy over a file from one machine to another on our network as part of a Java program. I'm calling xcopy like this:

xcopy "C:\Program Files\path\to\my\file" "\\othermachine\c$\Documents and Settings\<myUserName>\Desktop\Test\path\in\directory\structure\to\file" /e /y /i

Because I'm calling it from with in Java, I need all the prompts to be suppressed. For the most part, \i and \y have done exactly that. However, for this one file /i fails and I get the file or directory prompt. The result is that it hangs the entire program.

I've also tried calling it with /s /t /q appended on to the existing options, to no avail. Why isn't /i working to suppress the File or Directory prompt? Is there an order I need to call the options in? Is there something else I need to do?

EDIT: I should mention, the file is a text file - single line of text. It does not have an extension. It looks like this: FILE-NAME

5 Answers 5

8

echo F does the trick:

echo F | xcopy "C:\Program Files\path\to\my\file" "\\othermachine\c$\Documents and Settings\<myUserName>\Desktop\Test\path\in\directory\structure\to\file" /e /y /i

Note that the character may differ in localized versions (my german one expects D for file (Datei) or V for directory (Verzeichnis))

2
  • 2
    no localization, it is still the same letter in Win 8 rus. However it is annoying. I'd prefer xcopy used /-i option to suppress this stupid question. Jun 19, 2018 at 9:33
  • Unfortunately, "put a * at the end of the destination" only works if the source and destination have a three character extension. (At least, according to SS64.com.) Sorry, this comment was meant for Govert's comment below. echo -f worked for me (Eng US).
    – PReinie
    Aug 22, 2023 at 20:26
6

A seemingly undocumented trick is to put a * at the end of the destination - then xcopy will copy as a file, so

xcopy c:\source\file.txt c:\destination\newfile.txt*

The echo f | xcopy ... trick does not work on localized versions of Windows, where the prompt is different.

1
  • echo f at least works on Russian Win 8.1 // PS. The asterisk trick works, I can only wonder who could ever discover it! I bet it is a bug in xcopy that is [ab]used to compensate for the lack of a needed feature Jun 19, 2018 at 9:34
1

This thread on Stackoverflow has no marked answer, but it may help you (seems to be the same question).

1
  • Thanks for the suggestion, but that's a rather different scenario. That's suppressing the terminal itself when calling a batch file. I'm trying to suppress a prompt that xcopy gives. Sep 2, 2009 at 14:30
0

I've always found a useful cheat for these situations to have a text file with a Y in it and tag < y.txt on the end of the xcopy

0

One more possible trick is pre-creation of a target file or folder. Then xcopy would not ask, because it would already know. I would not say this solution is better than ones mentioned above, it is just different and hence I want it to be had here too.

My own simple task was copying a number of files from a network folder to a local accumulating folder.

  • AAAAA\Disk1.zip => AAAAA.zip
  • BBBBB\Disk1.zip => BBBBB.zip
  • et cetera

Original command (one line of a slightly longer .cmd script file) was

for /d /r %SRC% %%1 in (*.*) do xcopy /k /f /y %%1\Disk1.zip %~dp0%%~nx1.zip

That however spawns a lot of those (F)ile-or(D)-irectory questions on a first use with new (not met before) files. Especially if I am populating a new empty target folder.

Then the echo F and Asterisk Hack answers - they work fine too.

  • for /d /r %SRC% %%1 in (*.*) do echo f | xcopy /k /f /y %%1\Disk1.zip %~dp0%%~nx1.zip
  • for /d /r %SRC% %%1 in (*.*) do xcopy /k /f /y %%1\Disk1.zip %~dp0%%~nx1.zip*

Now, yet one more potential solution would be explicitly CREATING the target folder of file right before issuing the xcopy command.

For file it would be, for example:

echo. > C:\Temp\TargetPlace
xcopy Source C:\Temp\TargetPlace

Sadly Windows by default does not have touch command, quite useful to create dummy 0-bytes file.

For folder it would be, for example:

mkdir C:\Temp\TargetPlace
xcopy Source C:\Temp\TargetPlace

Granted, it takes two commands - two lines - in CMD shell script. You can not put multiple commands into CMD for loop easily, but here you can pipe the commands together (providing that xcopy would NOT actually read any user input when issued the specific command, which might change for example in future Window versions, albeit unlikely).

For folders:

for /d /r %SRC% %%1 in (*.*) do md %~dp0%%~nx1.zip | xcopy /k /f /y %%1\Disk1.zip %~dp0%%~nx1.zip

For files:

for /d /r %SRC% %%1 in (*.*) do echo. >%~dp0%%~nx1.zip | xcopy /k /f /y %%1\Disk1.zip %~dp0%%~nx1.zip

This last command has TWO output redirections in between - echo. > ... | xcopy. But at least in .cmd files of Windows 8.1 it works.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .