How to implement Catalog comparison tool in batch bat
Editor to share with you how to batch bat directory comparison tools, I believe that most people do not understand, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Update log:
V4:
1. Added the progress display of the calculation
two。 Found the new version of md5sum.exe, no dependence on dynamic libraries.
Tool 1: md5comv4.bat accepts two directory parameters, calculates the md5 value of each file in the two directories, and gives the report file md5v4.txt, how to use it:
Md5omv4.bat
According to the report file md5v4.txt generated by md5comv4.bat, you can find two directories
1: a file with the same name and md5
2: files with the same name but different md5
3: files with different names but same md5
4: files with different names but the same md5 in the same directory.
Note:
1:md5comv4.bat does not accept directory names with spaces in the path, but can accept file names with spaces
The calculation of 2:md5 is completed by md5sum.exe
The 3:md5comv4.bat runtime produces a large number of temporary files that are automatically maintained by tools and do not have to be cleaned up manually.
This tool is command-line, text interface, for friends accustomed to using the graphical interface, may not be convenient, therefore, friends accustomed to using the graphical interface is not recommended.
The code is as follows:
@ echo off
Setlocal enableextensions enabledelayedexpansion
Rem author: rubble@126.com
Rem 17:55 2009-4-14 version: 0.4
Set dira=%~1
Set dirb=%~2
If "% 1" = "" (
Instructions for using echo: please specify two directories on the command line parameters, or follow the prompts here.
Echo command line argument format: batch name ^ ^
Echo.
Set / p dira= Please enter the name of the first directory (drag and drop and enter enter):
)
Echo.
If "% 2" = "" (
Instructions for using echo: please specify two directories on the command line parameters, or follow the prompts here.
Echo command line argument format: batch name ^ ^
Echo.
Set / p dirb= Please enter the name of the second directory (drag and drop and enter enter):
)
If "% dira%" = "" exit / b
If "% dirb%" = "" exit / b
For / F "delims=. tokens=1" An in ("TIME%") do set startTime=%%A
For / F "delims=: tokens=1,2,3"% An in ("% startTime%") do (
Set startH=%%A
Set startM=%%B
Set startS=%%C
)
Set TEMPDIR=TEMPMD5\
Rd / Q / S% TEMPDIR% 1 > NUL 2 > NUL
Md TEMPDIR%
Set mdfive=md5v4.txt
Number of files processed by rem
Set count=0
Total number of files in the rem folders% dira% and% dirb%
Set total_count=0
Number of temporary files processed by rem
Set tmpf_count=0
The total number of temporary rem files is known to be 5. The rest is the number of md5 values.
Set tmpf_total_count=5
Echo step 1max 2, calculating, please wait a moment.
Echo is comparing the md5 value of files in dira% and dirb% > mdfive%
Set list_fn_md=%TEMPDIR%list_fn_md
Set list_fn_nmd=%TEMPDIR%list_fn_nmd
Set list_fn_oa=%TEMPDIR%list_fn_oa
Set list_fn_ob=%TEMPDIR%list_fn_ob
Set list_all_md=%TEMPDIR%list_all_md
Call: COUNTFILES% dira%
Call: COUNTFILES% dirb%
For / f "delims="% an in ('dir / b / Alav murd "% dira%\ *") do (
Set fn=%%~nxa
For / f "tokens=1 delims="% An in ('md5sum "% dira%\% a"') do set mda=%%A
Set / a countdown account 1
Call: PROGRESS! count!! total_count!
If "! mda:~0,1!" = "\" set MDAQ MDAR 1Magne1024!
Rem to! mda! For the file name, record md5 as! mda! The file of
Call: ADDMDFN "% TEMPDIR% roommda!"% dira%\! fn!"
Rem records the md5 value in a file to ensure that it is not duplicated
Call: ADDMDLIST! mda!
If exist "% dirb%\! fn!" (
For / f "tokens=1 delims="% An in ('md5sum "% dirb%\! fn!"') Do set mdb=%%A
Set / a countdown account 1
Call: ADDMDFN "% TEMPDIR% roommdb!"% dirb%\! fn!"
Call: PROGRESS! count!! total_count!
If "! mdb:~0,1!" = = "\" set MdbPhillips MdbPhantom 1Magne1024!
If "! mda!" = "! mdb!" (
Rem records files with the same file name and the same md5
Echo! mda!! mdb! fn! > "! list_fn_md!"
) else (
Rem records files with the same file name but different md5
Echo! mda!! mdb! fn! > "! list_fn_nmd!"
Call: ADDMDLIST! mdb!
)
) else (
Rem records files that appear only in% dira%
Echo! mda!! fn! > "! list_fn_oa!"
)
)
For / f "delims="% an in ('dir / b / Alav murd "% dirb%\ *") do (
Set fn=%%~nxa
If not exist "% dira%\! fn!" (
For / f "tokens=1 delims="% An in ('md5sum "% dirb%\% a"') do set mdb=%%A
If "! mdb:~0,1!" = = "\" set MdbPhillips MdbPhantom 1Magne1024!
Rem records files that appear only in% dirb%
Echo! mdb!! fn! > "! list_fn_ob!"
Rem records the md5 value in a file to ensure that it is not duplicated
Call: ADDMDLIST! mdb!
Call: ADDMDFN "% TEMPDIR% roommdb!"% dirb%\! fn!"
Set / a countdown account 1
Call: PROGRESS! count!! total_count!
)
)
Step 2 of echo, counting, please wait a moment.
Echo Statistics: > >% mdfive%
Echo left:% dira% > >% mdfive%
Echo right:% dirb% > >% mdfive%
If not exist "% list_fn_md%" set / a tmpf_total_count-=1
If not exist "% list_fn_nmd%" set / a tmpf_total_count-=1
If not exist "% list_fn_oa%" set / a tmpf_total_count-=1
If not exist "% list_fn_ob%" set / a tmpf_total_count-=1
If not exist "% list_all_md%" set / a tmpf_total_count-=1
Files that exist in both echo directories and have the same md5: > >% mdfive%
Call: OUTPUTLIST "% list_fn_md%"
Echo = > >% mdfive%
Echo left:% dira% > >% mdfive%
Echo right:% dirb% > >% mdfive%
Files that exist in both echo directories but with different md5: > >% mdfive%
Call: OUTPUTLIST "% list_fn_nmd%"
Echo = > >% mdfive%
Echo files that exist only in directory% dira%: > >% mdfive%
Call: OUTPUTLIST "% list_fn_oa%"
Echo = > >% mdfive%
Echo files that exist only in directory% dirb%: > >% mdfive%
Call: OUTPUTLIST "% list_fn_ob%"
Echo = > >% mdfive%
List of files summarized by echo in md5: > >% mdfive%
Call: OUTPUTLIST_MD "% list_all_md%"
Goto ALLDONE
Rem counts the number of files in the folder and puts them in the global variable total_count
: COUNTFILES
If% 1 goto = "" goto: EOF
For / f% an in ("% ~ 1\ *") do ('dir / b / avellisd "~ 1\ *")
Set / a total number of counts.The total number of counters
)
Goto: EOF
Rem parameter 1 must be in double quotation marks
: OUTPUTLIST
If% 1 = "" goto: EOF
If not exist% 1 goto: EOF
Set tcnt=0
For / f "tokens=1* delims=" an in (% ~ 1) do (
Echo a b > > mdfive%
Set / a tcnt+=1
)
Echo! tcnt! > > mdfive%
Set / a tmpf_count+=1
Call: PROGRESS! tmpf_count!! tmpf_total_count!
Goto: EOF
Rem parameter 1 must be in double quotation marks
: OUTPUTLIST_MD
If% 1 = "" goto: EOF
If not exist% 1 goto: EOF
Set / a tmpf_count+=1
Call: PROGRESS! tmpf_count!! tmpf_total_count!
Set md_count=0
For / f "tokens=1* delims=" an in (% ~ 1) do (
Echo a > > mdfive%
Set tcnt=0
For / f "tokens=1* delims="%% An in (! TEMPDIRM%% a) do (
Echo A B > > mdfive%
Set / a tcnt+=1
)
Set / a tmpf_count+=1
Call: PROGRESS! tmpf_count!! tmpf_total_count!
Echo! tcnt! > > mdfive%
Set / a md_count+=1
Echo. > > mdfive%
)
Echo md_count% > > mdfive%
Goto: EOF
Rem function: save the md5 value to a file named! list_all_md! In the file.
Rem and guarantee that duplicate md5 values will not be saved
: ADDMDLIST
If% 1 = "" goto: EOF
If exist "! list_all_md!" (
The delims here in rem can either be deleted or made to look like the line below, with a space after the equal sign
Rem for / f "tokens=1 delims=" an in (! list_all_md!) Do (
For / f "tokens=1" an in (! list_all_md!) Do (
If "% a" = = "% 1" goto: EOF
)
)
Echo 1 > > "! list_all_md!"
Total number of temporary files recorded by rem
Set / a tmpf_total_count+=1
Goto: EOF
Rem function: save% 2 with% 1 as the file name
Rem% 1: md5 value in this application
Files with rem 2:md5 value 1
: ADDMDFN
If% 1 = "" goto: EOF
If% 2 = "" goto: EOF
Echo 2 > > 1
Goto: EOF
: PROGRESS
Set tips=
Set backspace=
Set / a percent=%1*100/%2
Set / a char=%1%%7
Set / a bnum=%percent%/10
Set / a snum=10-%bnum%
For / L an in (1,1, bnum%) do set / p = ▌ > mdfive%
Start mdfive%
Echo
Endlocal
@ echo on
These are all the contents of this article entitled "how to implement Catalog comparison tools in batch bat". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!