Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
File 如何使用批处理文件在文件夹中循环_File_Batch File_Xcopy - Fatal编程技术网

File 如何使用批处理文件在文件夹中循环

File 如何使用批处理文件在文件夹中循环,file,batch-file,xcopy,File,Batch File,Xcopy,我需要用批处理文件做点什么 我需要将一个文件夹复制到另一个文件夹,但是 如果新文件夹存在,我需要验证新文件夹中的文件是否存在,然后在复制新文件之前,我需要在此文件末尾使用«.old»重命名该文件。 我有很好的Java、php等编程经验,但并不真正使用批处理文件 我正在使用java/php语法来解释我的问题 set folderOrigin=d:\test1 set folderFinal=d:\test5 if EXIST %folderFinal% ( for (fileOrigin :

我需要用批处理文件做点什么

我需要将一个文件夹复制到另一个文件夹,但是

如果新文件夹存在,我需要验证新文件夹中的文件是否存在,然后在复制新文件之前,我需要在此文件末尾使用«.old»重命名该文件。 我有很好的Java、php等编程经验,但并不真正使用批处理文件

我正在使用java/php语法来解释我的问题

set folderOrigin=d:\test1
set folderFinal=d:\test5 
if EXIST %folderFinal% (
  for (fileOrigin : folderOrigin){
      variableNamefileOrigin = fileOrigin
      for (fileFinal : folderFinal){
          variableNamefileFinal = fileFinal            
          if (variableNamefileOrigin == variableNamefileFinal){
              newvariable = variableNamefileFinal + ".old"
              ren variableNamefileFinal newvariable
              xcopy /s /q %folderOrigin%+%variableNamefileOrigin% 
              %folderFinal%+%variableNamefileFinal% 
          }
      }
  }
) else (
    xcopy /s /q %dossierOrigine% %dossierDestinataire%
)
pause

谢谢大家

假设源目录(“文件夹”是GUI中的工件;文件系统中的结构是“目录”)是
%sourceFolder%
,目标目录是
%finalFolder%
;并且还假设您只需要复制文件(而不是整个子树):

  • 要循环浏览
    %sourceFolder%
    中的文件,请使用
    for
    循环:

    for %%f in ("%sourceFolder%\*") do call :copyOneFile "%%~f"
    exit /b
    
  • 在子例程
    :copyOneFile
    中,当前文件为
    %1
    。要检查它是否存在于
    %finalFolder%
    中,请使用
    if exist
    ,如果存在,请重命名它,但在检查
    .old
    文件是否已经存在之前,请执行以下操作:

    :copyOneFile
    
    if exist "%finalFolder%\%~nx1" (
      if exist "%finalFolder\%~n1.old" del "%finalFolder\%~n1.old"
      ren "%finalFolder\%~nx1" "%~n1.old"
    )
    
  • 现在,您可以将文件从源文件夹复制到目标文件夹:

    copy "%~1" "%finalFolder%
    
  • 要理解结构
    %~nx1
    等,请使用
    for/?
    。注意,
    ren
    的第二个参数必须只有文件名,不允许有路径

    如果需要复制整个子树,则:

    • 复制文件后,使用
      for/d
      重做以获取目录

    • 使用适当的命令,而不是
      del
      copy


    您读过什么帮助文件说您可以使用花括号?大多数命令的语法都不正确。打开命令提示符,键入命令名,后跟/?阅读命令的帮助文件。@Squashman也许这行
    我正在使用java/php语法来解释我的问题….
    可能会解释大括号heresy@Magoo说得好。仍然不是不阅读帮助文件以确定正确语法的借口。我使用此语法作为伪代码来解释我的问题。。。我知道帮助…我已经读了一点…但是不容易理解…:)PS:我的英语不是很好,所以我试着写一些清楚的东西来解释我的问题,所以你想让我们根据你的伪代码为你写一个脚本?这不是适合这种要求的地方!您必须自己尝试,当您遇到特定问题时返回!请阅读并学习!