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