What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Messenger Plus! for Live Messenger » Scripting » [Help]Reading a file/Opening a file

[Help]Reading a file/Opening a file
Author: Message:
Matti
Elite Member
*****

Avatar
Script Developer and Helper

Posts: 1646
Reputation: 39
31 / Male / Flag
Joined: Apr 2004
RE: [Help]Reading a file/Opening a file
Nicely done without testing, but after some testing a few problems showed up. You're trying to use a non-existent Path variable for GetFolder, and you're not returning your results array. Also, your line loop is wrong: Line is a property iterator (ranging [0...Text.length-1] for this array), but you're not getting the values. As a best practice, you should close your text stream before returning the single line result.

Note that this function will only work with Unicode files, because of the -1 argument passed to OpenAsTextStream. If you're going to have mixed ASCII and Unicode files, you'll have to add some (pretty advanced) code to set the right encoding before reading the file. Also, this function will look for any file inside the given folder, but will ignore files in subdirectories.
Javascript code:
var FSO = new ActiveXObject("Scripting.FileSystemObject"); // define globally
 
/* StringInFolder: return the next line of a file after a string match
- FolderPath: path to a folder to search
- String: text to search for in file
- Multi (optional): if true, an array for multiple matches will be returned */

function StringInFolder(FolderPath, String, Multi)
{
    // Check arguments
    if(!FSO.FolderExists(FolderPath) || !String) return false;
    var Folder = FSO.GetFolder(FolderPath);
    var Return = [];
    for (var e = new Enumerator(Folder.Files); !e.atEnd(); e.moveNext())
    {
        var File = e.item();
        var Stream = File.OpenAsTextStream(1, -1);
        if (!Stream.AtEndOfStream) // shouldn't be empty
        {
            var Text = Stream.ReadAll().split("\r\n");
            var NextLine = false;
            // Loop through the lines
            for (var i = 0, len = Text.length, Line; i < len; ++i)
            {
                Line = Text[i];
                if (NextLine) // yes, we matched the previous line
                {
                    if (Multi)
                    {
                        // Add to results array
                        Return.push(Line);
                        NextLine = false;
                    }
                    else
                    {
                        // Return first result
                        Stream.Close();
                        return Line;
                    }
                }
                else if (Line.indexOf(String) !== -1) // found it!
                {
                    // Set flag to add or return the next line
                    NextLine = true;
                }
            }
        }
        Stream.Close();
    }
    if(Multi) return Return;
    return false; // when nothing is found
}


This post was edited on 08-12-2010 at 07:25 PM by Matti.
Plus! Script Developer | Plus! Beta Tester | Creator of Countdown Live | Co-developer of Screenshot Sender 5

Found my post useful? Rate me!
08-12-2010 07:24 PM
Profile E-Mail PM Web Find Quote Report
« Next Oldest Return to Top Next Newest »

Messages In This Thread
[Help]Reading a file/Opening a file - by Monckey100 on 08-12-2010 at 10:29 AM
RE: [Help]Reading a file/Opening a file - by whiz on 08-12-2010 at 11:15 AM
RE: [Help]Reading a file/Opening a file - by Matti on 08-12-2010 at 07:24 PM
RE: [Help]Reading a file/Opening a file - by Monckey100 on 08-12-2010 at 10:27 PM
RE: [Help]Reading a file/Opening a file - by Mnjul on 08-13-2010 at 08:25 AM


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On