Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XCOPY still asking (F = file, D = directory) confirmation

My batch script xcopy is still asking F = file, D = directory confirmation even though I have added /F in the script, the log is showing as below. Please help on how to avoid asking confirmation.

Script:

net use p: /delete   net use p: "\\200clan\F_Drive" /USER:adm /PERSISTENT:NO-1111 set source=%1 set target=p:/%2  echo %source% %target%  xcopy /S /I /Q /Y /F "%source%" "%target%" 

Log:

C:\test\foldera>xcopy /S /I /Q /Y /F "C:/test/folder1/folder2/logs/154/compareReport_177.html" "p:/Services/WSDLCompare/177_20151116/compareReport_177.html" Does P:\Services\WSDLCompare\177_20151116\UIReport_177.html specify a file name or directory name on the target (F = file, D = directory)?  
like image 718
Testuser Avatar asked Nov 17 '15 08:11

Testuser


People also ask

How do I stop xcopy asking file or directory?

Press F if you want the file or files to be copied to a file. Press D if you want the file or files to be copied to a 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.

Does specify a file or directory xcopy?

By default, xcopy prompts you to specify whether Destination is a file or a directory. Copies directories and subdirectories, unless they are empty. If you omit /s, xcopy works within a single directory. Copies all subdirectories, even if they are empty.

How do I exclude files from xcopy?

Open the text and write the files' name you'd like to exclude in a separate line like this (without bullets) to make xcopy exclude multiple folders. 3. Run the command now: xcopy c:\t1 c:\t2 /EXCLUDE: C:\mybatch\MyExclusion.

Should I use xcopy or robocopy?

Unlike Xcopy, Robocopy is used to mirror or synchronize directories. Robocopy will be able to check the target directory and delete files that are no longer in the main tree, instead of copying all files from one directory to another.


1 Answers

The /I switch (not /F as you mentioned in your question) prevents xcopy from asking whether the destination is a file or a directory only if multiple source files are given, so if the source is a directory, or if wildcards ? or * are used. If the destination already exists, such prompt does never appear.

There are the following scenarios (depending on the provided values of %source% and %target%):

  • a single source file, the destination is a file:

    the /I switch is useless, so you need to pipe F into the xcopy command line:

    echo F|xcopy /S /Q /Y /F "%source%" "%target%" 

    provided that the /Y switch is given (to force overwriting), you could also create the target file in advance (empty file):

    >> "%target%" rem/ xcopy /S /Q /Y /F "%source%" "%target%" 
  • a single source file, the destination is a directory:

    the /I switch is useless too; you can pipe D into the xcopy command line:

    echo D|xcopy /S /Q /Y /F "%source%" "%target%" 

    or you can simply append a \ to the destination:

    xcopy /S /Q /Y /F "%source%" "%target%\" 

    although this causes trouble when %target% specifies the current directory of a drive like D: for instance, because D: means the current directory of this drive whereas D:\ means the root directory of it;

    or you create the destination directory in advance:

    2> nul mkdir "%target%" xcopy /S /Q /Y /F "%source%" "%target%" 

    the 2> nul portion suppresses the error message in case the directory already exists;

  • multiple source files, the destination is a file:

    this is usually a senseless situation, because you tell xcopy to copy each source file to the same destination file, thus attempting to overwrite it;

  • multiple source files, the destination is a directory:

    the /I switch makes sense here:

    xcopy /S /I /Q /Y /F "%source%" "%target%" 

    the pipe option also works here:

    echo D|xcopy /S /Q /Y /F "%source%" "%target%" 

    so does appending a \ to the destination (regarding the limitation as mentioned above):

    xcopy /S /Q /Y /F "%source%" "%target%\" 

    or you create the destination directory in advance:

    2> nul mkdir "%target%" xcopy /S /Q /Y /F "%source%" "%target%" 

Conclusion

The most flexible and secure solution is to pipe the desired selection (F or D) into the xcopy command line. (Note that the query is locale-dependent.)


Supplement

There are some minor issues in your code fragment I want to mention here:

  • you should generally use the \ as a path separator as this is the Windows standard character for that purpose (although / works too in most cases);
  • there is -1111 appended to your second net use command line; if this constitutes the password for the resource, it should be moved before the /USER option; otherwise just remove it;
  • your set command lines introduce problems with some special characters (like &, ^, (, )); to avoid such, state set "source=%~1" and set "target=p:/%~2"; the ~ removes potential surrounding "" from the arguments (which are required if they contain SPACE, ,, ;, =);

Here is the code with the all of the above things reworked:

net use P: /DELETE rem supposing `-1111` constitutes the password for the resource: net use P: "\\200clan\F_Drive" -1111 /USER:adm /PERSISTENT:NO  set "source=%~1" set "target=P:\%~2"  echo "%source%" "%target%"  rem supposing the destination is a directory: echo D|xcopy /S /I /Q /Y /F "%source%" "%target%"   rem actually you do not need any interim variables: REM echo D|xcopy /S /I /Q /Y /F "%~1" "P:\%~2" 
like image 94
aschipfl Avatar answered Oct 13 '22 05:10

aschipfl