powershell - Compare directories exactly - including moved files -


my aim compare 2 directories - including structure of directories , sub-directories.

i need this, because want monitor if in folder e:\path2 changed. case copy of full folder in c:\path1. if changes has done in 2 directories.

it important us, because if changed in directory (accidentally or not) break down other functions in our infrastructure.

this script i've written:

# compare files "copy default folder" # script compares files , folders synced every client. # source: https://mcpmag.com/articles/2016/04/14/contents-of-two-folders-with-powershell.aspx  # 1. compare content , name of every file recursively $sourcedocshash = get-childitem -recurse –path c:\path1 | foreach  {get-filehash –path $_.fullname} $destdocshash = get-childitem -recurse –path e:\path2 | foreach  {get-filehash –path $_.fullname} $resultdocshash = (compare-object -referenceobject $sourcedocshash -differenceobject $destdocshash -property hash -passthru).path  # 2. compare name of every folder recursively $sourcefolders = get-childitem -recurse –path c:\path1 #| {!$_.psiscontainer} $destfolders = get-childitem -recurse –path e:\path2 #| {!$_.psiscontainer}  $comparefolders = compare-object -referenceobject $sourcefolders -differenceobject $destfolders -passthru -property name $resultfolders = $comparefolders |  select-object fullname  # 3. check if unc-path reachable # source: https://stackoverflow.com/questions/8095638/how-do-i-negate-a-condition-in-powershell # printout, if unc-path not available. if(-not (test-path \\bb-srv-025.ftscu.be\dip$\settings\ftscube\default-folder-on-client\00_ftscube)){   $uncpathreachable = "unc-path not reachable , maybe" }  # 4. count files statistics # source: https://stackoverflow.com/questions/14714284/count-items-in-a-folder-with-powershell $count = (get-childitem -recurse –path e:\path2 | measure-object ).count;  # final: print out result check_mk if($resultdocshash -or $resultfolders -or $uncpathreachable){   echo "2 copy-default-folders-c-00_ftscube files-and-folders-count=$count critial - $uncpathreachable following files or folders has been changed: $resultdocs $resultfolders (none if empty after ':')" } else{   echo "0 copy-default-folders-c-00_ftscube files-and-folders-count=$count ok - no files has changed" } 

i know output not perfect formatted, it's ok. :-)

this script spots following changes successfully:

  • create new folder or new file
  • rename folder or file -> shown error, output empty. can live that. maybe sees reason. :-)
  • delete folder or file
  • change file content

this script not spot following changes:

  • move folder or file other sub-folder. script still says "everything ok"

i've been trying lot of things, not solve this.

does can me how script can extended spot moved folder or file?

so want synchronize 2 folders , note changes made on that:

i suggest use

sync-folder script

or

freefile sync.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -