Xcopy将从批处理脚本的路径中赢得了副本

发布于 2025-01-25 02:28:52 字数 215 浏览 3 评论 0原文

我有一个Intune软件包,该软件包运行批处理脚本如下。

SET INST=%~dp0

xcopy %INST%\Folder1\* "C:\Program Files (x86)\Test\Folder2" /s /i

目标是将文件从文件夹1复制到文件夹2。批处理脚本和文件夹1位于同一主文件夹中。没有复制文件,所以我很困惑。

谢谢,

I have an intune package which runs a batch script as follows.

SET INST=%~dp0

xcopy %INST%\Folder1\* "C:\Program Files (x86)\Test\Folder2" /s /i

Goal is to copy files from Folder1 to Folder2. The Batch script and folder 1 are in the same main folder. No files are being copied so I'm confused.

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

指尖上的星空 2025-02-01 02:28:53

两条命令行可以用单个命令行替换:

%SystemRoot%\System32\xcopy.exe "%~dp0Folder1" "%ProgramFiles(x86)%\Test\Folder2\" /S

带有此命令行的批处理文件需要作为管理员运行,因为使用>%programFiles(x86)%通常是针对标准用户写的。

在命令提示符窗口中运行cmd /?< /code>运行cmd /?< /code>的帮助输出在最后一个帮助页面上说明了一个文件名(或任何其他参数字符串(如密码),其中包含一个空间或这些字符之一&amp;()[] {}^=;!'+,`〜(或从字面上解释&lt;&gt; |(如密码中)必须包含在要被解释为一个参数字符串,在该字面上,所有字符均在其上被解释,但>% and和!如果延迟在批处理文件中解析命令行启用了扩展。 SystemRoot%\ System32 \ Xcopy.exe 。

Windows命令 Xcopy 用完全合格的文件名指定。这提高了效率,因为Windows命令处理器无需在文件系统中搜索具有文件名的可执行文件/脚本xcopy在当前目录中以及列出的目录中与本地环境值的半olons分隔的目录代码>路径带有在本地环境变量的值中列出的文件扩展名pathext。完全合格的文件名的使用使该命令行也使该命令行更安全,因为当前目录中没有xcopy.cmd或损坏的path不再包含%SystemRoot %\ System32可能会导致此命令行的执行方式与预期的命令行的执行不同。

%〜DP0folder1之间没有反斜击批处理文件以\结尾。因此,%〜dp0folder1的结果参数字符串是100%有效的,不得通过&nbsp; the Windows file i/o函数在将目录参数字符串传递到文件系统之前。

可以将附加到源参数字符串\*,即使用“%〜dp0folder1 \*”作为 xcopy 的第一个参数字符串,但要复制默认情况下,指定源目录中的所有文件以及Option /s的所有文件都是非空目录中的所有文件。

目标参数字符串以后斜线结束。这使得 Xcopy 的100%清楚,目的地是目录。最终的后斜线使得不需要使用选项/IXcopy 始终将整个目录树创建到目标目录。目的地绝对是一个目录,其目的参数字符串以后斜击结束。

由于Windows Vista和Windows Server 2003在Windows System Directory中默认安装了 Robocopy ,因此使用 Xcopy 被弃用。 Robocopy 是一个更强大,更强大的文件/目录复制/移动程序。在命令提示符窗口robocopy/?以输出其使用帮助或读取 Robocopy

可以完成相同的目录复制任务

%SystemRoot%\System32\robocopy.exe "%~dp0Folder1" "%ProgramFiles(x86)%\Test\Folder2" /S /NDL /NFL /NJH /NJS /R:2 /W:3 >nul

可以使用 Robocopy 使用: Robocopy 也可以创建整个目的地目录树(如果需要),

。重要的是要提到robocopy.exe使用特殊参数字符串解析,例如reg.exe。 a&nbsp; \剩下一个\或a ”被解释为以下后背或双引号字符的逃生字符。因此 Robocopy 中的参数字符串“”应以单个后斜线结尾,因为这将被解释为双引号的逃生,因此所有内容to to to next '解释为一个参数字符串,尽管目录路径根本不能包含字符

有效的 Robocopy 命令行与源和目的地有关:

%SystemRoot%\System32\robocopy.exe "%~dp0Folder1" "%ProgramFiles(x86)%\Test\Folder2" /S
%SystemRoot%\System32\robocopy.exe "%~dp0Folder1\\" "%ProgramFiles(x86)%\Test\Folder2\\" /S

无效的 Robocopy 命令行的源和目的地:

%SystemRoot%\System32\robocopy.exe %~dp0Folder1 %ProgramFiles(x86)%\Test\Folder2 /S
%SystemRoot%\System32\robocopy.exe "%~dp0Folder1\" "%ProgramFiles(x86)%\Test\Folder2\" /S

第一行无效,因为>%〜DP0 可以将其扩展到包含空间的字符串或这些字符&amp;()[] {}^=;! /code>默认情况下扩展到包含A&nbsp;空间和圆括号的字符串,因此必须以双引号封闭源和目标路径。

如果驱动器的根目录是必要的目录路径以后斜线结束的源或目的地,则最好不要像c:\那样封闭root目录路径D:\在双引号中。

The two command lines could be replaced by the single command line:

%SystemRoot%\System32\xcopy.exe "%~dp0Folder1" "%ProgramFiles(x86)%\Test\Folder2\" /S

The batch file with this command line needs to be run as administrator because of the directory referenced with %ProgramFiles(x86)% is usually write-protected for standard users.

The help output on running cmd /? in a command prompt window explains on last help page that a file name (or any other argument string like a password) containing a space or one of these characters &()[]{}^=;!'+,`~ (or literally to interpret <>| as in a password) must be enclosed in " to be interpreted as one argument string on which all characters are interpreted literally with the exception of % and ! if delayed expansion is enabled on parsing the command line in the batch file. It is advisable to enclose file/folder argument strings always in double quotes if there is not guaranteed that " are not needed as for %SystemRoot%\System32\xcopy.exe.

The Windows command XCOPY is specified with fully qualified file name. That improves efficiency as the Windows Command Processor does not need to search in file system for an executable/script with file name xcopy in current directory and in the directories listed separated with semicolons in value of local environment variable PATH with a file extension listed in value of local environment variable PATHEXT. The usage of the fully qualified file name makes this command line also fail safe because of no xcopy.cmd in current directory or a corrupted PATH not containing anymore %SystemRoot%\System32 can cause anymore a different execution of this command line than the expected one.

There is no backslash between %~dp0 and Folder1 in source argument string because of %~dp0 always expands to full path of the directory containing the batch file ending with \. So the resulting argument string of %~dp0Folder1 is 100% valid which must not be modified in any way by the Windows file I/O functions before passing the directory argument string to the file system.

There can be appended to source argument string \*, i.e. use "%~dp0Folder1\*" as first argument string for XCOPY, but copying all files in the specified source directory, and with option /S also all files in non-empty directories, is the default.

The destination argument string ends with a backslash. That makes it 100% clear for XCOPY that the destination is a directory. That backslash at end makes it unnecessary to use option /I. XCOPY creates always the entire directory tree to the destination directory. The destination is definitely a directory with destination argument string ending with a backslash.

The usage of XCOPY is deprecated since Windows Vista and Windows Server 2003 on which ROBOCOPY is installed by default in the Windows system directory. ROBOCOPY is a more robust and more powerful file/directory copying/moving program. Run in a command prompt window robocopy /? for output of its usage help or read the Microsoft documentation for robocopy.

The same directory copying task can be done with ROBOCOPY with:

%SystemRoot%\System32\robocopy.exe "%~dp0Folder1" "%ProgramFiles(x86)%\Test\Folder2" /S /NDL /NFL /NJH /NJS /R:2 /W:3 >nul

ROBOCOPY creates also the entire destination directory tree if that is necessary.

It is important to mention that robocopy.exe uses a special argument string parsing like reg.exe. A \ left to one more \ or a " is interpreted as escape character for the following backslash or double quote character. For that reason no argument string of ROBOCOPY enclosed in " should end with a single backslash as that would be interpreted as escape for the double quote and so everything up to next " is interpreted as one argument string although a directory path cannot contain the character " at all.

Valid ROBOCOPY command lines are regarding to source and destination:

%SystemRoot%\System32\robocopy.exe "%~dp0Folder1" "%ProgramFiles(x86)%\Test\Folder2" /S
%SystemRoot%\System32\robocopy.exe "%~dp0Folder1\\" "%ProgramFiles(x86)%\Test\Folder2\\" /S

Invalid ROBOCOPY command lines are regarding to source and destination:

%SystemRoot%\System32\robocopy.exe %~dp0Folder1 %ProgramFiles(x86)%\Test\Folder2 /S
%SystemRoot%\System32\robocopy.exe "%~dp0Folder1\" "%ProgramFiles(x86)%\Test\Folder2\" /S

The first line is invalid as %~dp0 could expand to a string containing a space or one of these characters &()[]{}^=;!'+,`~ and %ProgramFiles(x86)% expands by default to a string containing a space and both round brackets and therefore source and destination path must be enclosed in double quotes.

If the root directory of a drive is the source or destination on which it is necessary that the directory path ends with a backslash, it is best not enclosing the root directory path like C:\ or D:\ in double quotes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文