There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
antivirus audio avg avg 8 backup bios boot browser bsod computer cpu crash css dell desktop driver drivers dvd email error excel explorer firefox firefox 3 freeze game graphics hard drive hardware hijackthis hjt install internet internet explorer itunes javascript keyboard lan laptop malware missing monitor msn network networking openoffice outlook outlook 2003 outlook express php popups problem router screen seo slow sound sp3 spyware trojan usb video virus vista vundo windows windows vista windows xp wireless word
DOS/PDA/Other
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Operating Systems > DOS/PDA/Other >
Solved: Batch file that compares contents of two folders


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

 
Thread Tools
Reconsniper1's Avatar
Junior Member with 10 posts.
 
Join Date: Apr 2008
17-Apr-2008, 11:33 PM #1
Solved: Batch file that compares contents of two folders
Hello All! I have searched the web looking for the answer to several of my questions in a attempt to find the answer to many of my batch questions. Some easy, some on the more difficult side. I came across this website and now it is the first place I search for my answers because the answers are normally spot on. Here is my newest question.

I am attempting to make a batch file that will compares the files of c:\test1 with c:\tests2.

I know you can do xcopy and have it copy the files that are not in one folder or files that have changed from one folder to the other folder, but there a way for it to produce a .txt showing what those files are without it actually moving the files.

Thanks,
Tim
Squashman's Avatar
Distinguished Member with 12,216 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
18-Apr-2008, 12:27 AM #2
Elvandil's Avatar
Moderator with 26,771 posts.
 
Join Date: Aug 2003
Location: Vermont
18-Apr-2008, 12:34 AM #3
Use the "delta" command.
Squashman's Avatar
Distinguished Member with 12,216 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
18-Apr-2008, 01:05 AM #4
That DirMatch would probably work as well. Good finds.
devil_himself's Avatar
Distinguished Member with 4,775 posts.
 
Join Date: Apr 2007
Location: India
Experience: Advanced
18-Apr-2008, 02:14 AM #5
Here's a Pure Batch Code

Code:
:bof

    @echo off
    setlocal

:init
   
    set dirA=c:\src
    set dirB=c:\dest

    if not exist "%dirA%" echo dirA not found & goto :EOF
    if not exist "%dirB%" echo dirB not found & goto :EOF

    for /f "delims=" %%a in ('dir /b /a-d "%dirA%" 2^>NUL') do if not exist "%dirB%%%a" echo %%a does not exist in "dirB"   
    for /f "delims=" %%a in ('dir /b /a-d "%dirB%" 2^>NUL') do if not exist "%dirA%%%a" echo %%a does not exist in "dirA"

  
:eof
Reconsniper1's Avatar
Junior Member with 10 posts.
 
Join Date: Apr 2008
18-Apr-2008, 05:51 PM #6
Hello everyone. Thanks for the responses.

I'm not looking for a 3rd party program to do this but if I can't get a batch code to work then I will probably resort to it.

Is Delta a linux command because XP doesn't have it?

I tried your batch Devil but it didn't do anything. I inserted the Source and Dest but i don't see an output file.

I came across this batch and it doesn't seem to be outputting the results. It basically lists all the icons on my desktop into the missing.log.

@echo off
setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in ('dir/b/a-d') do (
if exist C:\test\%%a (
fc %%a C:\test1\%%a >> compare.log
) else (
echo %%a is missing >> missing.log
)
)

Pause




Quote:
Originally Posted by devil_himself View Post
Here's a Pure Batch Code

Code:
:bof

    @echo off
    setlocal

:init
   
    set dirA=c:\src
    set dirB=c:\dest

    if not exist "%dirA%" echo dirA not found & goto :EOF
    if not exist "%dirB%" echo dirB not found & goto :EOF

    for /f "delims=" %%a in ('dir /b /a-d "%dirA%" 2^>NUL') do if not exist "%dirB%%%a" echo %%a does not exist in "dirB"   
    for /f "delims=" %%a in ('dir /b /a-d "%dirB%" 2^>NUL') do if not exist "%dirA%%%a" echo %%a does not exist in "dirA"

  
