Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 18th, 2004, 09:04 AM
Registered User
 
Join Date: Jun 2004
Location: , , .
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?



Reply With Quote
  #2 (permalink)  
Old June 18th, 2004, 02:34 PM
Registered User
 
Join Date: Jun 2004
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

<HTML>

<HEAD>
<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
-->
<HTA:APPLICATION ID="oMyApp"
    APPLICATIONNAME="Insert Copyright"
    BORDER="thick"
    CAPTION="yes"
    ICON="w_icon.gif"
    SHOWINTASKBAR="yes"
    SINGLEINSTANCE="yes"
    SYSMENU="yes"
    WINDOWSTATE="normal"
    MAXIMIZEBUTTON="no"
    MINIMIZEBUTTON="yes"
  >

<SCRIPT LANGUAGE="JavaScript">
    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;
}

//Recursion

function myRecursion(){
    try{
    IterateThroughDirectory(f);
    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)
{
    try{
    // 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";
            count++;
            AddJavaCopyright(fe.item());
        }
    }
    // 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()){
        IterateThroughDirectory(fsubf.item())
    }
    } 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 );
    lineNum=6;
    // 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(copyright1);
    OutTextStream.WriteLine(copyright2 + fe.name);
    OutTextStream.WriteLine(copyright2);
    OutTextStream.WriteLine(copyright3);
    OutTextStream.WriteLine(copyright2);
    OutTextStream.WriteLine(copyright4);
    OutTextStream.WriteLine(copyright5);
    OutTextStream.WriteLine(copyright6);
    OutTextStream.WriteLine(copyright7);
    OutTextStream.WriteBlankLines(1);
    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)){
        OriginalLine=InTextStream.ReadLine();
        OriginalLine=trim(OriginalLine);
        foundCR=reg.test(OriginalLine);
        OutTextStream.WriteLine(OriginalLine);
    }
    lineNum = 32;
    InTextStream.Close();
    OutTextStream.Close();

    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.
    }else{
        //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);
    }


}

</script>

</HEAD>

<BODY>

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

</BODY>

</HTML>


Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 04:57 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.