本帖最后由 aloha20200628 于 2024-6-3 15:26 编辑
回复 1# 娜美
以下代码可处理源文件名与目标文件名的重复情况-举例如下:
源文件夹中要移至目标文件夹的文件是》abc.txt,abc (2).txt
目标文件夹已存在的文件是》abc.txt,abc (2).txt,abc (3).txt
处理结果是目标文件夹新增两个文件》abc (4).txt,abc (5).txt
代码第12行数字 -5 管控文件名序号长度(2-999),绝对值增加则长度增加
- @echo off &setlocal enabledelayedexpansion
- set "path1=C:\2" &set "path2=D:\3" &pushd "!path1!"
- for /f "delims=" %%F in ('dir /b/a-d *.txt *.rar *.zip *.jpg *.mp4') do (
- if not exist "!path2!\%%~nxF" (move "%%F" "!path2!\") else (
- (call :get#nF "%%~nF" nFn n) & (call :check#2F nFn "%%~xF" !n! qF)
- move "%%F" "!path2!\!nFn!(!qF!)%%~xF"
- )
- )
- popd&endlocal&exit/b
- :get#nF //REM 获取源文件名序号锚点
- set "_nF=%~1" &if "!_nF:~-1!" neq ")" (set "%~2=%~1 " &set "%~3=2" &exit/b)
- set "_k=" &for /L %%k in (-2,-1,-5) do (
- set "c=!_nF:~%%k,1!"
- if "!c!"=="(" (set "%~2=!_nF:~0,%%k!" &set "%~3=!_k!" &exit/b) else (
- if "!c!" geq "0" if "!c!" leq "9" set "_k=!c!!_k!"
- )
- )
- exit/b
- :check#2F //REM 验证目标文件名不重复序号
- set "_qF=%~3"
- :[Loop]
- if exist "!path2!\!%~1!(!_qF!)%~2" (set/a "_qF+=1" &goto[Loop])
- set "%~4=!_qF!" &exit/b
复制代码
|