1

I'm trying to use File.getTotalSpace() and File.getFreeSpace() on a NAS share via UNC, from a Windows host.

Due to the various links in the NAS, the free/total space will be different based on the specific directory requested.

For example, in a DOS box:

dir \\nas\level1

might return 12,234,567 bytes free, but:

dir \\nas\level1\level2\level3

returns 987,654,321 bytes free.

I try:

new File("\\\\nas\\level1\\level2\\level3").getFreeSpace();

but this returns 12,234,567. It appears that getFreeSpace() and getTotalSpace() are retrieving the reported space from the root of the path (\\nas, in this case), rather than from the level I requested.

If I map that UNC path to a drive letter, e.g.:

net use s: \\nas\level1\level2\level3

then

new File("s:").getFreeSpace();

will return the correct value. But I have to iterate through a bunch of UNC paths, so mapping them all is not feasible.

So how can I get the free/total space of a UNC-based share from the specific directory level I'm requesting?

Joe
  • 6,767
  • 1
  • 16
  • 29
  • Suggestion: try java.nio [FileStore](https://docs.oracle.com/javase/7/docs/api/java/nio/file/class-use/FileStore.html). For example, `Files.getFileStore(Paths.get("path to file").toRealPath()).getUsableSpace();`. – paulsm4 Jan 27 '16 at 20:14
  • Thanks for the suggestion, but it produced the same result. – Joe Jan 27 '16 at 20:39

1 Answers1

0

I assume you get the expected space used/space free values from Windows explorer.

If so, you should also be able to get these same values programmatically from WMI.

SUGGESTIONS:

  1. Use a Java::WMI library or wrapper like jWMI.

  2. Write a simple VBScript or PowerShell script, then call your script from Java.

Here's a simple PowerShell (.ps1) example, from the above link:

$disk = Get-WmiObject Win32_LogicalDisk -ComputerName remotecomputer -Filter "DeviceID='C:'" |
Foreach-Object {$_.Size,$_.FreeSpace}

Here's another example:

https://superuser.com/questions/911534/determine-the-size-of-a-network-folder

$startFolder = "\\pmintl.net\rbsdata\SPA_BB01"

$colItems = (Get-ChildItem $startFolder | Measure-Object -property length -sum)
"$startFolder -- " + "{0:N2}" -f ($colItems.sum / 1MB) + " MB"

$colItems = (Get-ChildItem $startFolder -recurse | Where-Object {$_.PSIsContainer -eq $True} | Sort-Object)
foreach ($i in $colItems)
    {
        $subFolderItems = (Get-ChildItem $i.FullName | Measure-Object -property length -sum)
        $i.FullName + " -- " + "{0:N2}" -f ($subFolderItems.sum / 1MB) + " MB"
    }
Community
  • 1
  • 1
paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • Thanks, I'll check out those options. – Joe Jan 27 '16 at 21:10
  • The first script above seems to only work with physical or logical devices (drive letters) on a remote machine. The second looks like it would work to retrieve used space, but not space available or total space. Am I missing something? – Joe Jan 28 '16 at 13:28