同步文件夹
复制代码
- @goto start
- :help
- @echo.===== Ver: 1.0 ======== 同步文件夹 ======== 发布日期: 2010-08-09 =======
- @echo. BAT_NAME: synFolder.bat
- @echo. 返回值: [%def_filename%]
- @echo.
- @echo. 调用: synFolder.bat Folder1 [Folder2] [-s [size]] [-o [filename]] [-x]
- @echo. Folder1 : 源文件夹
- @echo. Folder2 : 目标文件夹,如不指定既为调用程序所在文件夹
- @echo. -s [size]: 文件大于(size MB)被忽略;
- @echo. 不选即不限定filesize;
- @echo. [size] 默认10,可不写. (0 ≤整数≤2147)
- @echo. -o [filename]: 保存同步信息到文件(默认:返回值)
- @echo. 不选即不记录文件copy的信息
- @echo. -x : 双向同步,根据源文件夹的文件更新目标文件夹,
- @echo. 两个文件夹都会改变;
- @echo. 但是源文件夹没有的文件不会从目标文件夹copy.
- @echo. -X 双向同步,两个文件夹完全相同;
- @echo. 不选x^|X,单向同步,根据源文件夹里的文件更新目标文件夹,
- @echo. 源文件夹不会变化;
- @echo. -? : 查看帮助
- @echo. 注意: 0字节文件夹不会被建立.
- @echo.====================== Copyright@ by hf-g 2010-08-08 [彭城] ============
- ::ping /n 4 127.0>nul&
- goto:end
- ::==============================================================
- :start
- @echo off
- setlocal enabledelayedexpansion
- ::==============================================================
- set "def_filename=%temp%\ret_List.txt"
- ::echo "%1" & pause
- if /i "%~1"=="" goto help
- if /i "%~1"=="-?" goto help
- set "ret_filename="&set "file_Size="
- set /a size_BL=0,ret_BL=0,x_BL=20
- call:get_P %*
- if "%exit_BL%"=="1" goto end
- if "%desFolder%"=="" set "desFolder=%~dp0"
- if "%ret_BL%"=="1" (if "%ret_filename%"=="" set "ret_filename=%def_filename%")
- if "%size_BL%"=="1" (if "%file_Size%"=="" set "file_Size=10")
- echo %ret_BL%
- echo off
- echo 源文件夹: %sorFolder%
- echo 目标文件夹: %desFolder%
- echo 返回值: %ret_filename%
- echo 文件限制: ^<= %file_Size% MB
- if "%x_BL%"=="21" echo 双向同步: -x 方式1
- if "%x_BL%"=="22" echo 双向同步: -X 方式2
- if "%x_BL%"=="20" echo 单向同步: 源文件夹不会变化
- ::goto end
- ::--------------------------- main -------------------------------
- for /f "skip=4 delims=" %%a in ('reg query "HKEY_CURRENT_USER\Control Panel\International" /v sShortDate') do (set "RegDateOld=%%a")
- set RegDateOld=%RegDateOld:~-8%
- reg add "HKEY_CURRENT_USER\Control Panel\International" /v sShortDate /t REG_SZ /d yyyy-M-d /f>nul
- ::1MB=1024kB=1024*1024Byte=1048576Byte
- if "%size_BL%"=="1" (set /a file_Size=%file_Size% "*" 1048576)
- ::echo %RegDateOld:~-8% %file_Size%
- ::大于等于指定%file_Size%的文件忽略. 同步为最新文件.
- call:get_fpath "%sorFolder%.\"
- set "sor_drive=%drive%"&set "sor_p_path=%p_path%"
- call:get_fpath "%desFolder%.\"
- set "des_drive=%drive%"&set "des_p_path=%p_path%"
- ::@echo on
- ::①
- (if "%ret_BL%"=="1" cd.>"%ret_filename%" 2>nul)&set/a count=0
- for /f "tokens=* delims=" %%a in ('dir /a-d/s/b "%sorFolder%.\"') do (
- if "%size_BL%"=="0" set /a file_Size=%%~za
- if %%~za LEQ !file_Size! (
- set "sorfile=%%a"
- set "sorfile=!sorfile:%%~da%sor_p_path%=!"
- for /f "tokens=* delims=" %%i in ("%desFolder%.\!sorfile!") do (
- if %%~ta GTR %%~ti (
- md "%%~dpi" 2>nul
- copy "%%~a" "%%~i">nul&&(set/a count+=1&set /p =!count!<nul&if "%ret_BL%"=="1" echo copyFile %%~a → %%~i>>"%ret_filename%")
- ) else (
- if %x_BL% GTR 20 (
- if %%~ti GTR %%~ta (
- md "%%~dpa" 2>nul
- copy "%%~i" "%%~a">nul&&(set/a count+=1&set /p =!count!<nul&if "%ret_BL%"=="1" echo copyFile %%~a ← %%~i>>"%ret_filename%")
- ))
- ))))
- @echo off
- ::②
- if %x_BL% == 22 (
- for /f "tokens=* delims=" %%a in ('dir /a-d/s/b "%desFolder%.\"') do (
- if %file_Size% GTR %%~za (
- set "desfile=%%a"
- set "desfile=!desfile:%%~da%des_p_path%=!"
- if not exist "%sorFolder%.\!desfile!" (
- call:get_fpath "%sorFolder%.\!desfile!"
- md "!drive!!p_path!" 2>nul
- copy "%%~a" "%sorFolder%.\!desfile!">nul&&(set/a count+=1&set /p =!count!<nul&if "%ret_BL%"=="1" echo copyFile %sorFolder%.\!desfile! ← %%~a>>"%ret_filename%")
- ))))
- if exist "%ret_filename%" (start notepad.exe "%ret_filename%"&ping/n 2 127.0>nul&del "%ret_filename%" 2>nul)
- ::----------------------------------------------------------------
- reg add "HKEY_CURRENT_USER\Control Panel\International" /v sShortDate /t REG_SZ /d %RegDateOld% /f>nul
- :end
- ::echo ☆☆☆
- del tempfile.txt strHEX.txt>nul 2>nul
- exit /b
- ::====================================================
- :get_fpath [drive:path]
- set "drive=%~d1"
- set "p_path=%~p1"
- goto:eof
- ::====================================================
- ::========================参数设置与获得============================
- :get_p
- echo off
- ::调用: call synFolder.bat Folder1 [Folder2] [-s [size]] [-o [filename]] [-x]
- set "sorFolder=%~1"&set /a count=1
- if not "%~2"=="" (
- set "_p=%~2"
- if not "!_p:~0,1!"=="-" (set "desFolder=%~2"&shift /1&set /a count+=1)
- ) else (
- goto get_p_end
- )
- :get_p1
- shift /1&set /a count+=1
- if not "%~1"=="" (
- set "_p=%~1"
- if not "!_p:~0,1!"=="-" echo 参数 !count! 错误.&set exit_BL=1&goto get_p_end
- if /i "%~1"=="-s" (
- set size_BL=1
- if "%~2"=="" goto get_p_end
- echo %~2|findstr "^[0-9][0-9]*$">nul&&(set "file_Size=%~2"&shift /1&set /a count+=1&goto get_p1)
- ) else (
- if /i "%~1"=="-o" (
- set ret_BL=1
- if "%~2"=="" goto get_p_end
- if /i "%~2"=="-s" (if "!size_BL!"==1 (set "ret_filename=%~2"&shift /1&set /a count+=1&goto get_p1) else (goto get_p1))
- if "%~2"=="-x" (if "!x_BL!"==21 (set "ret_filename=%~2"&shift /1&set /a count+=1&goto get_p1) else (goto get_p1))
- if "%~2"=="-X" (if "!x_BL!"==22 (set "ret_filename=%~2"&shift /1&set /a count+=1&goto get_p1) else (goto get_p1))
- set "ret_filename=%~2"&shift /1&set /a count+=1&goto get_p1
- ) else (
- if "%~1"=="-x" (
- set x_BL=21
- ) else (
- if "%~1"=="-X" (
- set x_BL=22
- ) else (
- echo 参数 !count! 错误.&set exit_BL=1&goto get_p_end
- )
- )
- )
- )
- goto get_p1
- )
- :get_p_end
- goto:eof
复制代码
- @goto start
- :help
- @echo.===== Ver: 2.0 ======== 同步文件夹 ======== 发布日期: 2014-06-09 =======
- @echo. BAT_NAME: %0
- @echo. 返回值: [%def_filename%]
- @echo.
- @echo. 调用方法(从别处调用):
- @echo. call %0 Folder1 [Folder2] [-s [size]] [-o [filename]] [-x^|X]
- @echo. Folder1 : 源文件夹
- @echo. Folder2 : 目标文件夹,如不指定既为调用程序所在文件夹
- @echo. -s [size]: 文件大于(size MB)被忽略;
- @echo. 不选即不限定filesize;
- @echo. [size] 默认10,可不写. (0 ≤整数≤2147)
- @echo. -o [filename]: 保存同步信息到文件(默认:返回值)
- @echo. 不选即不记录文件copy的信息
- @echo. -x : 双向同步,根据源文件夹的文件更新目标文件夹,
- @echo. 两个文件夹都会改变;
- @echo. 但是源文件夹没有的文件不会从目标文件夹copy.
- @echo. -X 双向同步,两个文件夹完全相同;
- @echo. 不选x^|X,单向同步,根据源文件夹里的文件更新目标文件夹,
- @echo. 源文件夹不会变化;
- @echo. -? : 查看帮助
- @echo. 注意: 0字节文件夹不会被建立.
- @echo.====================== Copyright@ by hf-g 2010-08-08 [彭城] ============
- @echo.====================== Edit by Tamce 2014-06-09 [Tamce.Joe] ============
- ::ping /n 4 127.0>nul&
- goto:end
- ::==============================================================
- :start
- @echo off
- setlocal enabledelayedexpansion
- ::==============================================================
- set "def_filename=%temp%\ret_List.txt"
- ::echo "%1" & pause
- if /i "%~1"=="" goto help
- if /i "%~1"=="-?" goto help
- set "ret_filename="&set "file_Size="
- set /a size_BL=0,ret_BL=0,x_BL=20
- call:get_P %*
- if "%exit_BL%"=="1" goto end
- if "%desFolder%"=="" set "desFolder=%~dp0"
- if "%ret_BL%"=="1" (if "%ret_filename%"=="" set "ret_filename=%def_filename%")
- if "%size_BL%"=="1" (if "%file_Size%"=="" set "file_Size=10")
- echo %ret_BL%
- echo off
- echo 源文件夹: %sorFolder%
- echo 目标文件夹: %desFolder%
- echo 返回值: %ret_filename%
- echo 文件限制: ^<= %file_Size% MB
- if "%x_BL%"=="21" echo 双向同步: -x 方式1
- if "%x_BL%"=="22" echo 双向同步: -X 方式2
- if "%x_BL%"=="20" echo 单向同步: 源文件夹不会变化
- ::goto end
- ::--------------------------- main -------------------------------
- for /f "eol=; skip=2 tokens=3 delims= " %%a in ('reg query "HKEY_CURRENT_USER\Control Panel\International" /v sShortDate') do (set "RegDateOld=%%a")
- ::echo %RegDateOld%
- ::set RegDateOld=%RegDateOld:~-8%
- reg add "HKEY_CURRENT_USER\Control Panel\International" /v sShortDate /t REG_SZ /d yyyy-M-d /f >nul
- ::1MB=1024kB=1024*1024Byte=1048576Byte
- if "%size_BL%"=="1" (set /a file_Size=%file_Size% "*" 1048576)
- ::echo %RegDateOld:~-8% %file_Size%
- ::大于等于指定%file_Size%的文件忽略. 同步为最新文件.
- call:get_fpath "%sorFolder%.\"
- set "sor_drive=%drive%"&set "sor_p_path=%p_path%"
- call:get_fpath "%desFolder%.\"
- set "des_drive=%drive%"&set "des_p_path=%p_path%"
- ::@echo on
- ::①
- (if "%ret_BL%"=="1" cd.>"%ret_filename%" 2>nul)&set/a count=0
- for /f "tokens=* delims=" %%a in ('dir /a-d/s/b "%sorFolder%.\"') do (
- if "%size_BL%"=="0" set /a file_Size=%%~za
- if %%~za LEQ !file_Size! (
- set "sorfile=%%a"
- set "sorfile=!sorfile:%%~da%sor_p_path%=!"
- for /f "tokens=* delims=" %%i in ("%desFolder%.\!sorfile!") do (
- if %%~ta GTR %%~ti (
- md "%%~dpi" 2>nul
- copy "%%~a" "%%~i">nul&&(set/a count+=1&set /p =!count!<nul&if "%ret_BL%"=="1" echo copyFile %%~a → %%~i>>"%ret_filename%")
- ) else (
- if %x_BL% GTR 20 (
- if %%~ti GTR %%~ta (
- md "%%~dpa" 2>nul
- copy "%%~i" "%%~a">nul&&(set/a count+=1&set /p =!count!<nul&if "%ret_BL%"=="1" echo copyFile %%~a ← %%~i>>"%ret_filename%")
- ))
- ))))
- @echo off
- ::②
- if %x_BL% == 22 (
- for /f "tokens=* delims=" %%a in ('dir /a-d/s/b "%desFolder%.\"') do (
- if %file_Size% GTR %%~za (
- set "desfile=%%a"
- set "desfile=!desfile:%%~da%des_p_path%=!"
- if not exist "%sorFolder%.\!desfile!" (
- call:get_fpath "%sorFolder%.\!desfile!"
- md "!drive!!p_path!" 2>nul
- copy "%%~a" "%sorFolder%.\!desfile!">nul&&(set/a count+=1&set /p =!count!<nul&if "%ret_BL%"=="1" echo copyFile %sorFolder%.\!desfile! ← %%~a>>"%ret_filename%")
- ))))
- if exist "%ret_filename%" (start notepad.exe "%ret_filename%")
- del "%def_filename%" 2>nul
- ::----------------------------------------------------------------
- reg add "HKEY_CURRENT_USER\Control Panel\International" /v sShortDate /t REG_SZ /d %RegDateOld% /f >nul
- :end
- ::echo ☆☆☆
- del tempfile.txt strHEX.txt>nul 2>nul
- exit /b
- ::====================================================
- :get_fpath [drive:path]
- set "drive=%~d1"
- set "p_path=%~p1"
- goto:eof
- ::====================================================
- ::========================参数设置与获得============================
- :get_p
- echo off
- ::调用: call synFolder.bat Folder1 [Folder2] [-s [size]] [-o [filename]] [-x]
- set "sorFolder=%~1"&set /a count=1
- if not "%~2"=="" (
- set "_p=%~2"
- if not "!_p:~0,1!"=="-" (set "desFolder=%~2"&shift /1&set /a count+=1)
- ) else (
- goto get_p_end
- )
- :get_p1
- shift /1&set /a count+=1
- if not "%~1"=="" (
- set "_p=%~1"
- if not "!_p:~0,1!"=="-" echo 参数 !count! 错误.&set exit_BL=1&goto get_p_end
- if /i "%~1"=="-s" (
- set size_BL=1
- if "%~2"=="" goto get_p_end
- echo %~2|findstr "^[0-9][0-9]*$">nul&&(set "file_Size=%~2"&shift /1&set /a count+=1&goto get_p1)
- ) else (
- if /i "%~1"=="-o" (
- set ret_BL=1
- if "%~2"=="" goto get_p_end
- if /i "%~2"=="-s" (if "!size_BL!"==1 (set "ret_filename=%~2"&shift /1&set /a count+=1&goto get_p1) else (goto get_p1))
- if "%~2"=="-x" (if "!x_BL!"==21 (set "ret_filename=%~2"&shift /1&set /a count+=1&goto get_p1) else (goto get_p1))
- if "%~2"=="-X" (if "!x_BL!"==22 (set "ret_filename=%~2"&shift /1&set /a count+=1&goto get_p1) else (goto get_p1))
- set "ret_filename=%~2"&shift /1&set /a count+=1&goto get_p1
- ) else (
- if "%~1"=="-x" (
- set x_BL=21
- ) else (
- if "%~1"=="-X" (
- set x_BL=22
- ) else (
- echo 参数 !count! 错误.&set exit_BL=1&goto get_p_end
- )
- )
- )
- )
- goto get_p1
- )
- :get_p_end
- goto:eof
欢迎光临 批处理之家 (http://bbs.bathome.net/) | Powered by Discuz! 7.2 |