:eof
Squashman's Avatar
Distinguished Member with 12,216 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
18-Apr-2008, 06:58 PM #7
Quote:
Originally Posted by Reconsniper1 View Post
Hello everyone. Thanks for the responses.

I'm not looking for a 3rd party program to do this but if I can't get a batch code to work then I will probably resort to it.

Is Delta a linux command because XP doesn't have it?
There is a download for the utility right in the link he gave you.
devil_himself's Avatar
Distinguished Member with 4,775 posts.
 
Join Date: Apr 2007
Location: India
Experience: Advanced
18-Apr-2008, 09:05 PM #8
Open The Command Prompt And Run The Batch It Will Echo The Results On The Screen !
Reconsniper1's Avatar
Junior Member with 10 posts.
 
Join Date: Apr 2008
18-Apr-2008, 09:08 PM #9
I tried that. It just pops up and closes right away. So I tried to throw PAUSE with it and it still didnt stay up. it like instantly opens / closes.
Squashman's Avatar
Distinguished Member with 12,216 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
18-Apr-2008, 09:51 PM #10
No you are not doing it correctly

Launch the batch file from with in the CMD box. So open up a cmd prompt first. Change directory to where the batch file is. Then type the batch file name to execute it.
Reconsniper1's Avatar
Junior Member with 10 posts.
 
Join Date: Apr 2008
18-Apr-2008, 09:58 PM #11
Thank you! That did work. All my other batch files work from just clicking on the icon so I just assumed they would all work that way. Is there any way for to have it output the the file names in a log file rather than on the screen.
Squashman's Avatar
Distinguished Member with 12,216 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
18-Apr-2008, 10:07 PM #12
batchfile.bat >> logfile.txt
Reconsniper1's Avatar
Junior Member with 10 posts.
 
Join Date: Apr 2008
18-Apr-2008, 10:42 PM #13
awesome!!! worked like a charm!
devil_himself's Avatar
Distinguished Member with 4,775 posts.
 
Join Date: Apr 2007
Location: India
Experience: Advanced
18-Apr-2008, 11:00 PM #14
Glad You Got It Working !

TheOutcaste's Avatar
Computer Specs
Senior Member with 1,537 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
18-Apr-2008, 11:53 PM #15
Quote:
Originally Posted by Reconsniper1 View Post
I came across this batch and it doesn't seem to be outputting the results. It basically lists all the icons on my desktop into the missing.log.

@echo off
setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in ('dir/b/a-d') do (
if exist C:\test\%%a (
fc %%a C:\test1\%%a >> compare.log
) else (
echo %%a is missing >> missing.log
)
)

Pause
The dir command in this program will do a directory of the current folder, so if the batch is on your desktop, it's comparing your desktop to the c:\test folder.
so change 'dir/b/a-d' to 'dir/b/a-d c:\test'
The If exist statement needs to test the other folder, otherwise the else portion will never execute. (You got a missing.log file because it was getting filenames off the desktop and checking to see if they exist in the test folder).
It also needs quotes in case a file name contains spaces
the fc command also needs the path added to do the compare, and needs quotes in case a file name contains spaces.
"delims=" will do the same as "tokens=* delims= "

So, if you actually want to compare the files and not just the names, it should read like this:

Code:
if exist compare.log del compare.log
if exist missing.log del missing.log
for /f "delims=" %%a in ('dir/b/a-d c:\test') do (
if exist "C:\test1\%%a" (
fc "c:\test\%%a" "C:\test1\%%a" >> compare.log
) else (
echo %%a is missing >> missing.log
)
)

Pause
The compare and missing logs will be created in the same folder as the batch file, so if it's on the desktop they will be there as well
This does not check for the case where a file exists in the test1 folder but not in the test folder.

HTH

Jerry
__________________
Of course I know all the answers ; I just don't always match the answers to the right questions

Warning -- Windows spoken here. (Rated R for Strong Language and Violence -- When your Windows PC flies through a window, that's violent, right?)
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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 On
Pingbacks are Off
Refbacks are Off

You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 05:19 PM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.