# Creating batch file to Check Directories



## Neablis (Jun 16, 2009)

Hello. Im trying to make a batch file that goes through two directors and writes to a txt document all the files that exist in one that dont exist in the other. I have it ALMOST working but i cant figure out how to get it to go down in folders to compare the rest of the directory. Any he[lp would be nice, THANKS!


```
@echo off

setlocal ENABLEDELAYEDEXPANSION

Set dirA=%1
Set dirB=%2

dir /B /O %dirA% >dirA.txt
dir /B /O %dirB% >dirB.txt

Echo. >>AnotinB.txt
find /V /C "this_is_an_absurd_string" dirB.txt >numlines.txt
for /f "tokens=3 delims= " %%B in (numlines.txt) do set /A maxnum=%%B

for /F "tokens=* delims= " %%A in (dirA.txt) do (
find /V /C "%%A" dirB.txt >numlines.txt
for /f "tokens=3 delims= " %%B in (numlines.txt) do set /A foundnum=%%B
if %maxnum%==!foundnum! echo %%A >>AnotinB.txt
)


copy /B AnotinB.txt dupli.txt >nul
more dupli.txt

for %%A in (dirA.txt dirB.txt AnotinB.txt numlines.txt) do (
if exist %%A del %%A
)
```


----------



## TheOutcaste (Mar 19, 2009)

Welcome to TSF!

The code you posted will display all files that are in DirA that are not in DirB, though it needs some quotes to work with folder names that have spaces:

```
dir /B /O [B]"[/B]%dirA%[B]"[/B] >dirA.txt
dir /B /O [B]"[/B]%dirB%[B]"[/B] >dirB.txt
```
Are you wanting to also compare subfolders? Meaning compare two complete trees, not just the folders?
So not only compare *DirA* to *DirB*, but *DirA\Sub1* to *DirB\Sub1*, and *DirA\Sub1\SubSub1* to *DirB\Sub1\SubSub1*?

Or do you just want to find out if a file anywhere in the *DirA* tree exists anywhere in the *DirB* tree?
So if *File1.txt* exists in *DirA* (*DirA\File1.txt*) and is found in *DirB\Sub1* (*DirB\Sub1\File1.txt*) it would *not* be listed?

Jerry


----------



## Neablis (Jun 16, 2009)

I think i may have got a little further, if not working. Havnt done extensive testing. it leaves the remements of the folder in the search but that can be ignored in my c++ code hats using it. The purpose to find all files that exist in DirA that dont exist in DirB so instead of 


```
dir /B /O %dirA% >dirA.txt
dir /B /O %dirB% >dirB.txt
```


```
dir /B /O /S %dirA% >dirA.txt
dir /B /O /S %dirB% >dirB.txt
```
to include all the sub directory's in the search. I saw a lot of people looking for similar stuff when looking for help, so it is working i hope this can help people.


----------



## TheOutcaste (Mar 19, 2009)

That won't work, as the files will contain the full path. Since dirA and dirB are different, it will indicate that all the files in dirA are not in dirB
For example, if test1.txt is in dirA, this will check to see if C:\dirA\test1.txt is found in the listing for dirB. It won't be, as test1.txt will be listed as C:\dir*B*\test1.txt

You need to remove the root folder from the string you are checking:

```
@Echo Off
SetLocal EnableDelayedExpansion
Set dirA=%~1
Set dirB=%~2

Dir /B /O /S "%dirA%">dirA.txt
Dir /B /O /S "%dirB%">dirB.txt

Echo.>>AnotinB.txt
Find /V /C "this_is_an_absurd_string" dirB.txt>numlines.txt
For /F "tokens=3 delims= " %%B In (numlines.txt) Do Set /A maxnum=%%B

For /F "tokens=*" %%A In (dirA.txt) Do (
Set _FN=%%A
Call Set _FN=%%_FN:%dirA%=%%
Find /V /C "!_FN!" dirB.txt>numlines.txt
For /F "tokens=3 delims= " %%B In (numlines.txt) Do Set /A foundnum=%%B
If %maxnum%==!foundnum! Echo %%A>>AnotinB.txt
)
more AnotinB.txt

For %%A In (dirA.txt dirB.txt numlines.txt) Do If Exist %%A Del %%A
```
HTH

Jerry


----------



## Neablis (Jun 16, 2009)

Actually its still not working. I can get the recursion working, but not the compare afterwords. Its wierd, im not even sure why its not working anymore, it just writes all the contents to the document.


----------



## TheOutcaste (Mar 19, 2009)

Here's another option. This will be much faster for large directories, as the above version runs the Find command for each file, and has to read the complete DirB.txt file for each file name it checks.

This will list all files that are found in both trees, all files that are in the First tree but not the Second, and all files that are in the Second tree but not the First.


```
@Echo Off
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: FindDup.cmd @Echo Off
:: Finds Duplicate and missing files in two directory trees
:: Written by TheOucaste
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
If [%1]==[] Goto _Usage
If [%2]==[] Goto _Usage
SetLocal EnableDelayedExpansion
Set _DirA=%~f1
Set _DirB=%~f2
If Not %_DirA:~-1%==\ Set _DirA=%_DirA%\
If Not %_DirB:~-1%==\ Set _DirB=%_DirB%\
If Not Exist "%_DirA%" (Echo %_DirA% does not exist)&Goto _Usage
If Not Exist "%_DirB%" (Echo %_DirB% does not exist)&Goto _Usage
:: set up info lines for each section of the report
Echo Files that are found in BOTH folders %_DirA% AND %_DirB%>_t0.txt
Echo.>>_t0.txt
Echo Files that are found in folder %_DirA% but NOT in folder %_DirB%>_t1.txt
Echo.>>_t1.txt
Echo Files that are found in folder %_DirB% but NOT in folder %_DirA%>_t2.txt
Echo.>>_t2.txt
If Exist _t3.txt Del /F /Q _t3.txt
:: Check root folders
For /F "Delims=" %%I In ('Dir /B /O:N /A-D "%_DirA%" 2^>Nul') Do If EXIST "%_DirB%%%I" (Echo %%I>>_t0.txt) Else (Echo %%I>>_t1.txt)
For /F "Delims=" %%I In ('Dir /B /O:N /A-D "%_DirB%" 2^>Nul') Do If NOT EXIST "%_DirA%%%I" Echo %%I>>_t2.txt
:: Check Subfolders of _DirA
For /F "Delims=" %%A In ('Dir /B /O:N /AD /S "%_DirA%" 2^>Nul') Do (
Set _CD=%%A
Call Set _CD=%%_CD:%_DirA%=%%
>>_t3.txt Echo !_CD!
For /F "Delims=" %%I In ('Dir /B /O:N /A-D "%_DirA%!_CD!" 2^>Nul') Do If EXIST "%_DirB%!_CD!\%%I" (Echo !_CD!\%%I >>_t0.txt) Else (Echo !_CD!\%%I>>_t1.txt)
For /F "Delims=" %%I In ('Dir /B /O:N /A-D "%_DirB%!_CD!" 2^>Nul') Do If NOT EXIST "%_DirA%!_CD!\%%I" Echo !_CD!\%%I>>_t2.txt
)
:: Check Subfolders of _DirB
For /F "Delims=" %%A In ('Dir /B /O:N /AD /S "%_DirB%"^|Findstr /I /V /G:_t3.txt') Do (
Set _CD=%%A
Call Set _CD=%%_CD:%_DirB%=%%
For /F "Delims=" %%I In ('Dir /B /O:N /A-D "%_DirB%!_CD!" 2^>Nul') Do If EXIST "%_DirA%!_CD!\%%I" (Echo !_CD!\%%I >>_t0.txt) Else (Echo !_CD!\%%I>>_t2.txt)
For /F "Delims=" %%I In ('Dir /B /O:N /A-D "%_DirA%!_CD!" 2^>Nul') Do If NOT EXIST "%_DirB%!_CD!\%%I" Echo !_CD!\%%I>>_t1.txt
)
:: write a separator line to end each section
For /L %%I In (0,1,2) Do Echo ---------------------------------------------------------------------->>_t%%I.txt
Copy /B _t0.txt+_t1.txt+_t2.txt duplicat.txt>Nul
:: Cleanup
For /L %%C In (0,1,3) Do (Del /F/Q _t%%C.txt)
Cls
More duplicat.txt
Echo Report saved as %~dp0duplicat.txt
Goto :EOF
:_Usage
Echo.
Echo %~n0 [drive:][path]Folder1 [[drive:]path]Folder2
Echo.
Echo.        Compares two folder trees and Lists the differences
Echo.        [drive:][path]Folder1 - First Folder Tree
Echo.        [drive:][path]Folder2 - Second Folder Tree
Echo.
Echo.        The folders must both exist. If Drive is not specified,
Echo.        assumes path is relative to the current folder.
```
If you only want to list files that are in the First tree but not in the Second, this will do it:

```
@Echo Off
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: FindMiss.cmd @Echo Off
:: Finds files missing from a second directory tree
:: Written by TheOucaste
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
If [%1]==[] Goto _Usage
If [%2]==[] Goto _Usage
SetLocal EnableDelayedExpansion
Set _DirA=%~f1
Set _DirB=%~f2
If Not %_DirA:~-1%==\ Set _DirA=%_DirA%\
If Not %_DirB:~-1%==\ Set _DirB=%_DirB%\
If Not Exist "%_DirA%" (Echo %_DirA% does not exist)&Goto _Usage
If Not Exist "%_DirB%" (Echo %_DirB% does not exist)&Goto _Usage
:: set up info lines for each section of the report
Echo Files that are found in folder %_DirA% but NOT in folder %_DirB%>duplicat.txt
Echo.>>duplicat.txt
:: Check root folders
For /F "Delims=" %%I In ('Dir /B /O:N /A-D "%_DirA%" 2^>Nul') Do If NOT EXIST "%_DirB%%%I" Echo %%I>>duplicat.txt
:: Check Subfolders of _DirA
For /F "Delims=" %%A In ('Dir /B /O:N /AD /S "%_DirA%" 2^>Nul') Do (
Set _CD=%%A
Call Set _CD=%%_CD:%_DirA%=%%
For /F "Delims=" %%I In ('Dir /B /O:N /A-D "%_DirA%!_CD!" 2^>Nul') Do If NOT EXIST "%_DirB%!_CD!\%%I" Echo !_CD!\%%I>>duplicat.txt
)
:: Cleanup
Cls
More duplicat.txt
Echo Report saved as %~dp0duplicat.txt
Goto :EOF
:_Usage
Echo.
Echo %~n0 [drive:][path]Folder1 [[drive:]path]Folder2
Echo.
Echo.        Compares two folder trees and Lists the files missing in the second
Echo.        [drive:][path]Folder1 - First Folder Tree
Echo.        [drive:][path]Folder2 - Second Folder Tree
Echo.
Echo.        The folders must both exist. If Drive is not specified,
Echo.        assumes path is relative to the current folder.
```
HTH

Jerry


----------



## Neablis (Jun 16, 2009)

Thank you Outcaste that script should work perfect for my project. 

BTW I Had to add 


```
if exists duplicat.txt del duplicat.txt
```
in the code because the results kept getting added together messing with my data. 

BTW Its very Elegant code, i like it. ray:


----------



## TheOutcaste (Mar 19, 2009)

You're Welcome!

I didn't use an "If exist delete" statement because the file is automatically deleted on every run when the header is written to the file by this line:

```
Echo Files that are found in folder %_DirA% but NOT in folder %_DirB%>duplicat.txt
```
The single > creates a new file.

Guess I should have used one though, just in case someone doesn't need that header in the output. Thanks for pointing that out.:wave:

If your issue has been resolved you can mark this thread Solved by using the Thread Tools at the Top Right of this thread (above the first post) :grin:

Jerry


----------

