将简单命令从 cmd 转换为批处理文件

Convert a simple command from cmd to a batch file

我有一个简单的命令,可以将所有 PDF 文件从一个包含子文件夹的文件夹复制到另一个路径:

for /r "c:\users\user\documents" %i in (*.pdf) do copy "%~fi" "d:\alla\%~nxi"

如何将该命令转换为批处理文件?当我将它从 Notepad 保存为 script.bat 和 运行 时,没有任何反应。

试试这个:

@for /r "c:\users\user\documents" %%i in (*.pdf) do @copy "%%~fi" "d:\alla\%%~nxi"

文档(可通过在命令提示符下键入 for /?help for 获得)解释了问题。我已 强调 部分的相关部分引述如下。

Runs a specified command for each file in a set of files.

FOR %variable IN (set) DO command [command-parameters]

  %variable           Specifies a single letter replaceable parameter.
  (set)               Specifies a set of one or more files.  Wildcards may be used.
  command             Specifies the command to carry out for each file.
  command-parameters  Specifies parameters or switches for the specified command.

To use the FOR command in a batch program, specify %%variable instead of %variable. Variable names are case sensitive, so %i is different from %I.

因此,虽然您的命令在命令行中运行,但为了从批处理文件中使用它,您需要将变量前面的 % 符号加倍。

for /r "c:\users\user\documents" %%i in (*.pdf) do copy "%%~fi" "d:\alla\%%~nxi"