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

时间:2016-11-15 22:46:07

标签: batch-file cmd command

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

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

如何将该命令转换为批处理文件?当我将Notepad保存为script.bat并运行时,没有任何事情发生。

2 个答案:

答案 0 :(得分:1)

试试这个:

reboot

答案 1 :(得分:1)

文档(通过在命令提示符下键入for /?help for来解释问题。我强调强调以下部分引用的相关部分。

  

为一组文件中的每个文件运行指定的命令。

     

FOR%变量IN(设置)DO命令[命令参数]

  %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.
  

要在批处理程序中使用FOR命令,请改为指定%%变量   %变量。变量名称区分大小写,因此%i不同   来自%I。

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

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