windows XCOPY 仍在询问(F = 文件,D = 目录)确认

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33752732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:20:31  来源:igfitidea点击:

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

windowsbatch-filecmdxcopy

提问by Testuser

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

即使我添加了脚本,我的批处理脚本xcopy仍然要求F = file, D = directory确认/F,日志显示如下。请帮助如何避免要求确认。

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\WSDLCompare7_20151116\UIReport_177.html specify a file name
or directory name on the target
(F = file, D = directory)? 
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\WSDLCompare7_20151116\UIReport_177.html specify a file name
or directory name on the target
(F = file, D = directory)? 

回答by aschipfl

The /Iswitch (not /Fas you mentioned in your question) prevents xcopyfrom 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.

/I开关(不/F,你在你的问题中提到)防止xcopy从询问目的地是否是文件或者只在多个源文件中给出的目录,所以如果源是一个目录,或者如果通配符?*使用。如果目的地已经存在,则不会出现此类提示。

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

有以下场景(取决于提供的%source%和值%target%):

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

    the /Iswitch is useless, so you need to pipe Finto the xcopycommand line:

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

    provided that the /Yswitch 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 /Iswitch is useless too; you can pipe Dinto the xcopycommand 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> nulportion 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 xcopyto copy each source file to the same destination file, thus attempting to overwrite it;

  • multiple source files, the destination is a directory:

    the /Iswitch 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%"
    
  • 单个源文件,目标是一个文件:

    /I开关是没用的,所以你需要管Fxcopy命令行:

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

    如果/Y给出了开关(强制覆盖),您还可以提前创建目标文件(空文件):

    >> "%target%" rem/
    xcopy /S /Q /Y /F "%source%" "%target%"
    
  • 单个源文件,目标是一个目录:

    /I开关是无用的太; 您可以通过管道D输入xcopy命令行:

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

    或者您可以简单地将 a 附加\到目的地:

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

    虽然这在%target%指定驱动器的当前目录时会引起麻烦D:,例如,因为D:表示该驱动器的当前目录,而D:\表示它的根目录;

    或者您提前创建目标目录:

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

    2> nul如果目录已经存在,该部分将抑制错误消息;

  • 多个源文件,目标是一个文件:

    这通常是一种毫无意义的情况,因为您告诉xcopy将每个源文件复制到相同的目标文件,从而试图覆盖它;

  • 多个源文件,目标是一个目录:

    /I开关是有道理的位置:

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

    管道选项也适用于这里:

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

    将 a 附加\到目的地也是如此(关于上述限制):

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

    或者您提前创建目标目录:

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

Conclusion

结论

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

最灵活和安全的解决方案是将所需的选择(FD)通过管道传输到xcopy命令行。(请注意,查询是依赖于语言环境的。)



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 -1111appended to your second net usecommand line; if this constitutes the password for the resource, it should be moved before the /USERoption; otherwise just remove it;
  • your setcommand 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, ,, ;, =);
  • 您通常应该将\用作路径分隔符,因为这是用于此目的的 Windows 标准字符(尽管/在大多数情况下也可以使用);
  • -1111附加到第二net use命令线; 如果这构成资源的密码,则应将其移至/USER选项之前;否则就删除它;
  • 您的set命令行会引入一些特殊字符(如&, ^, (, ))的问题;为避免这种情况,请说明set "source=%~1"set "target=p:/%~2";的~消除潜在的周围""从参数(这是必需的,如果它们包含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"

回答by Paul

Put a * behind the target file. This suppress the question and defaults to File.

在目标文件后面放一个 * 。这会抑制问题并默认为文件。

Xcopy file1 file2*

Xcopy file1 file2\ 

defaults to folder.

默认为文件夹。

回答by Dominique

xcopydoesn't know the target is a directory. You clarify this by putting a backslash at the end:

xcopy不知道目标是一个目录。您可以通过在末尾添加反斜杠来澄清这一点:

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

回答by Mizan Shamu

When copying a single file with XCOPY, there is no option to indicate if the destination is a filename or a directory (with the filename defaulting to that of the source file). In such cases XCOPY will prompt with a (locale specific) message like:

使用 XCOPY 复制单个文件时,没有选项指示目标是文件名还是目录(文件名默认为源文件的文件名)。在这种情况下,XCOPY 将提示一条(特定于语言环境的)消息,例如:

C:> xcopy foo.txt bar.txt

C:> xcopy foo.txt bar.txt

it prompts Does foo.txt specify a file name or directory name on the target (F = file, D = directory)?

它提示 foo.txt 是否在目标上指定了文件名或目录名(F = 文件,D = 目录)?

Adding a wildcard (*) to the end of the destination will suppress this prompt and default to copying as a file:

在目标末尾添加通配符 (*) 将取消此提示并默认复制为文件:

C:> xcopy foo.txt bar.txt* 1 File(s) copied

C:> xcopy foo.txt bar.txt* 1 已复制文件

This requires the source and target file extensions to be the same length, typically 3 characters.

这要求源文件扩展名和目标文件扩展名的长度相同,通常为 3 个字符。

for more information: https://ss64.com/nt/xcopy.html

更多信息:https: //ss64.com/nt/xcopy.html

回答by Darth Zman

I use:

我用:

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

It works.

有用。

回答by nhatkhai

Try:

尝试:

echo F>xcopy_answer.tmp
xcopy xcopy /Q/Y "%source%" "%target%"<xcopy_answer.tmp

回答by Divs

Removing the destination filename will suppress the message. This works for me!

删除目标文件名将抑制消息。这对我有用!