Contact: Web / Voice / Email / Tips
Simple Thoughts Blog - Java and Web Technologies

Simple solutions for complex problems.

 

Free Java Utility To Touch Files (Cross Platform)

September 23rd, 2006 by Angsuman Chakraborty

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.

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);
    }
}

You can also download the java executable class file. 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.


Filed under Headline News, How To, Java Software, Tech Note | | RSS 2.0 | Trackback this Article | Email this Article

You may also like to read

»How To Recursively Delete Files And Directories Using Java
»Shutdown Apache James Mail Server - Java Utility
»How To Simulate Telnet Session in Java
»Grid Computing: Sun Offers 100, 000 in Grid Compute Utility Cool Apps Prize
»Java Software: 1-Line Sort & Uniq Utility
»How To Create HardLinks, Junctions and Symbolic Links on Windows
»How To Change Temporary Download Directory of Download Accelerator Plus (DAP)
»JDK1.5 - More painful bugs aka Enumeration-Woes: Solved!
»Creating link files (.lnk ) files in windows
»Apple Released Java Security Update for Mac OS X
»How To Recover From Damaged Windows XP User Profile
»How To Flip English Text Using Unicode - ǝpoɔıun ƃuısn ʇxǝʇ ɥsılƃuǝ dılɟ oʇ ʍoɥ
»How To Clean Subversion Directories
»How To Exclude Files From Displaying in Subversion Status Command?
»JDK1.5 Downloaded?

3 Responses to “Free Java Utility To Touch Files (Cross Platform)”

  1. charles Says:

    There’s a problem with the recursinion in the touch method.

    This line:
    if(file.isDirectory()) for(File childFile:file.listFiles()) touch(file, time);

    should be:
    if(file.isDirectory()) for(File childFile:file.listFiles()) touch(childFile, time);

  2. Angsuman Chakraborty Says:

    Thanks. Corrected.

  3. Frieder Says:

    Thanks for this little utility! :)
    It would be nice if you could also correct the class file..

Looking forward to hear your thoughts.



Please enter the code shown below ( to verify that you are human ) before you click Submit Comment.

No. 1 method to ethically increase your blog traffic and reach.

Translate

Translate to EnglishÜbersetzen Sie zum Deutsch/GermanPřeložit do Čech/CzechOversætte hen til Dansk/DanishKääntää jotta Finnish/FinnishLefordít -hoz Magyar/HungarianÞýða til Íslenska/IcelandicTraducir a Latinoamericano Español/Latin American Spanishtagapagsalin sa Filipino/FilipinoTłumaczyć wobec Polski/PolishA traduce la spre Român/RomanianPrevesti za Srpski/Serbiantolmačiti v slovenski/SlovenianÖversätta till Svensk/SwedishChyfieitha at Cymraeg/Welshtercüme etmek -e doğru Türk/TurkishPrevesti to Hrvatski/CroatianПревеждам към Българин/BulgarianTraduzca al Español/SpanishTraduisez au Français/FrenchTraduca ad Italiano/ItalianTraduza ao Português/Portuguese日本語に翻訳しなさい /Japanese한국어에게 번역하십시오/Korean中文翻译/Chinese Simplifiedترجمة الى العربية/ArabicVertaal aan het Nederlands/DutchΜεταφράστε στα ελληνικά/GreekПереведите к русскому/RussianOversetter til Norsk/Norwegian中文翻译/Chinese TraditionalTraduzir a Língua portuguesa brasileira/Brazilian PortugueseReddo ut Latin/Latin

Taragana Network

»Ctrl-S
»Enterprise Blog
»Free Book on Eye Care by Natural Therapy
»Health Care Blog
»Hot Computer Jobs Blog
»Pet Care & Grooming News and Tips
»Phil Law Blog
»Taragana - Software Outsourcing
»The Angsuman Chakraborty Blog
»The Diabetes Cure Blog
»The Eye Treatment Blog
»The Stem Cell Blog
»Weblog Hosting Blog
"Too many of us look upon Americans as dollar chasers. This is a cruel libel, even if it is reiterated thoughtlessly by the Americans themselves." - Albert Einstein