function OnInit(initData) { initData.name = "FilterCmd"; initData.version = "1.1"; initData.copyright = "(c) 2020 Leo Davidson"; initData.url = "https://resource.dopus.com/t/filtercmd-run-another-command-on-files-matching-a-wildcard/36059"; initData.desc = "Run a command on a subset of the selected files"; initData.default_enable = true; initData.min_version = "12.0"; var cmd = initData.AddCommand(); cmd.name = "FilterCmd"; cmd.method = "OnFilterCmd"; cmd.desc = "Run a command on a subset of the selected files."; cmd.label = "FilterCmd"; cmd.template = "PATTERN/K,FILE/M,CMD/R"; cmd.hide = false; cmd.icon = "recursivefilter"; } function OnFilterCmd(scriptCmdData) { var args = scriptCmdData.func.args; if ((!args.got_arg.pattern && !args.got_arg.file) || !args.got_arg.cmd) return; var filesOnly = true; var tab = scriptCmdData.func.sourcetab; var cmdButton = scriptCmdData.func.command; cmdButton.deselect = false; // Leave everything selected // Make a new command object, independent of the original one var cmd = DOpus.Create.Command(); cmd.SetSourceTab(tab); var wild = null; if (args.got_arg.pattern) { wild = DOpus.FSUtil.NewWild(args.pattern, "f"); } var anyFiles = false; if (args.got_arg.file) { var vecFiles = args.file; var fsu = DOpus.FSUtil; for (var eSel = new Enumerator(args.file); !eSel.atEnd(); eSel.moveNext()) { var item = fsu.GetItem(eSel.item()); if (item.attr !== undefined) { if ((!filesOnly || !item.is_dir) && (!wild || wild.Match(item.name))) { cmd.AddFile(item); anyFiles = true; } } } } else { for (var eSel = new Enumerator(tab.selected); !eSel.atEnd(); eSel.moveNext()) { var item = eSel.item(); if ((!filesOnly || !item.is_dir) && (!wild || wild.Match(item.name))) { cmd.AddFile(item); anyFiles = true; } } } if (anyFiles) { cmd.RunCommand(args.cmd); } }