M
Marco Schmidt
No, this is not the usual question on how to get free diskspace. The
RFE
<http://developer.java.sun.com/developer/bugParade/bugs/4057701.html>
on that topic is already in its seventh year.
This is a pure Java program that parses the output of the 'dir'
command to determine that value. I could use some help with it. The
program works only under Windows, although the same approach could
most likely be used with other operating systems.
I'd like as many people as possible to run this program under their
flavor of Windows and report problems. Java 1.4+ is required.
Two things to point your attention to:
* What other "os.name" strings apart from "Windows NT" and "Windows
2000" exist for the NT flavor of Windows? I guess "Windows XP" and
"Windows 2003", but I'm not sure.
* What other decimal separators are there apart from the comma and the
dot? I'm talking about the characters that are put between groups of
digits in the dir number output. Example: the comma in 1,234 (USA) or
the dot in 1.234 (Germany). It's probably helpful that this is an
international newsgroup because many language versions of Windows are
installed on readers' systems. If that character is not removed, the
output cannot be parsed. The character is probably the one returned by
java/text/DecimalFormatSymbols.html#getGroupingSeparator().
Here's the program:
=== filesystem.Diskspace
package filesystem;
import java.io.BufferedReader;
import java.io.InputStreamReader;
/**
* Determine free disk space for a given directory by
* parsing the output of the dir command.
* This class is inspired by the code at
*
http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20088852.html
* Works only under Windows under certain circumstances.
* Yes, it's that shaky.
* Requires Java 1.4 or higher.
* @author Marco Schmidt
*/
public class Diskspace
{
private Diskspace()
{
// prevent instantiation of this class
}
/**
* Return available free disk space for a directory.
* @param dirName name of the directory
* @return free disk space in bytes or -1 if unknown
*/
public static long getFreeDiskSpace(String dirName)
{
try
{
// guess correct 'dir' command by looking at the
// operating system name
String os = System.getProperty("os.name");
String command;
if (os.equals("Windows NT") ||
os.equals("Windows 2000"))
{
command = "cmd.exe /c dir " + dirName;
}
else
{
command = "command.com /c dir " + dirName;
}
// run the dir command on the argument directory name
Runtime runtime = Runtime.getRuntime();
Process process = null;
process = runtime.exec(command);
if (process == null)
{
return -1;
}
// read the output of the dir command
// only the last line is of interest
BufferedReader in = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String line;
String freeSpace = null;
while ((line = in.readLine()) != null)
{
freeSpace = line;
}
if (freeSpace == null)
{
return -1;
}
process.destroy();
// remove dots & commas & leading and trailing whitespace
freeSpace = freeSpace.trim();
freeSpace = freeSpace.replaceAll("\\.", "");
freeSpace = freeSpace.replaceAll(",", "");
String[] items = freeSpace.split(" ");
// the first valid numeric value in items after(!) index 0
// is probably the free disk space
int index = 1;
while (index < items.length)
{
try
{
long bytes = Long.parseLong(items[index++]);
return bytes;
}
catch (NumberFormatException nfe)
{
}
}
return -1;
}
catch (Exception exception)
{
return -1;
}
}
/**
* Command line program to print the free diskspace to stdout
* for all 26 potential root directories A:\ to Z:\
* (when no parameters are given to this program)
* or for those directories (drives) specified as parameters.
* @param args program parameters
*/
public static void main(String[] args)
{
if (args.length == 0)
{
for (char c = 'A'; c <= 'Z'; c++)
{
String dirName = c + ":\\";
System.out.println(dirName + " " +
getFreeDiskSpace(dirName));
}
}
else
{
for (int i = 0; i < args.length; i++)
{
System.out.println(args + " " +
getFreeDiskSpace(args));
}
}
}
}
===
Regards,
Marco
RFE
<http://developer.java.sun.com/developer/bugParade/bugs/4057701.html>
on that topic is already in its seventh year.
This is a pure Java program that parses the output of the 'dir'
command to determine that value. I could use some help with it. The
program works only under Windows, although the same approach could
most likely be used with other operating systems.
I'd like as many people as possible to run this program under their
flavor of Windows and report problems. Java 1.4+ is required.
Two things to point your attention to:
* What other "os.name" strings apart from "Windows NT" and "Windows
2000" exist for the NT flavor of Windows? I guess "Windows XP" and
"Windows 2003", but I'm not sure.
* What other decimal separators are there apart from the comma and the
dot? I'm talking about the characters that are put between groups of
digits in the dir number output. Example: the comma in 1,234 (USA) or
the dot in 1.234 (Germany). It's probably helpful that this is an
international newsgroup because many language versions of Windows are
installed on readers' systems. If that character is not removed, the
output cannot be parsed. The character is probably the one returned by
java/text/DecimalFormatSymbols.html#getGroupingSeparator().
Here's the program:
=== filesystem.Diskspace
package filesystem;
import java.io.BufferedReader;
import java.io.InputStreamReader;
/**
* Determine free disk space for a given directory by
* parsing the output of the dir command.
* This class is inspired by the code at
*
http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20088852.html
* Works only under Windows under certain circumstances.
* Yes, it's that shaky.
* Requires Java 1.4 or higher.
* @author Marco Schmidt
*/
public class Diskspace
{
private Diskspace()
{
// prevent instantiation of this class
}
/**
* Return available free disk space for a directory.
* @param dirName name of the directory
* @return free disk space in bytes or -1 if unknown
*/
public static long getFreeDiskSpace(String dirName)
{
try
{
// guess correct 'dir' command by looking at the
// operating system name
String os = System.getProperty("os.name");
String command;
if (os.equals("Windows NT") ||
os.equals("Windows 2000"))
{
command = "cmd.exe /c dir " + dirName;
}
else
{
command = "command.com /c dir " + dirName;
}
// run the dir command on the argument directory name
Runtime runtime = Runtime.getRuntime();
Process process = null;
process = runtime.exec(command);
if (process == null)
{
return -1;
}
// read the output of the dir command
// only the last line is of interest
BufferedReader in = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String line;
String freeSpace = null;
while ((line = in.readLine()) != null)
{
freeSpace = line;
}
if (freeSpace == null)
{
return -1;
}
process.destroy();
// remove dots & commas & leading and trailing whitespace
freeSpace = freeSpace.trim();
freeSpace = freeSpace.replaceAll("\\.", "");
freeSpace = freeSpace.replaceAll(",", "");
String[] items = freeSpace.split(" ");
// the first valid numeric value in items after(!) index 0
// is probably the free disk space
int index = 1;
while (index < items.length)
{
try
{
long bytes = Long.parseLong(items[index++]);
return bytes;
}
catch (NumberFormatException nfe)
{
}
}
return -1;
}
catch (Exception exception)
{
return -1;
}
}
/**
* Command line program to print the free diskspace to stdout
* for all 26 potential root directories A:\ to Z:\
* (when no parameters are given to this program)
* or for those directories (drives) specified as parameters.
* @param args program parameters
*/
public static void main(String[] args)
{
if (args.length == 0)
{
for (char c = 'A'; c <= 'Z'; c++)
{
String dirName = c + ":\\";
System.out.println(dirName + " " +
getFreeDiskSpace(dirName));
}
}
else
{
for (int i = 0; i < args.length; i++)
{
System.out.println(args + " " +
getFreeDiskSpace(args));
}
}
}
}
===
Regards,
Marco