In this article, we show you how to calculate total number of sub directories, files and size of a remote directory on a FTP server, using the Apache Commons Net API. As always, the recursion algorithm is used for this kind of recursive calculation. The following is example code of a utility method that performs such calculation:

/**
 * This method calculates total number of sub directories, files and size
 * of a remote directory. 
 * @param ftpClient An instance of the FTPClient
 * @param parentDir Path of the remote directory.
 * @param currentDir The current directory (used for recursion).
 * @return An array of long numbers in which:
 * - the 1st number is total directories.
 * - the 2nd number is total files.
 * - the 3rd number is total size.
 * @throws IOException If any I/O error occurs.
 */
public static long[] calculateDirectoryInfo(FTPClient ftpClient, 
		String parentDir, String currentDir) throws IOException {
	long[] info = new long[3];
	long totalSize = 0;
	int totalDirs = 0;
	int totalFiles = 0;

	String dirToList = parentDir;
	if (!currentDir.equals("")) {
		dirToList += "/" + currentDir;
	}

	try {
		FTPFile[] subFiles = ftpClient.listFiles(dirToList);

		if (subFiles != null && subFiles.length > 0) {
			for (FTPFile aFile : subFiles) {
				String currentFileName = aFile.getName();
				if (currentFileName.equals(".")
						|| currentFileName.equals("..")) {
					// skip parent directory and the directory itself
					continue;
				}

				if (aFile.isDirectory()) {
					totalDirs++;
					long[] subDirInfo = 
							calculateDirectoryInfo(ftpClient, dirToList, currentFileName);
					totalDirs += subDirInfo[0];
					totalFiles += subDirInfo[1];
					totalSize += subDirInfo[2];
				} else {
					totalSize += aFile.getSize(); 
					totalFiles++;
				}
			}
		}

		info[0] = totalDirs;
		info[1] = totalFiles;
		info[2] = totalSize;

		return info;
	} catch (IOException ex) {
		throw ex;
	}
}

As we can see, this static method returns an array of long numbers that is result of the calculation:

    • The first number is total number of directories.
    • The second number is total number of files.
    • The third number is total size of the whole directory (in bytes).

And the following is a code snippet that illustrates how to use the above utility method:

String host = "www.yourserver.com";
int port = 21;
String username = "your_username";
String password = "your_password";

FTPClient ftpClient = new FTPClient();
try {
	ftpClient.connect(host, port);
	ftpClient.login(username, password);
	ftpClient.enterLocalPassiveMode();

	String remoteDirPath = "/Upload";

	long[] dirInfo = calculateDirectoryInfo(ftpClient, remoteDirPath, "");

	System.out.println("Total dirs: " + dirInfo[0]);
	System.out.println("Total files: " + dirInfo[1]);
	System.out.println("Total size: " + dirInfo[2]);			

	ftpClient.logout();
	ftpClient.disconnect();
} catch (IOException ex) {
	System.err.println(ex);
}

A sample output when running the above code snippet:

Total dirs: 8
Total files: 10
Total size: 68509 

For your convenience, you can download the demo program in the attachments section below.

 

Related Java FTP Tutorials:

 

Other Java FTP Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.



Attachments:
Download this file (FTPDirectoryCalculation.java)FTPDirectoryCalculation.java[Demo program]2 kB

Add comment