Thursday, February 19, 2009

Free Java Utility To Touch Files (Cross Platform)

This is a simple commandline Java utility which I wrote down in under 5 minutes to help in checking-in (svn commit) over 500 files which were modified but the dates weren't changed due to an error in our settings. So subversion failed to recognize it. Anyway this simple utility updates the timestamp of any file(s) and directories recursively to current time. It is extremely fast and cross-platform. It does one job and does it well. It is named after the unix utility touch, with similar functionality.


You can run it as follows:
java -classpath . Touch *.html

Replace *.html with file name(s) and directories you want to update. This requires JDK 1.5 or later.







import java.io.File;

/** Super-fast file / directory(recursive) touch.
* It doesn't ask for confirmation.
* Arguments: File / Directories to touch to current time.
*/
public class Touch {
public static void main(String ... args) {
long time = System.currentTimeMillis();
for(String fileName:args) touch(new File(fileName), time);
}

/** Recursively touch file and directories.
* @param File (file or directory) for touching.
*/
public static void touch(File file, long time) {
if(file.isDirectory()) for(File childFile:file.listFiles()) touch(childFile, time);
file.setLastModified(time);
}
}
/** Super-fast file / directory(recursive) touch.
* It doesn't ask for confirmation.
* Arguments: File / Directories to touch to current time.
*/
public class Touch {
public static void main(String ... args) {
long time = System.currentTimeMillis();
for(String fileName:args) touch(new File(fileName), time);
}

/** Recursively touch file and directories.
* @param File (file or directory) for touching.
*/
public static void touch(File file, long time) {
if(file.isDirectory()) for(File childFile:file.listFiles()) touch(childFile, time);
file.setLastModified(time);
}
}
/** Super-fast file / directory(recursive) touch.
* It doesn't ask for confirmation.
* Arguments: File / Directories to touch to current time.
*/
public class Touch {
public static void main(String ... args) {
long time = System.currentTimeMillis();
for(String fileName:args) touch(new File(fileName), time);
}

/** Recursively touch file and directories.
* @param File (file or directory) for touching.
*/
public static void touch(File file, long time) {
if(file.isDirectory()) for(File childFile:file.listFiles()) touch(childFile, time);
file.setLastModified(time);
}
}
/** Super-fast file / directory(recursive) touch.
* It doesn't ask for confirmation.
* Arguments: File / Directories to touch to current time.
*/
public class Touch {
public static void main(String ... args) {
long time = System.currentTimeMillis();
for(String fileName:args) touch(new File(fileName), time);
}

/** Recursively touch file and directories.
* @param File (file or directory) for touching.
*/
public static void touch(File file, long time) {
if(file.isDirectory()) for(File childFile:file.listFiles()) touch(childFile, time);
file.setLastModified(time);
}
}

No comments: