Shoutbox

[Help]Reading a file/Opening a file - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Messenger Plus! for Live Messenger (/forumdisplay.php?fid=4)
+---- Forum: Scripting (/forumdisplay.php?fid=39)
+----- Thread: [Help]Reading a file/Opening a file (/showthread.php?tid=95231)

[Help]Reading a file/Opening a file by Monckey100 on 08-12-2010 at 10:29 AM

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?


RE: [Help]Reading a file/Opening a file by whiz on 08-12-2010 at 11:15 AM

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


RE: [Help]Reading a file/Opening a file by Matti on 08-12-2010 at 07:24 PM

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
}


RE: [Help]Reading a file/Opening a file by Monckey100 on 08-12-2010 at 10:27 PM

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


RE: [Help]Reading a file/Opening a file by Mnjul on 08-13-2010 at 08:25 AM

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