Check if text file is empty

This topic contains 10 replies, has 4 voices, and was last updated by Profile photo of Perry Jones Perry Jones 2 years, 7 months ago.

  • Author
    Posts
  • #14822
    Profile photo of Perry Jones
    Perry Jones
    Participant

    So I've got this small bit of code that should execute and check if a text file is empty. it is it should remove it and then rename another text file to replace it. It looks like this:

    if((Get-Content "c:\Scripts\CurrentPass.txt") -eq $Null){
    Remove-Item c:\Scripts\CurrentPass.txt
    Rename-Item c:\Scripts\FuturePass.txt CurrentPass.txt

    But it's not working. what did I do wrong.

  • #14824
    Profile photo of Don Jones
    Don Jones
    Keymaster

    What part isn't working?

    And, is your path name correct? It looks misspelled.

    You can also consider getting the file using Get-ChildItem, and checking its size.

  • #14825
    Profile photo of Stein Petersen
    Stein Petersen
    Participant

    Hi.

    You have a typo in the paths, C:\Scirpts and c:\Scripts

  • #14826
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    Aside from the typo "Scirpts", it looks like it should work.

  • #14827
    Profile photo of Perry Jones
    Perry Jones
    Participant

    Yeah. the typo was my mistake. I can't copy and paste it here because it's on a separate machine that doesn't have internet access. Basically, from what I'm seeing, the file is empty, but the code is not executing. But let me be more clear the file is "technically" empty. It has no characters and has two lines that are both empty. No space no tab no nothing. just two lines with a length of zero.

    I guess I should explain the environment this code sits in. 2 scripts: one to generate random passwords and write them to a text file. Another to read the text file and remove the passwords from the file after they are used. once it runs out(i.e. the text file is empty) it should remove it and another file with more passwords is already available. All the code needs to do is rename it to "CurrentPass.txt" so the code can use it.

    but for some reason it's not. Luckily the typo doesn't exist in the code I have 😛

  • #14828
    Profile photo of Don Jones
    Don Jones
    Keymaster

    If it has any lines, that's not legally empty. A carriage return or other whitespace "counts." Get-Content returns each line as an object, and so attempting to compare that array of two "empty" lines to $null will fail. They're not null.

    You're going to have to change your logic. For example, start by running Get-Content on the file and piping it to Measure-Object. You'll see that it isn't null – it contains objects. So you're going to have to put some logic in to look at each line and see if they're all empty (which isn't the same as $null).

  • #14829
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    Ah. Blank lines, whitespace lines, and $null are all different things. Try this, if you're looking for non-whitespace:

    if (-not ((Get-Content c:\Scripts\CurrentPass.txt -Raw) -match '\S'))
    {
        Remove-Item c:\Scripts\CurrentPass.txt
        Rename-Item c:\Scripts\FuturePass.txt CurrentPass.txt
    }
    

    The -Raw switch is a bit of a performance increase, but requires PowerShell 3.0. If you're running this on 2.0, just take out the -Raw switch and it will still work.

  • #14832
    Profile photo of Perry Jones
    Perry Jones
    Participant

    @Dave Wyatt Saw your post after I fixed it. Will try what you have as well. Checking for "" may not be consistent for what I need this for so your method may work better. Thank you to all who responded to my post 🙂

  • #14833
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    Careful there. That code will evaulate to true if there are more than one blank line in any file, even if the file also contains non-blank lines. I made a blog post on this topic a while back: https://powershell.org/2013/10/28/comparison-operators-collections-and-conditionals-oh-my/

  • #14834
    Profile photo of Perry Jones
    Perry Jones
    Participant

    Thank you Dave. I will read your blog post. And thank you to the great Don Jones as well!

  • #14831
    Profile photo of Perry Jones
    Perry Jones
    Participant

    Slight modification and it now works! Huzzah!

    if((Get-Content "c:\Scripts\CurrentPass.txt") -eq ""){
    Remove-Item c:\Scripts\CurrentPass.txt
    Rename-Item c:\Scripts\FuturePass.txt CurrentPass.txt

You must be logged in to reply to this topic.