Author Posts

June 5, 2018 at 12:43 pm

hi,
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,$Folder1.name.split('.')

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.

thanks

June 5, 2018 at 4:19 pm

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,
                Name,
                LastWriteTime,
                @{Name='DayOfYear';Expression={$_.LastWriteTime.DayOfYear}}

Output:

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.

June 6, 2018 at 10:29 am

Hi,
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?

thanks.

June 6, 2018 at 2:48 pm

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
                    InternalName:     
                    OriginalFilename: 
                    FileVersion:      
                    FileDescription:  
                    Product:          
                    ProductVersion:   
                    Debug:            False
                    Patched:          False
                    PreRelease:       False
                    PrivateBuild:     False
                    SpecialBuild:     False
                    Language:         
                    
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,
                FullName,
                Name,
                LastWriteTime,
                @{Name='DayOfYear';Expression={$_.LastWriteTime.DayOfYear}},
                @{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 = @(
            $group.Group[0].Name,
            $group.Group[0].RowCount,
            $group.Group[1].Name,
            $group.Group[1].RowCount
        )

        "Row count for {0}({1}) doesn't match {2}({3})." -f $arr
    }
}

June 10, 2018 at 1:33 pm

hi,
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?

June 13, 2018 at 2:48 pm

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.