[Bash shell] 纯文本查看 复制代码@echo off&setlocal enabledelayedexpansion
@rem 生成log文件名
set output=%date:~,4%-%date:~5,2%-%date:~8,2%_%time%.txt
set output=%output::=-%
set output=%output: =%
@rem 调用函数完成功能,5个参数分别是源目录,目标目录,用户名,密码,log文件名
@rem 因为windows不允许文件名里有冒号,所以这里把:换成-
call :s D:\我的文件 \\back-server\RenShiBu username password %output::=-%
@echo 文件已备份完成,30秒后自动关闭窗口
choice /t 30 /d y /n >nul
goto :eof
:s
@echo %1 %2 %3 %4 %5
set src_path=%1
set dst_path=%2
set username=%3
set password=%4
@rem log生成在目标路径
set log_file=!dst_path!\%5
@rem 复制前关掉xx1.exe和xx2.exe进程
TASKKILL /IM xx1.exe
TASKKILL /IM xx2.exe
@rem 自动登录局域网电脑(不映射盘),先重启explorer
taskkill /f /im explorer.exe
explorer.exe
net use !dst_path! /user:!username! !password!
@rem 生成log文件
@echo>!log_file!
@rem 复制前能进行对比,已有的文件一模一样无更新的·不复制。反之替换掉!
for /f "delims=" %%f in ('dir /b/s/a-d^ !src_path!') do (
@rem 遍历所有子目录,获得文件%%f,然后将%%f中的原路径替换成目标路径,便于比较文件
set cur_file="%%f"
call set dst_file=%%cur_file:%src_path%=%dst_path%%%
set pathb=%%~dpf
call set dst_path=%%pathb:%src_path%=%dst_path%%%
@rem 创建子目录
@md !dst_path!
@rem 如果文件不存在,则将need_proc设为2,表示需要覆盖。如果设为1,表示文件存在需要比较内容
set need_proc=0
if exist !dst_file! (set need_proc=1) else (set need_proc=2)
if !need_proc!==1 (
@rem 比较文件二进制,如果输出信息里有"找不到差异"就表示文件相同,否则需要覆盖
@echo compare !cur_file! !dst_file!
set need_proc=2
for /F %%i in ('fc /b !cur_file! !dst_file!^|findstr 找不到差异') do (
set need_proc=1
)
)
@rem 这里是正式覆盖,并且写入log文件
if !need_proc!==2 (
@echo copy /y !cur_file! !dst_file!
copy /y !cur_file! !dst_file!
@echo !cur_file!>>!log_file!
)
)
:eof