Forum Discussion

Mahesh_B's avatar
Mahesh_B
Copper Contributor
Jul 01, 2021

Script to return true only if satisfies condition

Hi All,

I have a requirement where I need to create script.
That script would return true only if size of specific file is greater than 1 KB else it should return false.
Any help regarding this would be appreciated.

Thanks,
Mahesh

  • yuzoyox's avatar
    yuzoyox
    Iron Contributor

    Mahesh_B 

     

    1kb = 1000bytes

     

    an empty text file have 2 bytes.

     

    $filepath = Get-Item 'C:\Users\YUZOYOX\Documents\bat\comparq\teste\oia.txt'
    if ($filepath.Length -gt '1000')
    {
    Write-Output "true"
    } else {
    Write-Output "false"
    }

     

    dont forget to mark the post as a solution, thank you

    • psophos's avatar
      psophos
      Brass Contributor

      yuzoyox   you can do it this way too:

      $filepath = Get-Item 'D:\Data\emp.csv'
      if ($filepath.Length -gt 1KB)
      {
          Write-Output $true
      } else {
          Write-Output $false
      }

      It's just a slight mod to your code.

      You can compare directly to 1kb, and

      it's usually better to return $true or $false.  Unless the script is expecting the strings.

    • Mahesh_B's avatar
      Mahesh_B
      Copper Contributor

      Hi yuzoyox ,

       

      Script seems to be not working. Created script and checked for file with Size 53.3 KB. Still it returned TRUE.

       

      $filepath = Get-Item 'D:\Data\emp.csv'
      if ($filepath.Length -gt '1000')
      {
      Write-Output "true"
      } else {
      Write-Output "false"
      }

       

      Thanks,

      Mahesh

      • yuzoyox's avatar
        yuzoyox
        Iron Contributor
        Hi Mahesh,
        as per your request, it need to return true if it is greather than 1000.
        if convert your 53.3KB it will be in 53300, 53300 > 1000 = true.

Resources