To complete this thread here is the working AFL to read in a list of sub-folder names filtered by tag characters (my requirement). Pay special attention to the comments about enabling the FileSystemObject. Also inclosed is an AFL for reading a list of file names filtered by extension.
Thanks to Joe Landry for getting me headed in the right direction.
EnableScript("JScript");
// jScript function to return a list of subfolder names
// filtered by appended tag chars (sort of like an extension on a file)
// Tag = "" returns all without filtering
// Caution: the FileSystemObject must be enabled on the system
// by doing the following:
// <Start> <Run...> regsvr32 scrrun.dll <OK>
// Once enabled, malware may also have access to this object
// which enables jScript to read and write files and folders!
//
<%
function GetSubFolderNames(Path,Tag)
{
var FSO = new ActiveXObject("Scripting.FileSystemObject");
var Folder = FSO.GetFolder(Path);
var FolderEnumerator = new Enumerator(Folder.SubFolders);
var Foldernames = "";
for(; !FolderEnumerator.atEnd(); FolderEnumerator.moveNext())
{
Folder = FolderEnumerator.item();
var Len = Folder.Name.length;
var tagLen = Tag.length;
if(Folder.Name.substring(Len-tagLen, Len) == Tag)
{
Foldernames += Folder.name.substring(0, Len-tagLen) + ",";
}
}
return Foldernames.substring(0, Foldernames.length-1);
}
%>
// Get csv list of names of param sets in the current ChartMode
// Current ChartMode types: "t"|"v"|"r"
// Params are located in folders named SetName+ChartMode
function setsList()
{
global ChartMode;
global ChartID;
global PersistentPath1;
filePath = PersistentPath1+"Chart"+ChartID;
script = GetScriptObject();
fileList = script.GetSubFolderNames(filePath,ChartMode);
return fileList;
}
// JScript function enumerates files of specified type
// Based on code supplied by Joe Landry 1/16/2008
// Access it like the setsList() function above
<%
function EnumFiles(Path, Extension)
{
var FSO = new ActiveXObject("Scripting.FileSystemObject");
var Folder = FSO.GetFolder(Path);
var FileEnumerator = new Enumerator(Folder.Files);
var Filenames = "";
var Ext = "";
var Len = 0;
for(; !FileEnumerator.atEnd(); FileEnumerator.moveNext())
{
var File = FileEnumerator.item();
Len = File.Name.length;
Ext = File.Name.substring(Len-3, Len);
if(Ext == Extension)
{
Filenames += File.Name.substring(0, Len-4) + ",";
}
Len = Filenames.length;
}
return Filenames.substring(0, Len-1);
}
%>
On Jan 16, 2008, at 3:53 PM, Dennis Brown wrote:
Joe,
Thank you for your email help. I have it working now. I was thrown for a while because the:
new ActiveXObject("Scripting.FileSystemObject");
statement would not run. I did not have that object enabled on my machine and had to register it first.
Thanks,
Dennis
On Jan 15, 2008, at 6:46 AM, Joe Landry wrote:
Dennis -
Not in AFL but in jScript that you can put in your AFL scripts. There's a function in jScript(C++ as well?) called Enumerator that you can use to get the list of files in a directory as shown below. This is in a routine for automated back test that's somewhere in the Amibroker library from 2002.
I also have it written as a more generalized function and if you'd like further help write to me directly.
Hope this helps,
Joe Landry
f = fso.GetFolder(AFLFolder);
fc = new Enumerator(f.files);
for (; !fc.atEnd(); fc.moveNext())
/*****************************
*
* BatchTest.js
*
* Batch testing sample script
* Shows how to use JScript and new AmiBroker 4.23
* 'Analysis' object to perform batch backtesting
* and generate reports
*
* Created: Dec 21, 2002 TJ
* Last modification: Dec 22, 2002 TJ
*
* Copyright (C)2002 Amibroker.com
*
* Status: Freeware
* You can use/modify/adopt this code freely
*
*/
/* The directory where AFL files are stored
** Also reports generated by the bactest
** will be saved here
*/
AFLFolder = "C:\\Program Files\\AmiBroker\\AFL"; // MODIFY TO FIT YOUR SETUP
WScript.Echo("Batch testing of all AFL files stored in " + AFLFolder );
var AB, AA;
var fso, f, f1, fc, s;
var filename;
/* Create AmiBroker object and get Analysis object */
AB = new ActiveXObject("Broker.Application");
AA = AB.Analysis;
/* backtest over symbols and all quotes*/
AA.ClearFilters();
AA.ApplyTo = 0; // use symbols
AA.RangeMode = 0; // all quotes
/* to use filters you should uncomment lines below
// AA.ApplyTo = 2; // use filters
// AA.Filter(0,"watchlist") = 2 /* watch list number */;
// AA.Filter(0,"group") = 0 /* group number */;
/* Create FileSystemObject */
fso = new ActiveXObject("Scripting.FileSystemObject");
/* Iterate through all files in the folder */
f = fso.GetFolder(AFLFolder);
fc = new Enumerator(f.files);
for (; !fc.atEnd(); fc.moveNext())
{
// we need to add empty string to make sure that filename is a string object
filename = "" + fc.item();
/* check the AFL extension */
if( filename.substr( filename.length - 4 ).toUpperCase() == ".AFL" )
{
if( AA.LoadFormula( filename ) )
{
AA.Backtest();
reportname = filename.substr( 0, filename.length - 3 ) + "HTML" ;
AA.Report( reportname ); // generate report
}
}
}
----- Original Message -----
Sent: Monday, January 14, 2008 10:33 PM
Subject: [amibroker] How do I read dir names in AFL
Hello,
I am stumped on this problem. I want to read the names of a set of
folders that are inside another folder. I want to do this in AFL
because, I want to make a pulldown list param of the folder names. I
will use this to select a parameter set to switch to. Right now, I
have to remember the names of the folders or look them up in Explorer
and type the name into a text field param. I would rather be able to
just select from a list of the valid folders that were created.
I could do this indirectly if I keep track of every folder I ever
created in a file, and then use that list. However, it would require
a lot of housekeeping code to clean up this list from manual
additions and deletions in Explorer.
Does anyone know how to read the folder names directly?
Thank you,
Dennis