Function Repository Resource:

FileSystemCompare

Source Notebook

Compare the contents of two directories

Contributed by: Jon McLoone

ResourceFunction["FileSystemCompare"][path1,path2]

compares the contents of two folders in the local file system.

Details and Options

FileSystemCompare has the following options:
"FilePattern""*"include files whose names match the given string pattern
"Depth"Infinityinclude files that are in subdirectories up to the given depth
"ComparisonMethod""Name"the file property used to compare files
Some possible values for "ComparisonMethod" are:
"AbsoluteFileName"the full path to the file, resolving symbolic links as needed
"ByteCount"the size of the file on disk in bytes
"CreationDate"when the file was originally created
"DirectoryName"the DirectoryName of the file
"FileAssociation"the path to the default application used to open the file
"FileAttributes"file system attributes for the file
"FileBaseName"the FileBaseName of the file
"FileExtension"the FileExtension of the file
"FileType"the FileType of the file
"LastAccessDate"when the file was last accessed
"LastChangeDate"when the file's attributes were last changed
"LastModificationDate"when the file was last modified
"OwnerName"the user name of the file's owner
"Permissions"the permissions of the file
"ReadOnly"whether or not the file is marked as read only

Examples

Basic Examples (3) 

Create two example directories with identical contents:

In[1]:=
dir1 = CreateDirectory[
   FileNameJoin[{$TemporaryDirectory, CreateUUID[]}]];
Export[FileNameJoin[{dir1, "image.png"}], Plot[Sin[x], {x, 0, 10}]];
Export[FileNameJoin[{dir1, "text.txt"}], ResourceData["Gettysburg Address"]];
dir2 = CopyDirectory[dir1, FileNameJoin@{$TemporaryDirectory, CreateUUID[]}];

A comparison shows no changes:

In[2]:=
ResourceFunction["FileSystemCompare"][dir1, dir2]
Out[2]=

Show changes with a file deleted from the second directory:

In[3]:=
DeleteFile[FileNameJoin[{dir2, "text.txt"}]];
ResourceFunction["FileSystemCompare"][dir1, dir2]
Out[4]=

Show changes with a file added to the second directory:

In[5]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/260eca71-3a26-44e8-a76a-d98e99f6aa9b"]
Out[6]=

Options (6) 

FilePattern (2) 

By default, FileSystemCompare will look at all files:

In[7]:=
ResourceFunction["FileSystemCompare"][dir1, dir2]
Out[7]=

Specify a pattern to match file names:

In[8]:=
ResourceFunction["FileSystemCompare"][dir1, dir2, "FilePattern" -> "*.png"]
Out[8]=

Depth (2) 

By default, FileSystemCompare recursively searches subdirectories:

In[9]:=
subdir = CreateDirectory[FileNameJoin[{dir1, "sounds"}]];
Export[FileNameJoin[{subdir, "middlec.mid"}], Sound[{SoundNote["C"], SoundNote["G"]}]];
ResourceFunction["FileSystemCompare"][dir1, dir2]
Out[10]=

Specify a level to limit the depth of the search:

In[11]:=
ResourceFunction["FileSystemCompare"][dir1, dir2, "Depth" -> 1]
Out[11]=

ComparisonMethod (2) 

By default, only file names are used:

In[12]:=
Export[FileNameJoin[{dir2, "text.txt"}], "This text is different"];
ResourceFunction["FileSystemCompare"][dir1, dir2]
Out[8]=

Use FileHash to test if the contents of files are different:

In[13]:=
ResourceFunction["FileSystemCompare"][dir1, dir2, "ComparisonMethod" -> FileHash]
Out[13]=

Use a file property to compare files:

In[14]:=
ResourceFunction["FileSystemCompare"][dir1, dir2, "ComparisonMethod" -> "CreationDate"]
Out[14]=
In[15]:=
ResourceFunction["FileSystemCompare"][dir1, dir2, "ComparisonMethod" -> "LastModificationDate"]
Out[15]=

Neat Examples (1) 

Find how many new documentation notebooks were added and removed in Mathematica 12.0 (this only works if you have 11.3 installed as well):

In[16]:=
current = FileNameJoin[{$InstallationDirectory, "Documentation"}]
Out[16]=
In[17]:=
old = StringReplace[current, "12.0" -> "11.3"]
Out[17]=
In[18]:=
CountsBy[ResourceFunction["FileSystemCompare"][old, current, "FilePattern" -> "*.nb"], If[MissingQ[First[#]], "Added", "Removed"] &]
Out[18]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Version History

  • 1.0.0 – 07 February 2019

Related Resources

License Information