XCOPY 仍在询问(F = 文件,D = 目录)确认 [英] XCOPY still asking (F = file, D = directory) confirmation
问题描述
我的批处理脚本 xcopy
仍然要求 F = file, D = directory
确认,即使我在脚本中添加了 /F
,日志显示如下.请帮助说明如何避免要求确认.
脚本:
net use p:/deletenet use p: "\200clanF_Drive"/USER:adm/PERSISTENT:NO-1111设置源=%1设置目标=p:/%2回声%源%%目标%xcopy/S/I/Q/Y/F "%source%" "%target%"
日志:
<块引用>C: estfoldera>xcopy/S/I/Q/Y/F "C:/test/folder1/folder2/logs/154/compareReport_177.html" "p:/Services/WSDLCompare/177_20151116/compareReport_177.html"P:ServicesWSDLCompare177_20151116UIReport_177.html 是否指定文件名或目标上的目录名称(F = 文件,D = 目录)?
/I
开关(不是您在问题中提到的 /F
)阻止 xcopy
仅当给出多个源文件时才询问目标是文件还是目录,所以如果源是目录,或者如果通配符?
或*代码>被使用.如果目的地已经存在,则不会出现此类提示.
有以下场景(取决于%source%
和%target%
的提供值):
单个源文件,目标是一个文件:
/I
开关没用,所以你需要将F
通过管道传送到xcopy
命令行: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%"
或者您可以简单地将
附加到目的地:
xcopy/S/Q/Y/F "%source%" "%target%"
虽然这会在
%target%
指定驱动器的当前目录时造成麻烦,例如D:
,因为D:
意味着此驱动器的当前目录,而D:
表示它的根目录;或者你提前创建目标目录:
2>空 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%"
将
附加到目的地也是如此(关于上述限制):
xcopy/S/Q/Y/F "%source%" "%target%"
或者你提前创建目标目录:
2>空 mkdir "%target%"xcopy/S/Q/Y/F "%source%" "%target%"
结论
最灵活和最安全的解决方案是将所需的选择(F
或 D
)通过管道传输到 xcopy
命令行.(请注意,查询依赖于语言环境.)
补充
您的代码片段中有一些小问题,我想在这里提一下:
- 您通常应该使用
作为路径分隔符,因为这是用于此目的的 Windows 标准字符(尽管
/
在大多数情况下也适用); - 有
-1111
附加到您的第二个net use
命令行;如果这构成了资源的密码,则应将其移到/USER
选项之前;否则就删除它; - 您的
set
命令行引入了一些特殊字符问题(例如&
、^
、(
、<代码>)代码>);为了避免这种情况,声明set "source=%~1"
和set "target=p:/%~2"
;~
从参数中删除潜在的包围""
(如果它们包含 SPACE、、
、<代码>;,=
);
以下是重做上述所有内容的代码:
net use P:/DELETErem 假设 `-1111` 构成资源的密码:net use P: "\200clanF_Drive" -1111/USER:adm/PERSISTENT:NO设置源=%~1"设置目标=P:\%~2"echo "%source%" "%target%"rem 假设目的地是一个目录:echo D|xcopy/S/I/Q/Y/F "%source%" "%target%"rem 实际上你不需要任何临时变量:REM echo D|xcopy/S/I/Q/Y/F "%~1" "P:\%~2"
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: "\200clanF_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: estfoldera>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:ServicesWSDLCompare177_20151116UIReport_177.html specify a file name or directory name on the target (F = file, D = directory)?
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 pipeF
into thexcopy
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 pipeD
into thexcopy
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 likeD:
for instance, becauseD:
means the current directory of this drive whereasD:
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 secondnet 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, stateset "source=%~1"
andset "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: "\200clanF_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"
这篇关于XCOPY 仍在询问(F = 文件,D = 目录)确认的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!