![]() |
![]() |
![]() |
|||||
![]() |
![]() |
![]() |
![]() |
![]() |
|||
| 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: * 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 |
|
|||||||
| Programming A discussion forum for programs and programming used in tech-related businesses. |
![]() |
|
|
LinkBack | Thread Tools |
|
|
#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 ) |
|
|
|
| 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 |
|
|
#2 (permalink) |
|
TSF Enthusiast
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 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? |
|
|
|
|
|
#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 |
|
|
|
|
|
#4 (permalink) |
|
TSF Enthusiast
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 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 |
|
|
|
|
|
#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.
|
|
|
|
|
|
#6 (permalink) |
|
TSF Enthusiast
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.
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.
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? |
|
|
|
|
|
#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 BTW Its very Elegant code, i like it.
|
|
|
|
|
|
#8 (permalink) |
|
TSF Enthusiast
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 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? |
|
|
|
![]() |
| Thread Tools | |
|
|