:: since you state the objects in both trees are always the same, I have not included a comparison to verify the 2 trees match If exist %Resource%\* del /q %Resource%\* If exist %Output%\Differences\*.txt del %Output%\Differences\*.txt If exist %Output%\%Output_File% del %Output%\%Output_File% Set Object_List=Object_List_minus_Direcotries.txt :: if you already have an object list, place it in the Input folder and remove lines 24 through 35 of this script :: If the trees are not under the same parent, then include the full path for Tree A and B below :: If you remove the 3 lines above, then you need to replace the 2 below variables with values :: the next 3 lines are only needed if you want to predefine multiple directories for comparison Set environment=D:\path\to\parent directory containing the different tree structures (if they do not share a parent, then make this the drive) Set Output=D:\path\to\where your want to view your\Output_Files Set Resource=D:\path\to\batch_file_home\Resource_Files Set Input=D:\path\to\batch_file_home\Input_Files :: set a workspace location for the script outside of the trees being reviewed :: This script compares the contents of 2 trees If the directory trees have different parents, then read through the scripts comments. If both directory structures are under the same parent, then all you need to do is update the first 8 variables in the script. and creates a folder with a diff file for each non-matching object.creates a list of the file differences (named File_differences.txt in the output folder).takes 2 directory trees and compares each file in each tree.I modified a batch file I wrote for a CD process that should meet your need. S L O W :/Īt this point, a batch file makes sense: off The simplest solution is to use FORFILES instead, since it directly supports relative paths. The problem is getting the relative paths so that the hierarchy can be applied to another root. It is easy to iterate the folder hierarchy for a given root using FOR /R. Enter HELP FC or FC /? from the command prompt to get more information.Įxtending the solution to support subfolders is a bit tricky. There are a number of options to the FC command. Extra files in folder2 are simply ignored. The command will report on files that exist in folder1 but are missing in folder2. You can be more specific for the file mask in the first folder if you want. ![]() A single FC command can do what you want: fc folder1\* folder2\* Run the below commands to get the lists of files from C:\Temp\Folder1\ and C:\Temp\Folder2 folders and store the lists in the $Folder1List and $Folder2List variables.No need for a batch file. Related: Get-ChildItem: Listing Files, Registry and Certificates This time, you’ll use the Get-ChildItem command to get the list of files in a folder to compare with another folder’s list of files. Now that you know how to compare file contents, why not compare actual files from different locations? Comparing lists of files comes in handy when you’re managing two different servers where you keep the same folder for a service containing the same files. Comparing Lists of Files from Different Locations Bear in mind that the Compare-Object command compares source strings and not characters. Replace the path below with your chosen files’ path to compare.Ĭompare Objects wth PowerShell : Comparing File Contents with Case SensitivityĬomparing strings also works is similar to comparing file contents, where you can either specify the strings or use variables to represent the strings to compare. Open PowerShell, and run the below commands to get the content ( Get-Content) of the specified files into variables ( $file1 and $file2). Related: PowerShell Get-Content a PowerShell Tail Equivalent The Get-Content command retrieves the content of a file from a specified location. How would you know if both files keep the same content? Running the Get-Content command can be the solution. Imagine you’re keeping two different files with the same content. PowerShell version 5.1 is used in this tutorial. This tutorial will be a hands-on demonstration, but so long as you have a Windows PC with PowerShell, you’re good to go. Related: Understanding PowerShell Objects Prerequisites ![]() Save your eyes the strain and effectively compare objects! In this tutorial, you’ll learn to compare objects with PowerShell, like files or file contents, while customizing the output. But doesn’t eyeballing files and contents strain your eyes at some point? Well, lucky for you, you can compare objects with PowerShell the way you want to with the Compare-Object command. Sure enough, comparing files or file contents in Windows is one thing you’ve been doing through the years.
0 Comments
Leave a Reply. |