Exemplo n.º 1
0
  /**
   * Find free space on the Windows platform using the 'dir' command.
   *
   * @param path the path to get free space for, including the colon
   * @param timeout The timeout amount in milliseconds or no timeout if the value is zero or less
   * @return the amount of free drive space on the drive
   * @throws IOException if an error occurs
   */
  long freeSpaceWindows(String path, final long timeout) throws IOException {
    path = FilenameUtils.normalize(path, false);
    if (path.length() > 0 && path.charAt(0) != '"') {
      path = "\"" + path + "\"";
    }

    // build and run the 'dir' command
    final String[] cmdAttribs = new String[] {"cmd.exe", "/C", "dir /a /-c " + path};

    // read in the output of the command to an ArrayList
    final List<String> lines = performCommand(cmdAttribs, Integer.MAX_VALUE, timeout);

    // now iterate over the lines we just read and find the LAST
    // non-empty line (the free space bytes should be in the last element
    // of the ArrayList anyway, but this will ensure it works even if it's
    // not, still assuming it is on the last non-blank line)
    for (int i = lines.size() - 1; i >= 0; i--) {
      final String line = lines.get(i);
      if (line.length() > 0) {
        return parseDir(line, path);
      }
    }
    // all lines are blank
    throw new IOException(
        "Command line 'dir /-c' did not return any info " + "for path '" + path + "'");
  }