Reading multiple CSV files

This topic contains 2 replies, has 2 voices, and was last updated by  Olaf Soyk 3 months, 3 weeks ago.

  • Author
    Posts
  • #92464

    Venkata Kalyan
    Participant

    Hi,
    Everyday a folder will be created with date, and in that we have few csv files. I need to verify all the CSV files and find, if any csv file contains only one row (it should email).

    I wrote a script, which can look for a particular file, but finding current folder & looping through all csv files are not working. Can someone guide me on this?

    Below is the code, which I am having:

    $b=Import-Csv C:\Users\venkatak\Desktop\Scripts\CSV_Verification\Snap_SalesbyMonth-013120180220.csv | Measure-Object | select count
    if($b.Count-eq 1)
    {

    Send-MailMessage -To “Manager 1 " -From “Reports Admin " -SMTPServer test.com -Subject “Daily report” -Body "Error"
    }

  • #92473

    Olaf Soyk
    Participant

    Venkata,

    Everyday a folder will be created with date, and in that we have few csv files

    I think it would have been helpful if you told us the format of the date in the folder. You can list all folders with Get-ChildItem.
    To check all CSV files in that folder you would use Get-Child-Item again and parse them with a Foreach-Object.
    Before you start sending emails I would recommend to test your logic if it works at all. 😉
    And last but not leat: When you post code please format it as code here in the forum. It's easier to read, to copy, to debug and to understand.

  • #92474

    Olaf Soyk
    Participant

    If you're just interested in the count of rows you could use something like this:

    $TargetFolder = 'C:\Users\venkatak\Desktop\Scripts\CSV_Verification'
    Get-ChildItem -Path $TargetFolder -Filter *.csv | 
        ForEach-Object {
            @(Import-Csv -Path $_.FullName -Delimiter ';' ).count
        }

    Of course you should change the -Delimiter ';' to whatever delimiter you have in your CSV files.

You must be logged in to reply to this topic.