Author Posts

January 31, 2018 at 6:54 am

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"
}

January 31, 2018 at 9:36 am

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.

January 31, 2018 at 10:05 am

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.