回复 1# bat456
功能逻辑暂时不动,for循环内部的命令进行初步优化:
1. 把写日志的操作放到for循环外面
2. 减少call命令的使用
试试这样能否稍微快一点点- @echo off
- setlocal enabledelayedexpansion
-
- rem 生成log文件名
- set output=%date:~,4%-%date:~5,2%-%date:~8,2%_%time%.txt
- rem 因为windows不允许文件名里有冒号,所以这里把:换成-
- set output=%output::=-%
- set output=%output: =%
-
- rem 调用函数完成功能,5个参数分别是源目录,目标目录,用户名,密码,log文件名
- call :s 源地址 目标地址 用户 密码 %output%
- echo 文件已备份完成,30秒后自动关闭窗口
- ping -n 30 127.0.0.1>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!\rizhi\%5
-
- rem 创建目录
- md !dst_path!\rizhi
-
- rem 复制前关掉xx1.exe和xx2.exe进程
- taskkill /f /im xx1.exe /im xx2.exe
-
- rem 自动登录局域网电脑(不映射盘),先重启explorer
- net use !dst_path! /user:!username! !password!
- taskkill /f /im explorer.exe
- start explorer.exe
-
- rem 复制前能进行对比,已有的文件一模一样无更新的·不复制。反之替换掉!
- (for /f "delims=" %%f in ('dir /b /s /a-d !src_path!') do (
- rem 遍历所有子目录,获得文件%%f,然后将%%f中的原路径替换成目标路径,便于比较文件
- set cur_file="%%f"
- REM call set dst_file=%%cur_file:%src_path%=%dst_path%%%
- set pathb=%%~dpf
- REM call set dst_path=%%pathb:%src_path%=%dst_path%%%
- set dst_path=!pathb:%src_path%=%dst_path%!
-
- rem 创建子目录
- if not exist !dst_path! (md !dst_path!)
-
- rem 这里是正式覆盖,并且写入log文件
- REM echo xcopy /d /y !cur_file! !dst_path!
- set need_proc=0
- for /f %%i in ('xcopy /d /y !cur_file! !dst_path!^|findstr "复制了 1 个文件"') do (
- set need_proc=1
- )
- if !need_proc!==1 (echo !cur_file!)
- ))>"!log_file!"
-
- :eof
复制代码
|