Compare between two folders

This topic contains 6 replies, has 2 voices, and was last updated by  Rob Simmers 1 week, 2 days ago.

  • Author
  • #101807


    i have 2 folders that update daily by new files.
    one folder contain csv file and the other regulaer text file.
    I need to compare every day csv file against text file and see if they contatin the same number of rows.
    (the compare done only by files that have the same creation day).
    The compare should done by same name and creation day.
    for example:
    abcd.txt compare with abcd.csv that create in 05/06/2018

    I have a problem with the creation day because he contain many things without date.
    i need only date dd/mm/yyyy, and i can`t use split because i get error with this type of use.

    $Folder1 = Get-ChildItem "D:\a\file" | select-Object CreationTime,name
    $arr1 = @($Folder1.CreationTime,$'.')

    can you help with this issue?
    Help me to continu this script till the end.
    i`m new in this language so my experennice is low.


  • #101825

    Rob Simmers

    This is a tougher solution for a beginner, but we can help. First, you don't want to try to parse the date that way. If you are trying to simply compare a day, you can convert a date to a DayOfYear. The first thing you need to do is get everything into a single place, so we create and empty array and add file objects from both directories. Once we have that, you can use a Calculated Expression to basically run a function against each row.

    $files = @()
    $files += Get-ChildItem C:\Scripts\A
    $files += Get-ChildItem C:\Scripts\B
    $files = $files | 
             Select FullName,


    FullName                            Name                   LastWriteTime        DayOfYear
    --------                            ----                   -------------        ---------
    C:\Scripts\A\New Text Document.txt  New Text Document.txt  6/4/2018 11:54:23 AM       155
    C:\Scripts\A\New Text Document2.txt New Text Document2.txt 6/5/2018 10:49:34 AM       156
    C:\Scripts\B\New Text Document.csv  New Text Document.csv  6/4/2018 11:54:11 AM       155
    C:\Scripts\B\New Text Document2.csv New Text Document2.csv 6/5/2018 11:54:45 AM       156

    Now, the next step is row count. Regardless of the file extension, if it's in CSV format, you can use Import-CSV to parse it into an object. Try to parse these files with Import-CSV and use the .Count property to get the row count.

  • #101900


    thanks for the start.

    two things:
    *I need to sperate the .txt and .csv from the names.
    to do it with split method?
    if yes, how can i split something and drop what write after the dot (.txt,.csc)

    *Second i need to two take from last write time only the dd/mm/yyyy and compare it to current date.
    how can i do it?


  • #101921

    Rob Simmers

    Get-ChildItem has other properties that can be accessed. If you just return a record, there are default properties that are shown, but you can return all properties. Note BaseName is just the name of the file, so you can add that to the Select statement to have it included in your data.

    PS C:\Users\Rob> 
    $files = @()
    $files += Get-ChildItem C:\Scripts\A
    $files += Get-ChildItem C:\Scripts\B
    PS C:\Users\Rob> $files | Select  -First 1
        Directory: C:\Scripts\A
    Mode                LastWriteTime         Length Name                                                                                                                                             
    ----                -------------         ------ ----                                                                                                                                             
    -a----         6/4/2018  11:54 AM             54 New Text Document.txt                                                                                                                            
    PS C:\Users\Rob> $files | Select -Property * -First 1
    PSPath            : Microsoft.PowerShell.Core\FileSystem::C:\Scripts\A\New Text Document.txt
    PSParentPath      : Microsoft.PowerShell.Core\FileSystem::C:\Scripts\A
    PSChildName       : New Text Document.txt
    PSDrive           : C
    PSProvider        : Microsoft.PowerShell.Core\FileSystem
    PSIsContainer     : False
    Mode              : -a----
    VersionInfo       : File:             C:\Scripts\A\New Text Document.txt
                        Debug:            False
                        Patched:          False
                        PreRelease:       False
                        PrivateBuild:     False
                        SpecialBuild:     False
    BaseName          : New Text Document
    Target            : {}
    LinkType          : 
    Name              : New Text Document.txt
    Length            : 54
    DirectoryName     : C:\Scripts\A
    Directory         : C:\Scripts\A
    IsReadOnly        : False
    Exists            : True
    FullName          : C:\Scripts\A\New Text Document.txt
    Extension         : .txt
    CreationTime      : 6/5/2018 10:47:01 AM
    CreationTimeUtc   : 6/5/2018 2:47:01 PM
    LastAccessTime    : 6/5/2018 10:47:01 AM
    LastAccessTimeUtc : 6/5/2018 2:47:01 PM
    LastWriteTime     : 6/4/2018 11:54:23 AM
    LastWriteTimeUtc  : 6/4/2018 3:54:23 PM
    Attributes        : Archive

    The second question it if you are trying to compare files from the same day, you should use DayOfYear. Here is a basic solution:

    $files = @()
    $files += Get-ChildItem C:\Scripts\A
    $files += Get-ChildItem C:\Scripts\B
    $files = $files | 
             Select BaseName,
                    @{Name='RowCount';Expression={@(Import-CSV -Path $_.FullName).Count}}
    #Group the files on the day of last write
    $grouped = $files | Group-Object -Property DayOfYear
    #Loop through the days 
    foreach ($group in $grouped) {
        #Assume there are two files, we reference them by index to do a comparision
        if ($group.Group[0].RowCount -ne $group.Group[1].RowCount) {
            $arr = @(
            "Row count for {0}({1}) doesn't match {2}({3})." -f $arr
  • #102199


    again many thanks for the anwser.
    but you talk on spesific situation that i have:
    one file from type x.txt in one directorey and x.csv in other direcorey.
    here it`s a totaly diffrent situation, i have alot of files.
    so my compare need to by name and currect date.
    i try alot of things, try to work with split method to cut the ending(.csv,.txt) to compare between two.
    but all get bad.
    i tried couple other staff, failed.

    someone have idea?

  • #102404



  • #102415

    Rob Simmers

    If the file names are identical, you can still leverage Group-Object. Just group on the BaseName:

    $grouped = $files | Group-Object -Property BaseName

    Then you will need to add an additional IF statement to do a DayOfYear check on the files to ensure they were written on the same day.

You must be logged in to reply to this topic.