11 Feb 2018

cmd提取文件

介绍:
提取指定的文件,进行系统增量部署。
脚本为windows平台下的bat文件

新建源文件:source\12\2.txt , source\1.txt
创建目标文件夹:target

1. 按文件路径提取文件

  1. 新建文件存放要提取的文件 files.txt
    1.txt
    12\2.txt
    
  2. 新建脚本文件 copyToTarget.bat
    @echo off  
    cls  
    @echo [根据文件路径拷贝文件]  
    Title [根据文件路径拷贝文件]
         
    set file=files.txt  
    set isExist=0  
    set bakDir=target
    set source=source
         
    for /f %%i in (%file%) do (  
        if not exist %source%/%%i (  
            echo %source%/%%i[不存在]      
            set isExist=1       
        )  
    )
    if %isExist%==1 (  
        choice /c YN /M "有文件不存在,是否继续?"  
        if errorlevel==2  ( exit )      
        if errorlevel==1 ( goto doCopy )
    )
         
    :doCopy    
    for /f "tokens=1,2 delims=" %%i in (%file%) do (
       if not exist %bakDir%\%%i (  
            md %bakDir%\%%i      
        )
       echo f | xcopy %source%\%%i %bakDir%\%%i /s /y /f     
    )
       
    pause::[提示按任意键结束...]  
    
  3. 执行脚本 结果:在目标文件夹复制了指定的文件

2. 更新已有的文件

  1. 创建脚本 updateTargetFile.bat
    @echo off
    set "source=source"
    set "target=target"
    xcopy %source% %target% /u  /s /y
    pause
    
  2. 执行脚本 结果:源目标文件夹下已存在的文件已被覆盖

Tags: