0

What is the correct way of determining if a file or folder is empty in powershell? I came across this post and tried the example that the second user gave, and it worked.

Now, I've been checking if a directory is empty by using the length property, like this:

(Get-ChildItem -Path "C:\Users\someUser\Downloads\" -Recurse).Length)

However, that only returns the number of bytes that the file/folder has. I tested the length property and found that if you were to have a blank text file/folder placed in the downloads then the length property wouldn't return the correct value.

Can you use the length property to correctly determine the number of files/folders that a directory has?

Community
  • 1
  • 1
  • Why don't you use `Test-Path "C:\Users\someUser\Downloads\*"` as suggested in [another](http://stackoverflow.com/a/10550670/608772) answer of the same [post](http://stackoverflow.com/q/10550128/608772). – JPBlanc Feb 10 '15 at 04:43
  • 1
    Possible duplicate of [Powershell test if folder empty](http://stackoverflow.com/questions/10550128/powershell-test-if-folder-empty) – jpmc26 Mar 29 '17 at 21:45

3 Answers3

3

Try this, it will count the number of items:

Get-ChildItem -Path "C:\Users\someUser\Downloads\" -Recurse | Measure-Object
campbell.rw
  • 1,366
  • 12
  • 22
  • Thanks, it worked, however, why does the length property give the value that it does? Why doesn't it give the number of files/folders in a directory? –  Feb 07 '15 at 20:47
  • 1
    Because when you do a get-childitem call, then length property respresents the file size. My example doesn't format well in this comment, but just do a get-childitem call by itself and you will see the length column. For example: PS>get-childitem Directory: C:\Users\Foo\Desktop Mode LastWriteTime Length Name ---- ------------- ------ ---- d---- 2013-04-20 9:51 PM 2013-01-22-antlrworks-2.0 – campbell.rw Feb 07 '15 at 20:49
  • Thanks again, I just tested your answer, but I had to modify it like this: (Get-ChildItem -Path "C:\Users\somerUser\Downloads\" -Recurse | Measure-Object) I had to place brackets around the command for it to return the proper value. Why? –  Feb 07 '15 at 20:53
  • You dont need any brackets at all, I didn't use them when I ran it. – campbell.rw Feb 07 '15 at 21:52
2

if you want to be sure a file or folder is empty you can use the following:

for a file:

If ((Get-Content C:\YouFile.txt) -eq $Null) {
    "The file is empty"
}

if the file contains white space it wont return as null, so if you want to check even white space you can use the following:

If ([string]::IsNullOrWhiteSpace((Get-Content C:\YourFile.txt))) {
    "The file is empty"
}

for a folder:

If ((Get-ChildItem -Force C:\SomeFolder) -eq $Null) {
    "the Folder is empty"
}
Sagiv b.g
  • 30,379
  • 9
  • 68
  • 99
0

The other way to handle this is just to use the base static method in System.Io.Directory -- Directory.EnumerationFileSystemInfos() and see how many objects, if any, are returned. Note that a FileSystemInfo can be either a directory or file, so it'll work in your case.

[system.io.directory]::EnumerateFileSystemEntries("C:\temp")
Bill Dinger
  • 179
  • 1
  • 6