本帖最后由 Gin_Q 于 2021-5-29 13:16 编辑
- @echo off
- title By Author GIN 2021/05/25
- SETLOCAL ENABLEDELAYEDEXPANSION
-
- set souceFile=files.txt
- set targetDir=d:\gin\c#
- set notFoundFileName=未找到.txt
-
- set newDirectory=%cd%\newDirectory
- if not exist %newDirectory% (md "%newDirectory%")
- REM 源路径为:d:\gin\c#
- REM 新路径为:D:\GIN\copyFile\newDirectory
- echo 源路径为:%targetDir%
- echo 新路径为:%newDirectory%
-
- call :equalTotalChar "%targetDir%" "%newDirectory%"
- set equalCharTotal=!errorlevel!
- call :readFileList "%souceFile%"
- call :getTargetinfo "%targetDir%"
- call :matchTarget
- call :deleteSourceFile
-
- pause & exit
-
- REM 计算顶部路径相同部分
- :equalTotalChar
- set sourcePath=%~1
- set newPath=%~2
- for /l %%i in (0,1,255) do (
- if /i "!sourcePath:~%%i,1!" neq "!newPath:~%%i,1!" (
- if /i "!sourcePath:~%%i,1!" equ "\" (exit /b %%i) else (
- for /l %%b in (%%i,-1,0) do (
- if "!sourcePath:~%%b,1!" equ "\" (exit /b %%b)
- )
- )
- )
- )
- exit /b 0
-
- REM 读取文件列表
- :readFileList
- set fileName=%1
- set /a sourceFileLines = 0
- for /f "tokens=*" %%L in ('type %fileName%') do (set /a sourceFileLines+=1 & set "source_!sourceFileLines!=%%L")
- exit /b
-
- REM 获取目标目录信息
- :getTargetinfo
- set targetDirectory=%1
- set /a targetNumber=0
- for /f "tokens=*" %%u in ('dir /b /s %targetDirectory%') do (set /a targetNumber+=1 & set "target_!targetNumber!=%%u")
- exit /b
-
- REM 匹配目标
- :matchTarget
- echo 数据源数量为:%sourceFileLines%
- set /a deleteNumber=0
- echo %date% %time% >> %notFoundFileName%
- for /l %%s in (1,1,%sourceFileLines%) do (
- echo 正在处理第 %%s 个,!source_%%s!
- set found=false
- for /l %%t in (1,1,%targetNumber%) do (
- call :getFileOrDirectoryName "!source_%%s!" "!target_%%t!" & set matchResult=!errorlevel!
- if !matchResult! equ 1 (
- set found=true
- set "tempNewDirOrFile=%newDirectory%!target_%%t:~%equalCharTotal%!"
- pushd "!target_%%t!" && popd & (
- if not exist "!tempNewDirOrFile!" (
- md "!tempNewDirOrFile!" && xcopy /e /y "!target_%%t!" "!tempNewDirOrFile!"
- )
- ) || (
- if exist "!tempNewDirOrFile!" (
- echo f | xcopy /y "!target_%%t!" "!tempNewDirOrFile!"
- ) else (
- echo f | xcopy "!target_%%t!" "!tempNewDirOrFile!"
- )
- )
- set /a deleteNumber+=1 & set "deleteSourceFileOrDirectory_!deleteNumber!=!target_%%t!"
- ) 1>nul 2>nul
- )
- if "!found!" equ "false" (
- echo !source_%%s! >> %notFoundFileName%
- )
- )
- exit /b
-
- REM 匹配 目标判断
- :getFileOrDirectoryName
- set "souce=%1"
- set "target=%~nx2"
- if %souce% == "%target%" (exit /b 1)
- exit /b 0
-
- REM 删除源文件
- :deleteSourceFile
- echo 是否删除所有原路径下已经被复制的文件和文件夹?
- echo 输入 Y 是,N 否:
- choice
- set input=%errorlevel%
- if %input% neq 1 (exit /b)
- for /l %%n in (1,1,!deleteNumber!) do (del /p /s !deleteSourceFileOrDirectory_%%n!)
- exit /b
复制代码
|