JSI Tip 7951. How can I compare the file names of two folders and sub-folders?


Using only standard commands, I have scripted CompFldrS.bat to compare the file names of two folders and their sub-folders.

The syntax for using CompFldrS.bat is:

\[call\] CompFldS 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:
"C:\Folder1\I am in Folder1 but Not in Folder2.doc"
"C:\Folder1\I am in both Folders.doc"
"C:\Folder1\Folder1Sub\File1.txt"

and D:\Folder2 contains the following file names:

"D:\Folder2\I am in both folders.doc"
"D:\Folder2\I am in Folder2 but Not in Folder1.doc"
"D:\Folder2\Folder1Sub\File1.txt"
"D:\Folder2\Folder2Sub\File2.txt"

then 

compfldrs 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"
"D:\Folder2\Folder2Sub\File2.txt"
NOTE: CompFldrS.bat uses VarLen.bat.

NOTE: Any differences in file name case is not significant.

CompFldrS.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
set prev=N
call :silent>nul 2>&1
for /f "Tokens=*" %%a in ('type "%TEMP%\CompFldr_Sort.tm3"') do (
 set line=%%a
 call :Report
)
:finish
if exist "%TEMP%\CompFldr*.tm*" del /q "%TEMP%\CompFldr*.tm*"
endlocal
exit /b 0
:Report
set line=%line:"=%
set line=%line:/=\%
if "%line:~0,1%" EQU "." set pline=%line:~3%&call VarLen pline Len&goto :EOF
set Drv=%line:~0,2%
call set tline=%%line:~3,%len%%%
if "%tline%" NEQ "%pline%" goto :EOF
@echo "%Drv%%pline%"
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: CompFldrS Folder1 Folder2
endlocal
exit /b 1
:silent
dir %Src% /b /a-d /s >>"%TEMP%\CompFldr_Src.tm0"
dir %Dst% /b /a-d /s >>"%TEMP%\CompFldr_Dst.tm0"
for /f "Tokens=*" %%s in ('type "%TEMP%\CompFldr_Src.tm0"') do (
 call :SrcD "%%s"
)
for /f "Tokens=*" %%d in ('type "%TEMP%\CompFldr_Dst.tm0"') do (
 call :DstD "%%d"
)
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"
copy "%TEMP%\CompFldr_Src.tm2"+"%TEMP%\CompFldr_Dst.tm2"+"%TEMP%\CompFldr_Sort.tm1" "%TEMP%\CompFldr_Sort.tm2"
sort /+5 "%TEMP%\CompFldr_Sort.tm2" /O "%TEMP%\CompFldr_Sort.tm3"
goto :EOF
:SrcD
set work=%1
call set work=%%work:%SrcDrv%=%%
set work=%work:"=%
set work=%work:\=/%
@echo "...%work%">>"%TEMP%\CompFldr_Src.tm1"
@echo %SrcDrv% "%work%" 1 >>"%TEMP%\CompFldr_Sort.tm1"
goto :EOF
:DstD
set work=%1
call set work=%%work:%DstDrv%=%%
set work=%work:"=%
set work=%work:\=/%
@echo "...%work%">>"%TEMP%\CompFldr_Dst.tm1"
@echo %DstDrv% "%work%" 2 >>"%TEMP%\CompFldr_Sort.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