Updated version, v0.2 using regexp search instead of simple ".indexOf()".
For simple searches with simple strings, it should work the same, just notice that you need to escape special characters with this one using a single backslash. "B.B.King (disc1)" should read "B\.B\.King \(disc1\)" e.g.
//thread:
//https://resource.dopus.com/viewtopic.php?f=3&t=21649&start=20#p11694
///////////////////////////////////////////////////////////////////////////////
// Simple dialog based search to select folders.
// Folders will be selected if they contain matching files and at the same
// time do not contain a subfolder (optional).
///////////////////////////////////////////////////////////////////////////////
var version = "0.2";
var defaultSearch = new SearchParams(
/*search string*/ 'iso',
/*search extension*/ true,
/*search stem*/ false,
/*subfolder name*/ 'video_ts',
/*exclude subfolder*/ true
);
var lastSearch = null;
///////////////////////////////////////////////////////////////////////////////
function OnClick(data){
DOpus.ClearOutput();
data.func.command.ClearFiles();
//get enumerator and count of selected folders or complete list of folders
var folders = GetFolders(data);
//get a combination of default, last used and current dialog search params
var params = GetSearchParams(data); if (!params) return; //dialog cancelled
//save current search params
SaveSearchParams(data, params);
//init, open and return progress dialog
var progress = OpenProgressDialog( data, "Folder Selector..", folders.count);
var searchMode = params.GetSearchMode();
while (!folders.enumerator.atEnd() ) {
var folder = folders.enumerator.item(); folders.enumerator.moveNext();
dout("Folder ["+folder.name+"]..");
progress.SetName(folder.name);
//if subfolder to exclude is given, search subfolders before looking for matching files
if (params.excludeFolder && defaultSearch.excludeFolderName!="" && SubFolderExists( folder.path+"\\"+folder.name, defaultSearch.excludeFolderName)){
dout(" Contains subfolder ["+defaultSearch.excludeFolderName+"], skipping folder.");
progress.StepFiles(1);
continue;
}
if (exists = FileExists( folder.path+"\\"+folder.name, params.searchMe, searchMode)){
dout(" Contains at least one matching file.");
data.func.command.AddFile(folder);
}
progress.StepFiles(1);
}
data.func.command.RunCommand("Select FROMSCRIPT DESELECTNOMATCH MAKEVISIBLE");
progress.Hide();
DOMsgBox("ISO-FolderSelector","Found "+data.func.command.files.count+" folder(s) matching your search.","Ok");
}
///////////////////////////////////////////////////////////////////////////////
function ReadFolder( path, recurse, exception){
if (recurse == undefined) recurse=false;
if (exception == undefined) exception=true;
var fEnum = DOpus.FSUtil.ReadDir( path, true);
if (fEnum.error!=0){
var error = "ReadFolder(): Error reading folder ["+name+"], code ["+fEnum.error+"].";
if (exception) throw error;
dout(error);
}
return fEnum;
}
///////////////////////////////////////////////////////////////////////////////
function SubFolderExists( folderPath, subFolderName){
subFolderName = subFolderName.toLowerCase();
var fEnum = ReadFolder( folderPath, true, true);
while (!fEnum.complete && (fItem = fEnum.Next()))
if (fItem.is_dir && (fItem.name.toLowerCase() == subFolderName))
return true;
return false;
}
///////////////////////////////////////////////////////////////////////////////
function FileExists( folderPath, searchMe, searchMode){
if (searchMode == undefined) searchMode='full';
searchMe = searchMe.toLowerCase();
if (searchMode=='ext') searchMe=searchMe.replace('\.','');
var fEnum = ReadFolder( folderPath, true, true);
while (!fEnum.complete && (fItem = fEnum.Next())){
if (fItem.is_dir) continue;
switch (searchMode){
case 'ext':
if (fItem.ext.toLowerCase().search("\."+searchMe)!=-1)
return true;
break;
case 'stem':
if (fItem.name_stem.toLowerCase().search(searchMe)!=-1)
return true;
break;
case 'full':
default:
if (fItem.name.toLowerCase().search(searchMe)!=-1)
return true;
break;
}
}
return false;
}
///////////////////////////////////////////////////////////////////////////////
function GetFolders(data){
if (data.func.sourcetab.selected_dirs.count)
var dirs = data.func.sourcetab.selected_dirs;
else
var dirs = data.func.sourcetab.dirs;
return { enumerator:new Enumerator(dirs), count:dirs.count};
}
///////////////////////////////////////////////////////////////////////////////
function DoDialog( params){
var dlg = DOpus.Dlg
dlg.window = DOpus.Listers(0);
dlg.message = "Please enter string/extension to search for:\n ";
dlg.title = "Folder Selector..";
dlg.buttons = "OK|Cancel";
dlg.icon = "question";
dlg.max = 128; //enable text field
dlg['default'] = params.searchMe;
dlg.options(0).label = "search extension";
dlg.options(0).state = params.searchExtension;
dlg.options(1).label = "search stem";
dlg.options(1).state = params.searchStem;
if (defaultSearch.excludeFolderName!=""){
dlg.options(2).label = "shall not contain folder ("+defaultSearch.excludeFolderName+")";
dlg.options(2).state = params.excludeFolder;
}
return dlg;
}
///////////////////////////////////////////////////////////////////////////////
function OpenProgressDialog( data, title, numFiles){
var progress = data.func.command.progress;
progress.delay = false;
progress.abort = true;
progress.Init(data.func.sourcetab, title);
progress.AddFiles(numFiles);
progress.Show();
return progress;
}
///////////////////////////////////////////////////////////////////////////////
function SearchParams(searchMe, searchExtension, searchStem, excludeFolderName, excludeFolder){
if (searchMe == undefined) searchMe='iso';
if (searchExtension == undefined) searchExtension=true;
if (searchStem == undefined) searchStem=false;
if (excludeFolderName == undefined) excludeFolderName="";
if (excludeFolder == undefined) excludeFolder=false;
this.searchMe=searchMe;
this.searchExtension=searchExtension;
this.searchStem=searchStem;
this.excludeFolderName=excludeFolderName;
this.excludeFolder=excludeFolder;
this.GetSearchMode = function(){
var searchMode='full';
if (this.searchExtension && !this.searchStem) searchMode='ext';
if (!this.searchExtension && this.searchStem) searchMode='stem';
return searchMode;
}
}
///////////////////////////////////////////////////////////////////////////////
function GetSearchParamsFromDialog(dlg){
var dlgSearchParams = new SearchParams();
dlgSearchParams.searchMe = dlg.input;
dlgSearchParams.searchExtension = dlg.options(0).state;
dlgSearchParams.searchStem = dlg.options(1).state;
if (defaultSearch.excludeFolderName){ //exclude subfolder name given, so option is present to exclude it
dlgSearchParams.excludeFolder = dlg.options(2).state;
}
return dlgSearchParams;
}
///////////////////////////////////////////////////////////////////////////////
function ValidateSearchParams(params){
if (!params)
return false;
if (!params.searchExtension && !params.searchStem){
DOMsgBox("Folder Selector..", "You need to select stem or extension or both!", "Oops!");
return false;
}
if (params.searchMe==""){
DOMsgBox("Folder Selector..", "You forgot to enter a string to search for!", "Oops!");
return false;
}
return true;
}
///////////////////////////////////////////////////////////////////////////////
function GetDefaultOrLastSearchParams(data){
var search = defaultSearch;
if (data.func.sourcetab.vars.exists("lastSearch"))
search = Vector2Search(data.func.sourcetab.vars.get("lastSearch"));
return search;
}
///////////////////////////////////////////////////////////////////////////////
function GetCombinedSearchParams(data){
var userSearchParams = GetDefaultOrLastSearchParams(data);
var dlg = DoDialog(userSearchParams);
var result=dlg.Show();
if (result==0){
dout("Folder Selector dialog cancelled.");
return null;
}
return GetSearchParamsFromDialog(dlg);
}
///////////////////////////////////////////////////////////////////////////////
function GetSearchParams(data){
var params = null;
while (!ValidateSearchParams(params)){
params = GetCombinedSearchParams(data);
if (!params)
return false; //dialog canceled
}
return params;
}
///////////////////////////////////////////////////////////////////////////////
function SaveSearchParams(data, params){
data.func.sourcetab.vars.set("lastSearch", Search2Vector(params));
}
///////////////////////////////////////////////////////////////////////////////
function Search2Vector( search){
var vector = DOpus.NewVector();
vector.push_back(search.searchMe);
vector.push_back(search.searchExtension);
vector.push_back(search.searchStem);
vector.push_back(search.excludeFolderName);
vector.push_back(search.excludeFolder);
return vector;
}
///////////////////////////////////////////////////////////////////////////////
function Vector2Search( vector){
return new SearchParams( vector(0), vector(1), vector(2), vector(3), vector(4));
}
///////////////////////////////////////////////////////////////////////////////
function DOMsgBox(title,message,buttons){
var dlg = DOpus.Dlg
dlg.window = DOpus.Listers(0);
dlg.message = message;
dlg.title = title;
dlg.buttons = buttons;
var result = dlg.Show();
dout(title+" - "+message+" - "+buttons+" -> "+result);
return result;
}
///////////////////////////////////////////////////////////////////////////////
function dout(text){
DOpus.Output(text);
}