locked
File Size Check RRS feed

  • Question

  • Hi,

    I have a piece of script which is used to check the file size. If the size of the file LMCReport.csv is 0 kb, I am replacing it with another file LMCReportDummy.csv. However if the LMCReport.csv file is not 0 KB, I need the original file created from, the Import-csv - Export-csv command.

    For some reason, even if the file size is not 0 KB, it seems powershell is still replacing it with another file LMCReportDummy.csv. I am not sure what changes I need to do for my code to work.

    Import-Csv E:\ScheduledBOReports\TempLog.csv | sort Text -Unique | Export-Csv E:\ScheduledBOReports\LMCReport.csv -NoTypeInformation -Force
    $File = 'E:\ScheduledBOReports\LMCReport.csv'
    if (Test-Path $File) { (Get-Item $File).length -eq 0kb }
    Copy-Item E:\ScheduledBOReports\LMCReportDummy.csv E:\ScheduledBOReports\LMCReport.csv

    Tuesday, March 18, 2014 1:19 PM

Answers

  • Your if statement is structured a bit oddly. Right now, Copy-Item is outside the If block (so it will always execute), and the if block contains what should really be part of the conditional itself. Try this:

    $File = 'E:\ScheduledBOReports\LMCReport.csv'
    
    Import-Csv E:\ScheduledBOReports\TempLog.csv |
    sort Text -Unique |
    Export-Csv $File -NoTypeInformation -Force
    
    if ((Test-Path $File) -and  (Get-Item $File).length -eq 0kb)
    {
        Copy-Item E:\ScheduledBOReports\LMCReportDummy.csv E:\ScheduledBOReports\LMCReport.csv
    }
    

    Tuesday, March 18, 2014 1:24 PM

All replies

  • Your if statement is structured a bit oddly. Right now, Copy-Item is outside the If block (so it will always execute), and the if block contains what should really be part of the conditional itself. Try this:

    $File = 'E:\ScheduledBOReports\LMCReport.csv'
    
    Import-Csv E:\ScheduledBOReports\TempLog.csv |
    sort Text -Unique |
    Export-Csv $File -NoTypeInformation -Force
    
    if ((Test-Path $File) -and  (Get-Item $File).length -eq 0kb)
    {
        Copy-Item E:\ScheduledBOReports\LMCReportDummy.csv E:\ScheduledBOReports\LMCReport.csv
    }
    

    Tuesday, March 18, 2014 1:24 PM
  • Hi David,

    Thanks a million. That worked great. I appreciate your help and time.

    Regards

    Tuesday, March 18, 2014 1:44 PM