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
}