locked
Looking to confirm the size of a file but .Length is returning the incorrect True/False Value RRS feed

  • Question

  • Hi Everyone,

    I'm another pretty new scripter with powershell and I am having a weird issue that I'm sure is a simple quick fix but I am not able to find the solution.

    II am comparing two CSV (File1.csv & File2.CSV) files then exporting the results to a separate CSV file (Changes.CSV)  I am attempting to run an IF statement based upon if the length/size of the file is not less than/equal to 0 kb. (Pretty much either the result will either spit out data or the file is blank).  When using .Length the values come out incorrect.

    #Declare ChangesFile Variable
    [string]$ChangesFile = "C:\temp\changes.csv"
    
    #Compare Files
    Compare-Object $(Get-Content C:\temp\File1.csv) $(Get-Content C:\temp\File2.csv) | Export-Csv  $ChangesFile -Delimiter "|" -NoTypeInformation -NoClobber
    
    #Confirm what the system thinks the size to be
    Get-Childitem -file $ChangesFile | select length
    
    #Check if file is less than/equal to 0
    $ChangesFile.Length -le 0

    Currently the two files being compared are exact copies.  Changes.csv is 0 kb yet this check returns False.

    Any advice?


    • Edited by Aaron Phipps Thursday, August 3, 2017 8:38 PM Editing variable
    Thursday, August 3, 2017 7:31 PM

Answers

  • You cannot get the file length fro the contents.

    if((Get-Item C:\temp\File1.csv).Length -ne (Get-Item C:\temp\File2.csv).Length){
    		Write-Host 'Files do not match'
    }


    \_(ツ)_/

    • Marked as answer by Aaron Phipps Thursday, August 3, 2017 10:13 PM
    Thursday, August 3, 2017 9:45 PM
  • You are totally correct.  I changed up the method and got it to work:

    ...If ((Get-Content $ChangesFile) -eq $null){...}

    This did the trick!

    Thanks again for your help

    • Marked as answer by Aaron Phipps Thursday, August 3, 2017 10:13 PM
    Thursday, August 3, 2017 10:13 PM

All replies

  • The variable is null. $ADChangesCheckFile

    You are never setting this to anything.


    \_(ツ)_/

    Thursday, August 3, 2017 7:52 PM
  • Sorry that was a transfer error while scrubbing the script. I've updated it now.
    Thursday, August 3, 2017 8:39 PM
  • In your code it will NEVER be less than 0.  It is a string you have assigned.


    \_(ツ)_/

    Thursday, August 3, 2017 8:42 PM
  • The length shows 0  (using the Get-childitem...) now that the file is empty but even if I change it to -eq I am not able to get it to return true 
    $ChangesFile.Length -eq 0
    Thursday, August 3, 2017 9:00 PM
  • You are comparing file contents and then testing the file name.  Why?  Forget about exporting and look at the output form the compare.  It has no length property.


    \_(ツ)_/

    Thursday, August 3, 2017 9:14 PM
  • I'd like to do just that, compare the two files and if there is nothing to export the script ends there and if there is data from the compare export that to a CSV file.  I'm not sure how to script that and what I have was the best I could do to achieve that goal.
    • Proposed as answer by jrv Thursday, August 3, 2017 9:25 PM
    • Unproposed as answer by jrv Thursday, August 3, 2017 9:25 PM
    Thursday, August 3, 2017 9:18 PM
  • You will need o use a file diff program to do that.

    See this:

     cmd /c comp /?


    \_(ツ)_/

    Thursday, August 3, 2017 9:27 PM
  • Thanks for the help.  I was hoping to keep it all in the script because this is the middle of a bigger script. I need to figure out to fix the .length section to spit out the correct value
    Thursday, August 3, 2017 9:40 PM
  • You cannot get the file length fro the contents.

    if((Get-Item C:\temp\File1.csv).Length -ne (Get-Item C:\temp\File2.csv).Length){
    		Write-Host 'Files do not match'
    }


    \_(ツ)_/

    • Marked as answer by Aaron Phipps Thursday, August 3, 2017 10:13 PM
    Thursday, August 3, 2017 9:45 PM
  • You are totally correct.  I changed up the method and got it to work:

    ...If ((Get-Content $ChangesFile) -eq $null){...}

    This did the trick!

    Thanks again for your help

    • Marked as answer by Aaron Phipps Thursday, August 3, 2017 10:13 PM
    Thursday, August 3, 2017 10:13 PM