Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old June 18th, 2004, 09:04 AM
Registered User
Join Date: Jun 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Iterate directories using FileSystemObject

I need to update 3500 java files with a copyright statement on my local drive (I'll check them back into changeman later). I'll need to go into each directory (about 100) that may or may not have subdirectories and modify each file. I'm getting caught up in going down the branches.

Has anyone written an routine to walk the directory structure using FileSystemObject?

Old June 18th, 2004, 02:34 PM
Registered User
Join Date: Jun 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts

I found a way to do it, I've pasted it here in case anyone else needs it.


<META name="GENERATOR" content="Microsoft FrontPage 4.0">
<META name="ProgId" content="FrontPage.Editor.Document">
<TITLE>Properties Parser</TITLE>

Insert Copyright HTML Application
Written by Keith Lofy
    APPLICATIONNAME="Insert Copyright"

    var ForReading = 1;
    var ForWriting = 2;
    var ForAppending = 8;
    var TriStateFalse = 0;
    var OriginalLine = "";
    var SplitLine = "";
    var ModifiedLine = "";
    var sFilePathName = "";
    var sModifiedFilePathName = "";
    var outputtext = "";
    var FSO = new ActiveXObject("Scripting.FileSystemObject");
    var f = FSO.GetFolder("C:/Documents and Settings/My Documents/CopyrightReplace");
    var count = 0;
    var copyright1 = "/*";
    var copyright2 = "* ";
    var copyright3 = "* June 18, 2004";
    var copyright4 = "* Copyright 2004, SBC Communication Inc. All rights reserved.";
    var copyright5 = "* This copyrighted material is the Confidential, Unpublished Property ";
    var copyright6 = "* of SBC Communications Inc. ";
    var copyright7 = "*/";

function trim(str) {
    str.replace(/^\s*/, '').replace(/\s*$/, '');
   return str;


function myRecursion(){
    alert("File Count is : " + count + "\n" + "output text is: " + outputtext);
    // Destroy and de-reference FileSystemObject
    delete objTextSteam
    objTextStream = null;
    delete FSO;
    FSO = null;
    alert("Converstion Completed.");
    } catch(e){
     alert("An error occured in: myRecursion(), the error was: " + e.description)


function IterateThroughDirectory(objFolder)
    // Get enumerator for files in directory
    var fe = new Enumerator(objFolder.Files);
    // Check to see if file is a .java file
    for (;!fe.atEnd();fe.moveNext()){
        if(fe.item().type == "JAVA File"){
            outputtext = outputtext + "File Name: " + fe.item().path + "." + fe.item().name + " \n";
    // now that each file in the folder has been processed repeat sequence for all subdirectories
    var fsubf = new Enumerator(objFolder.SubFolders);
    for (i=0; !fsubf.atEnd(); fsubf.moveNext()){
    } catch(e){
     alert("An error occured in: IterateThroughDirectory(), the error was: " + e.description)


function AddJavaCopyright(fe){
    var originalFileName = fe.name;
    var originalFilePath = fe.path;
    var modifiedFileName = originalFileName.replace(new RegExp("\.java"), "new.java");
    var modifiedFilePath = originalFilePath.replace(new RegExp("\.java"), "new.java");
    var lineNum = 0;
    try {

    // Open file and search for "Copyright" or "Copyright"
    var InTextStream = FSO.OpenTextFile (originalFilePath, ForReading, true, TriStateFalse );
    // Open output file
    var OutTextStream = FSO.CreateTextFile(modifiedFilePath,true,false);
    lineNum = 9;
    // Read input file and insert copyright information into files at first line.
    OutTextStream.WriteLine(copyright2 + fe.name);
    lineNum = 21;

    var reg = new RegExp("Copyright");
    var foundCR = false;
    //If Copyright is found close both streams and delete outstream.
    while(!(InTextStream.AtEndOfStream) && (foundCR==false)){
    lineNum = 32;

    if (foundCR) {
        //Copyright was found so close file input file, delete output file and move to next file
        FSO.DeleteFile(modifiedFilePath, true);
        //Write name of file to array of file names not modified.
        //Delete original file since we will save modified file over the top of it.
        FSO.CopyFile(modifiedFilePath, originalFilePath);
        FSO.DeleteFile(modifiedFilePath, true);

        //Write name of file to array of file name modified.
    } catch(e){
     alert("An error occured in: AddJavaCopyright(), the error was: " + e.description + "\n The line number was: " + lineNum + "\n Original path: " + originalFilePath + "\n Original path: " + modifiedFilePath);





<FORM name="form1">
  <INPUT type="button" onclick="myRecursion()" value="Iterate through dir"><BR>



Similar Threads
Thread Thread Starter Forum Replies Last Post
Iterate count within for-each-group stolte XSLT 1 November 19th, 2007 07:40 PM
To iterate the FOR loop variable garg.ashish15 Oracle 0 November 10th, 2006 09:31 AM
Unable to Iterate over large number of records Neuron Java Databases 0 June 28th, 2006 11:14 AM
Is anybody tell me how to iterate <asp:Checkbox> a frmsasp ASP.NET 2.0 Professional 2 October 12th, 2005 10:05 AM
Is anybody tell me how to iterate <asp:Checkbox> a frmsasp All Other Wrox Books 0 October 7th, 2005 11:56 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.