Skip to content

Find any difference between two folder by leveraging the power of c# Linq thru powershell. No need to recompile if your selection criteria changes.

License

Notifications You must be signed in to change notification settings

sietsevdschoot/FolderDiff

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Folder Diff

FolderDiff is a command line tool to compare contents of two folders.

####Features:

  • Use simple or complex file selection criteria
  • Combine file selectors
  • Supports PowerShell and Windows Command Prompt
  • Supports extremely long file- and foldernames up to 32.767 characters (Thanks Delimon.Win32.IO Library!)
  • No recompilation required when search criteria changes
  • Results can be presented / projected using PowerShell

Installation

  1. Fork the repository or download the zip.
  2. Extract the zip.
  3. Navigate to FolderDiffPowerShell folder

Usage

Usage: .\FolderDiff.ps1 referenceFolder differenceFolder [supportLongFilenames]

	ReferenceFolder: Full path of reference folder
	DifferenceFolder: Full path of difference folder
	[SupportLongFilenames]: optional boolean to enable long filenames support (default is $false)

Default file selection is to look for files:

  • That are newer in the differenceFolder
  • Or, files that only exists in the differenceFolder.
Usage in PowerShell
.\FolderDiff.ps1 C:\Backup\PC\Development\ C:\Backup\Laptop\Development\ | %{ $_.FullName } 

or

.\FolderDiff.ps1 C:\Backup\PC\Development\ C:\Backup\Laptop\Development\ $true | %{ $_.FullName }
Usage in Command Prompt (DOS)
FolderDiff.bat C:\Backup\PC\Development\ C:\Backup\Laptop\Development\

Selecting files

Selecting files can be done by defining one or more file selectors. The proces of selecting files is done by evaluating all file selectors and take the distinct set of all results.

The signature of a file selector in PowerShell look this:

{ param($diffFile, $referenceFiles) $diffFile.File.Extension -eq '.txt' }

A file selector is basically a delegate that takes a differenceFile and all referenceFiles and returns a boolean to indicate if the differenceFile should be included in the results.

Default file-selection.

The default file selectors are defined in FolderDiffPowerShell\FolderDiff.ps1 in the function FolderDiff-Search.

	@($folderDiff.DiffFolder($referenceFolder, $differenceFolder, $supportLongFilesNames,
		{ param($diffFile, $referenceFiles)
		
			$refFile = (Get-File $referenceFolder $diffFile.RelativePath)
			$diffFileIsNewer = $diffFile.File.LastWriteTime -gt $refFile.LastWriteTime 
			
			$refFile.Exists -and $diffFileIsNewer 
		},
		{ param($diffFile, $referenceFiles) 
			-Not (File-Exists $referenceFolder $diffFile.RelativePath) 
		}
	))

These file selectors could also be rewritten in a single file selector:

	@($folderDiff.DiffFolder($referenceFolder, $differenceFolder, $supportLongFilesNames,
		{ param($diffFile, $referenceFiles)
		
			$refFile = (Get-File $referenceFolder $diffFile.RelativePath)
			$diffFileIsNewer = $diffFile.File.LastWriteTime -gt $refFile.LastWriteTime 
			
			($refFile.Exists -and $diffFileIsNewer) -or (-not $refFile.Exists)
		}
	))

These conditions can be changed if necessary. Feel free to do so.

Selectable properties

The files are expressed as a type MyFileInfo which wraps FileInfoBase together with the Relative path.

If we search in a folder c:\Backup\ and a file is found in c:\Backup\Pc\Folder\File.txt
then the RelativePath is Pc\Folder\File.txt

| Name        	| Name              	| Type                                     	|
|---------------|-------------------	|-------------------------------------------|
| MyFile        | MyFile.File           |                                           |
| RelativePath 	|                   	| string                                   	|
| File         	|                   	| System.IO.Abstractions.FileInfoBase      	|
|              	| Attributes        	| System.IO.FileAttributes                 	|
|              	| CreationTime      	| datetime                                 	|
|              	| CreationTimeUtc   	| datetime                                 	|
|              	| Directory         	| System.IO.Abstractions.DirectoryInfoBase 	|
|              	| DirectoryName     	| string                                   	|
|              	| Exists            	| bool                                     	|
|              	| Extension         	| string                                   	|
|              	| FullName          	| string                                   	|
|              	| IsReadOnly        	| bool                                     	|
|              	| LastAccessTime    	| datetime                                 	|
|              	| LastAccessTimeUtc 	| datetime                                 	|
|              	| LastWriteTime     	| datetime                                 	|
|              	| LastWriteTimeUtc  	| datetime                                 	|
|              	| Length            	| long                                     	|
|              	| Name              	| string                                   	|

Some cmdlets are available to help in selecting files

 *   [bool] File-Exists $path $relativePath
 * [MyFile] Get-File $path $relativePath
Examples:

If we need to get the main folders which contain newer or unique files.
We can write: only list the folders which have 6 subdirectories or less.

.\FolderDiff.ps1 C:\Backup\PC\Development\ C:\Backup\Laptop\Development\ `
  | %{ $_.Directory.FullName } | Get-Unique | ? { ($_.Split("\")).Length -lt 6 }

I will update the examples on request.

Contributing

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D

History

20-12-2015 - Initial Readme.md

Credits

  • Delimon.Win32.IO by Johan Delimon

    It is a great library to overcome the windows file system limitation on folder- and filename length.

  • System.IO.Abstractions by Tatham Oddie

    Great and essential Nuget package if you want to write testable code which relies on the FileSystem.
    I use this package for all my IO projects. Make sure to also checkout System.IO.Abstractions.TestingHelpers

About

Find any difference between two folder by leveraging the power of c# Linq thru powershell. No need to recompile if your selection criteria changes.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published