Author Posts

April 25, 2014 at 5:53 am

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.

April 25, 2014 at 5:55 am

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.

April 25, 2014 at 5:55 am

Hi.

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

April 25, 2014 at 5:56 am

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

April 25, 2014 at 6:01 am

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 😛

April 25, 2014 at 6:03 am

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).

April 25, 2014 at 6:04 am

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.

April 25, 2014 at 6:08 am

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

April 25, 2014 at 6:09 am

@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 🙂

April 25, 2014 at 6:10 am

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/

April 25, 2014 at 6:14 am

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