Welcome to Tech Support Forum home to more then 136,000 problems solved. Issues have included: Spyware, Malware, Virus Issues, Windows, Microsoft, Linux, Networking, Security, Hardware, and Gaming Getting your problem solved is as easy as:
1. Registering for a free account
2. Asking your question
3. Receiving an answer

Registered members:
* Get free support
* Communicate privately with other members (PM).
* Removal of this message
* See fewer ads.
* And much more..

 



Want to know how to post a question? click here Having problems with spyware and pop-ups? First Steps
Go Back   Tech Support Forum > The IT Pro > Programming
User Name
Password
Site Map Register Donate Rules Blogs Mark Forums Read


Programming A discussion forum for programs and programming used in tech-related businesses.

Reply
 
LinkBack Thread Tools
Old 06-16-2009, 10:51 AM   #1 (permalink)
Registered User
 
Join Date: Jun 2009
Posts: 4
OS: Windows 7


Creating batch file to Check Directories

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!

Code:
@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
)
Neablis is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Important Information
Join the #1 Tech Support Forum Today - It's Totally Free!

TechSupportForum.com is a leading support website for your computer needs. We offer free, friendly and personalized computer support. Why pay to have your computer fixed when you can do it for free.

Join TechSupportforum.com Today - Click Here

Old 06-16-2009, 01:31 PM   #2 (permalink)
TSF Enthusiast
 
TheOutcaste's Avatar
 
Join Date: Mar 2009
Location: Portland, OR
Posts: 815
OS: MS-Dos 6.22 - Win7


Re: Creating batch file to Check Directories

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:
Code:
dir /B /O        "%dirA%       " >dirA.txt
dir /B /O        "%dirB%       " >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
__________________
Microsoft MVP - Windows Desktop Experience
Of course I know all the answers; I just don't always match the answers to the right questions.
Rated R for Violence -- When your PC flies through a window, that's violent, right?
TheOutcaste is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 06-16-2009, 01:50 PM   #3 (permalink)
Registered User
 
Join Date: Jun 2009
Posts: 4
OS: Windows 7


Re: Creating batch file to Check Directories

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

Code:
dir /B /O %dirA% >dirA.txt
dir /B /O %dirB% >dirB.txt
Code:
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.
Neablis is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 06-16-2009, 02:44 PM   #4 (permalink)
TSF Enthusiast
 
TheOutcaste's Avatar
 
Join Date: Mar 2009
Location: Portland, OR
Posts: 815
OS: MS-Dos 6.22 - Win7


Re: Creating batch file to Check Directories

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:\dirB\test1.txt

You need to remove the root folder from the string you are checking:
Code:
@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
__________________
Microsoft MVP - Windows Desktop Experience
Of course I know all the answers; I just don't always match the answers to the right questions.
Rated R for Violence -- When your PC flies through a window, that's violent, right?

Last edited by TheOutcaste; 06-16-2009 at 02:52 PM. Reason: typo
TheOutcaste is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 06-16-2009, 02:48 PM   #5 (permalink)
Registered User
 
Join Date: Jun 2009
Posts: 4
OS: Windows 7


Re: Creating batch file to Check Directories

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.
Neablis is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 06-16-2009, 03:48 PM   #6 (permalink)
TSF Enthusiast
 
TheOutcaste's Avatar
 
Join Date: Mar 2009
Location: Portland, OR
Posts: 815
OS: MS-Dos 6.22 - Win7


Re: Creating batch file to Check Directories

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.

Code:
@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:
Code:
@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
__________________
Microsoft MVP - Windows Desktop Experience
Of course I know all the answers; I just don't always match the answers to the right questions.
Rated R for Violence -- When your PC flies through a window, that's violent, right?
TheOutcaste is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 06-17-2009, 07:19 AM   #7 (permalink)
Registered User
 
Join Date: Jun 2009
Posts: 4
OS: Windows 7


Re: Creating batch file to Check Directories

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

BTW I Had to add

Code:
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.
Neablis is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 06-17-2009, 09:45 AM   #8 (permalink)
TSF Enthusiast
 
TheOutcaste's Avatar
 
Join Date: Mar 2009
Location: Portland, OR
Posts: 815
OS: MS-Dos 6.22 - Win7


Re: Creating batch file to Check Directories

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:
Code:
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.

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)

Jerry
__________________
Microsoft MVP - Windows Desktop Experience
Of course I know all the answers; I just don't always match the answers to the right questions.
Rated R for Violence -- When your PC flies through a window, that's violent, right?
TheOutcaste is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT -7. The time now is 02:04 PM.



Copyright 2001 - 2009, Tech Support Forum
Home Tips Plus | Outdoor Basecamp | Automotive Support Forum

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85