1.

Solve : batch file to compare folders and subfolders?

Answer»

I am trying to write a batch file to compare the contents of a folder and its subfolders and return true or false based on whether the files they contain are the same. (It will be part of an if statement that deletes one of the folders if they are equal).

The closest I've found is the fc command, but that doesn't return true or false.

Thanks for any help you can give!what code you have used with that fc ? maybe errorlevel can help here ?


EXAMPLE:
Code: [Select]H:\Users\_CORE7>dir C:\somefakedir
Volume in drive C has no label.
Volume Serial Number is 3C35-D9EE

Directory of C:\

File Not Found

H:\Users\_CORE7>ECHO %errorlevel%
1
Code: [Select]H:\Users\_CORE7>dir C:\
Volume in drive C has no label.
Volume Serial Number is 3C35-D9EE

Directory of C:\

0 File(s) 0 bytes
7 Dir(s) 159483252736 bytes free

H:\Users\_CORE7>echo %errorlevel%
0i was just EXPERIMENTING with the command with a couple of text files,
but here it is:

Code: [Select]fc "c:\New Folder\New Text Document.txt" "c:\New Folder (2)\New trext Document.txt"and it said:
Code: [Select]Comparing files "c:\New Folder\New Text Document.txt" and "c:\New Folder (2)\New trext Document.txt"
FC: no differences encountered
So I don't know how/if you can take that output and turn it into "true"

also, does fc work for folders as well as files?Quote from: your.name812 on July 24, 2009, 12:27:57 PM

i was just experimenting with the command with a couple of text files,
but here it is:

Code: [Select]fc "c:\New Folder\New Text Document.txt" "c:\New Folder (2)\New trext Document.txt"and it said:
Code: [Select]Comparing files "c:\New Folder\New Text Document.txt" and "c:\New Folder (2)\New trext Document.txt"
FC: no differences encountered
So I don't know how/if you can take that output and turn it into "true"

also, does fc work for folders as well as files?

Code: [Select]S:\>echo hello>file1.txt

S:\>copy file1.txt file2.txt
1 file(s) copied.

S:\>echo goodbye>file3.txt

S:\>set result=false & fc file1.txt file2.txt>nul && set result=true

S:\>echo %result%
true

S:\>set result=false & fc file1.txt file3.txt>nul && set result=true

S:\>echo %result%
false

Thanks guys.

Trout, that looks like what I want, but I'm rather new to dos commands so could you explain the syntax of this line?:

S:\>set result=false & fc file1.txt file2.txt>nul && set result=true

Also, would this work with folders instead of files? and would it include subfolders?

Thanks!Quote from: your.name812 on July 24, 2009, 01:05:02 PM

could you explain the syntax of this line?:

S:\>set result=false & fc file1.txt file2.txt>nul && set result=true

(1)Create a VARIABLE called 'result' and set its value to the string 'false'

(2) compare 2 files and send the output to the nul device so it doesn't SHOW on screen

(3) use the && operator to test the errorlevel (the command after the && is only executed if there was no error, i.e. no differences were found) and if no differences were found set the value of the variable 'result' to 'true'
Quote
Also, would this work with folders instead of files? and would it include subfolders?

Folders yes

Code: [Select]fc dir1\*.* dir2\*.*
Subfolders no

Excellent. Thanks very much


Discussion

No Comment Found