2017-08-14 86 views
0

因此,在构建的批处理脚本中,我从文件夹中取出单个文件,将其复制到目标文件夹,并根据次数重命名该脚本已被循环。基本上我需要从多个计算机上分布的不同文件夹命名为相同的文件,并将它们复制到一个新的文件夹中进行处理。我已经阅读了xcopy和copy,因为它看起来像是要使用的东西,但我一直无法找到任何让我告诉它只复制一个命名文件的东西。我已为我有什么迄今下面的章节注释行脚本我还没有想通了:对多个文件夹中的单个文件使用xcopy或copy

ECHO off 
SETLOCAL enabledelayedexpansion 

ECHO Note: Your combined permission list cvs can be found in the desktop folder 

SET /A #=-1 
:start 

SET /A #+=1 

:again  
ECHO Please input the file path to the permissionoutput.txt 
SET /p permissionoutputpath= 

SET "sourcefolder=%permissionoutputpath%" 
SET "destinationfolder=C:\Users\kayla\Desktop\HOLDER-CombinedPermissionsLists" 
IF not exist "%sourcefolder%\permissionoutput.txt" Echo file not found&goto again 
copy "%sourcefolder%\permissionoutput.txt" "%destinationfolder%\permissionoutput%#%.txt" 

ECHO Add another file to combine: y or n? 
SET /p addanotherfile= 
if %addanotherfile%==y goto :start 

UPDATE:代码更正答案的全部功能,用作参考

回答

1
SET /A #=-1 
:start 

SET /A #+=1 

:again  
ECHO Please input the file path to the permissionoutput.txt 
SET /p permissionoutputpath= 

SET "sourcefolder=%permissionoutputpath%" 
SET "destinationfolder=C:\Users\kayla\Desktop\HOLDER-CombinedPermissionsLists" 
IF not exist "%sourcefolder%\permissionoutput.txt" Echo file not found&goto again 
copy "%sourcefolder%\permissionoutput.txt" "%destinationfolder%\permissionoutput%#%.txt" 

ECHO Add another file to combine: y or n? 
SET /p addanotherfile= 
if /i "%addanotherfile%"=="y" goto start 

#是一个合法的变量名。它初始化为-1然后通过:start开始,每个循环,使第一个值就会产生时,它的使用为0(如果你想在1开始只是将其初始化到0代替)

下一页 - 你sets - 但是空格在字符串中有效set命令将包含在set指令中指定的变量名/值中。 “引用分配”确保线上的任何零散空格不包含在分配的值中。

好吧 - 接下来,确保文件存在,如果不存在,则生成一条消息并循环回:again,它绕过#的增量。

否则,只需复制文件即可。你知道它的源名,并且你的目标名是通过包含%#%来包含当前值#(所有的批量变量都是字符串 - set /a指令仅仅将字符串转换为二进制来执行所需的计算,然后转换返回字符串以存储在环境中。)

最后,解释添加另一个文件的请求。 if /i使比较不区分大小写。由于您无法直接控制用户的回复,因此“引用每一方”可确保在用户输入“确定'nuff'或其他意外的响应时不会违反if语法。

前导冒号是而不是需要在goto。我宁愿忽略它以保持与call命令一致,其中不冒号表示将调用外部例程,并且冒号意味着该例程位于该批处理文件中。

相关问题