mergesimilar合并类似文件(bat实现)
@ECHO OFF setlocal ENABLEDELAYEDEXPANSION set filename="%~1" REM=echo !filename! call :STRLEN !filename! set /a nameLen=!STRLEN! call :GETFORMAT !filename! set fileformat=!GETFORMAT! set /a filenum=0 set /a bHasError=0 set files= set dropfiles= FOR /F "delims=" %%k in ('dir /b /A:-D /O:N !fileformat!') do ( IF EXIST "%%k" ( REM type "%%k" >>!destfilename! call :STRLEN "%%k" IF !nameLen! neq !STRLEN! ( set /a bHasError=1 ECHO=这个名字长度不同%%k,按照名字排序将有问题,该文件不合并 set dropfiles=!dropfiles!"%%k" ) ELSE ( set /a filenum+=1 IF "!files!"=="" ( set files="%%k" ) else ( set files=!files!+"%%k" ) ) ) ) IF !filenum! geq 2 ( REM=超过两个文件,拼接完成 set destfilename=!GETFORMAT[0]!.!filenum!.ms REM copy !fileformat! !destfilename! REM type >!destfilename! 2>nul REM copy效率比type高很多 copy !files! !destfilename! ECHO=拼接完成 IF !bHasError! equ 1 ( ECHO=部分文件没有被合并!dropfiles! PAUSE ) REM PAUSE ) else ( @color C @mode con: cols=50 lines=8 ECHO=少于两个文件无需拼接 PAUSE ) GOTO :eof REM=去掉数值替换成通配,支持形如ABC12345.runlog :GETFORMAT set GETFORMAT= set str=%~1 IF "!str!"=="" ( GOTO :eof ) set /a cur=0 set prefix= set postfix= set /a IsPrefix=1 set /a IsPostfix=0 :GETFORMAT_while set char=!str:~%cur%1! IF NOT "!char!"=="" ( if !char! geq 0 ( if !char! leq 9 ( REM=结束前缀 set /a IsPrefix=0 ) ) IF "!char!"=="_" ( REM=结束前缀 set /a IsPrefix=0 ) IF "!char!"=="." ( REM=结束前缀 set /a IsPrefix=0 REM=重新开始后缀 set /a IsPostfix=1 set postfix= ) IF !IsPrefix! EQU 1 ( REM=拼接 set "prefix=!prefix!!char!" ) IF !IsPostfix! EQU 1 ( REM=拼接 set "postfix=!postfix!!char!" ) set /a cur=cur+1 goto :GETFORMAT_while ) ) set "GETFORMAT=!prefix!*!postfix!" set "GETFORMAT[0]=!prefix!!postfix!" GOTO :eof :STRLEN set str=%~1 set cur=0; :while_STRLEN if "!str:~%cur%1!"=="" ( set /a STRLEN=cur ) else ( set /a cur=cur+1 goto :while_STRLEN ) GOTO :eof