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:
Monckey100
New Member
*


Posts: 2
Joined: Aug 2010
O.P. [Help]Reading a file/Opening a file
I have a specific task i wish to do for a script of mine, i want to make it so a folder is scanned and then each file is searched for a specific string and then the found line after it is stored in a variable, I know this is possible via C++ but i am relatively new to javascript and I have done quite abit of googling and found nothing. I wasn't sure if ActiveX was possible in MsgPlus or not, or any of those other fancy languages.(that i seem to have a hard time understand how to implement onto MsgPlus)

If this isn't possible via JScript then how may i make it so my Jscript sends a variable to a C++ DLL and then have the dll return the variable it has stored back?
08-12-2010 10:29 AM
Profile E-Mail PM Find Quote Report
whiz
Senior Member
****


Posts: 568
Reputation: 8
– / – / Flag
Joined: Nov 2008
RE: [Help]Reading a file/Opening a file
I can't test this right now, but this should do the trick.

jscript code:
var FSO = new ActiveXObject("Scripting.FileSystemObject"); // define globally

/* StringInFolder: return the next line of a file after a string match
- Folder: 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(Folder, String, Multi)
{
    var Folder = FSO.GetFolder(Path);
    var Return = [];
    for (var Enum = new Enumerator(Folder.Files), !Enum.atEnd(), Enum.moveNext())
    {
        var File = Enum.item();
        var Stream = File.OpenAsTextStream(1, -1);
        if (!Stream.AtEndOfStream) // shouldn't be empty
        {
            var Text = Stream.ReadAll().split("\r\n");
            var NextLine = false;
            for (var Line in Text)
            {
                if (NextLine) // yes, we matched the previous line
                {
                    if (Multi) // do we want an array of matches?
                    {
                        Return.push(Line);
                        NextLine = false;
                    }
                    else
                    {
                        return Line;
                    }
                }
                else if (Line.indexOf(String) !== -1) // found it!
                {
                    NextLine = true; // flag to check the next line
                }
            }
        }
        Stream.Close();
    }
}

var Result = StringInFolder("C:\\", "test", true); // example usage

This post was edited on 08-12-2010 at 11:41 AM by whiz.
08-12-2010 11:15 AM
Profile E-Mail PM Find Quote Report
Matti
Elite Member
*****

Avatar
Script Developer and Helper

Posts: 1646
Reputation: 39
32 / 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.
js 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
Monckey100
New Member
*


Posts: 2
Joined: Aug 2010
O.P. RE: [Help]Reading a file/Opening a file
thank you both of you, i was looking on google for something like this but every time it would be like "that would create a security hole" or use ActiveX so before resorting to hoping ActiveX would work i came here.

Edit: it continously returned false.
.-.
var testing = StringInFolder('J:\Program Files\Messenger Plus! Live\Scripts\Monckey256\test', "Kai", false)
ChatWnd.SendMessage(testing);

This post was edited on 08-12-2010 at 11:45 PM by Monckey100.
08-12-2010 10:27 PM
Profile E-Mail PM Find Quote Report
Mnjul
forum super mod
******

Avatar
plz wub me

Posts: 5396
Reputation: 58
– / Other / Flag
Joined: Nov 2002
Status: Away
RE: [Help]Reading a file/Opening a file
quote:
Originally posted by Monckey100
var testing = StringInFolder('J:\Program Files\Messenger Plus! Live\Scripts\Monckey256\test', "Kai", false)
You need to use double backslashes in a string, so:

var testing = StringInFolder('J:\\Program Files\\Messenger Plus! Live\\Scripts\\Monckey256\\test', "Kai", false);
08-13-2010 08:25 AM
Profile PM Web Find Quote Report
« Next Oldest Return to Top Next Newest »


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