JSI Tip 7950. How can I compare the file names of two folders?


Using only standard commands, I have scripted CompFldr.bat to compare the file names of two folders.

The syntax for using CompFldr.bat is:

\[call\] CompFldr Folder1 Folder2

Where Folder1 and Folder2 are the respective folder paths. They must use drive letters.

Example:

If C:\Folder1 contains the following file names:
"I am in Folder1 but Not in Folder2.doc"
"I am in both Folders.doc"

and D:\Folder2 contains the following file names:

"I am in both folders.doc"
"I am in Folder2 but Not in Folder1.doc"

then 

compfldr c:\folder1 d:\folder2

would produce:

"C:\Folder1\I am in Folder1 but Not in Folder2.doc"
"D:\Folder2\I am in Folder2 but Not in Folder1.doc"
NOTE: Any differences in file name case is not significant.

CompFldr.bat Contains:

@echo off
setlocal
if exist "%TEMP%\CompFldr*.tm*" del /q "%TEMP%\CompFldr*.tm*"
if \{%2\}==\{\} goto Syntax
if not exist %1 goto Syntax
if not exist %2 goto Syntax
for /f "tokens=*" %%s in ("%1") do (
 set Src=%%~fs
 set SrcAttr=%%~as
 set SrcDrv=%%~ds
)
set Src="%Src:"=%"
set Src=%Src:\"="%
for /f "tokens=*" %%d in ("%2") do (
 set Dst=%%~fd
 set DstAttr=%%~ad
 set DstDrv=%%~dd
)
set Dst="%Dst:"=%"
set Dst=%Dst:\"="%
:: Verify Source and Destination are folders 
if /i "%SrcAttr:~0,1%%DstAttr:~0,1%" NEQ "dd" goto error
if /i %Src% EQU %Dst% goto error
If "%SrcDrv:~1,1%" NEQ ":" goto notlcl
If "%DstDrv:~1,1%" NEQ ":" goto notlcl
call :silent>nul 2>&1
if not exist "%TEMP%\CompFldrC.tm1" @echo No file difference between %Src% and %Dst%&goto finish
set Src=%Src:"=%
set Dst=%Dst:"=%

for /f "Tokens=1*" %%r in ('type "%TEMP%\CompFldrC.tm1"') do (
 call :Report %%r %%s
)
:finish
if exist "%TEMP%\CompFldr*.tm*" del /q "%TEMP%\CompFldr*.tm*"
endlocal
exit /b 0
:Report
set file=%2
set file=%file:"=%
if "%1" EQU "S" Set fld="%Src%\%file%"&goto Report1
if "%1" EQU "D" Set fld="%Dst%\%file%"
:Report1
@echo %fld%
goto :EOF
:notlcl
@echo %Src% and %Dst% must use drive letters.
goto Syntax
:error
@echo %Src% and %Dst% must be (different) folders.
:Syntax
@echo Syntax: CompFldr Folder1 Folder2
endlocal
exit /b 1
:silent
@echo .>"%TEMP%\CompFldr_Src.tm0"
@echo .>"%TEMP%\CompFldr_Dst.tm0"
dir %Src% /b /a-d >>"%TEMP%\CompFldr_Src.tm0"
dir %Dst% /b /a-d >>"%TEMP%\CompFldr_Dst.tm0"
for /f "Tokens=*" %%s in ('type "%TEMP%\CompFldr_Src.tm0"') do (
 @echo "%%s" >>"%TEMP%\CompFldr_Src.tm1"
)
for /f "Tokens=*" %%d in ('type "%TEMP%\CompFldr_Dst.tm0"') do (
 @echo "%%d" >>"%TEMP%\CompFldr_Dst.tm1"
)
findstr /i /v /l /g:"%TEMP%\CompFldr_Dst.tm1" "%TEMP%\CompFldr_Src.tm1">"%TEMP%\CompFldr_Src.tm2"
findstr /i /v /l /g:"%TEMP%\CompFldr_Src.tm1" "%TEMP%\CompFldr_Dst.tm1">"%TEMP%\CompFldr_Dst.tm2"
if not exist "%TEMP%\CompFldr_Src.tm2" goto CompFldr1
for /f "Tokens=*" %%s in ('type "%TEMP%\CompFldr_Src.tm2"') do (
 @echo S %%s>>"%TEMP%\CompFldrC.tm0"
)
:CompFldr1
if not exist "%TEMP%\CompFldr_Dst.tm2" goto CompFldr2
for /f "Tokens=*" %%d in ('type "%TEMP%\CompFldr_Dst.tm2"') do (
 @echo D %%d>>"%TEMP%\CompFldrC.tm0"
)
:CompFldr2
if not exist "%TEMP%\CompFldrC.tm0" goto :EOF
Sort /+2 "%TEMP%\CompFldrC.tm0" /O "%TEMP%\CompFldrC.tm1"



Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish