2

I am trying to use org.apache.commons.io.FileSystemUtils.freeSpaceKb(String path) in Java to get the free space of a UNC path that has white spaces. The problem is that this function does not work properly when there are white spaces.

Say there is a computer in the network called John-PC. John has a shared folder that has white spaces, and we want to store something on that folder. But first, we want to make sure that he has enough space on his local drive. So we use freeSpaceKb(String path) to get the free space available. If the path was like this it would have worked:

\\\john-pc\data\abc

But if we have white spaces in the path it does not work anymore.

\\\john-pc\data\abc def

I would really appreciate it if anyone would give me a good way of doing this.

Dave L.
  • 9,595
  • 7
  • 43
  • 69
  • Also, freeSpaceKb() is in org.apache.commons.io.FileSystemUtils. And, I have tried to replace the whitespaces with "%20", or some guy said "@", but it does not work like that. – Faraz Baghernezhad Jun 27 '12 at 16:59
  • Does `//john-pc/data/abc def/` works? – nullpotent Jun 27 '12 at 17:04
  • Tested here, using: `FileSystemUtils.freeSpaceKb("\\\\john-pc\\data\\abc def\\");` , without the last folder, with the last folder, without and with the last ` \\ `, and they all worked fine here. What's the error? I'm using Windows XP and Commons-io 2.1, btw – Montolide Jun 27 '12 at 17:08
  • oh, btw, @AljoshaBre example also worked here :) – Montolide Jun 27 '12 at 17:18
  • Thanks for the answers guys. @AljoshaBre: No man, that has the same issue. – Faraz Baghernezhad Jun 27 '12 at 17:23
  • @Montolide: This is in the log file: WARNING: java.io.IOException: Command line returned OS error code '1' for command [cmd.exe, /C, dir /-c \\john-pc\data\abc def] – Faraz Baghernezhad Jun 27 '12 at 17:26
  • BTW, I'm using Commons-io 2.4 and windows 7. I really do not understand how it would work on your machine and not mine. I also tested this on another machine with windows 7, and the same issue whit white spaces. Are you sure that you are using the UNC path of the folder, and not the directory path? Thanks. – Faraz Baghernezhad Jun 27 '12 at 17:38
  • @Montolide Thank you so much for mentioning the Commons-io version. I thought we were using 2.4, but turns out we were using 1.4 in our server. Replacing 1.4 with 2.4 did the trick. Probably 1.4 has a weird bug? I'm still wondering if there is a way to use 1.4. Upgrading to 2.4 may cause some unknown issues. Any thoughts? – Faraz Baghernezhad Jun 27 '12 at 20:19

2 Answers2

0

Try to add quotes around the folder with whitespace. I haven't tried it but it might help.

\\john-pc\data\\"abc def\"

aphex
  • 3,372
  • 2
  • 28
  • 56
0

Most probably there is a bug with Commons-io-1.4.jar. Upgrading to Commons-io-2.4.jar will do the trick.

I'm still wondering if there is a way to use 1.4. Upgrading to 2.4 may cause some unknown issues. Any thoughts?

  • Maybe that's another question you should do. But I believe in my small experience that upgrading the apache commons libraries USUALLY is OK. – Montolide Jun 28 '12 at 17:42