Initially I thought it would be easy to adapt one to function as another, but in reality they (folders and files) must be treated as separated entities.
Suggested usages:
-quickly change files with different configs (I did this to test and report a problem with admin.ini, for instance - it avoids the booooring renaming required for that.
-quickly change localization files that must have a hardcoded basename so the main app can read it properly - so you can get an "english.ini" file and fast switch "<your language>.ini" to test your translation jobs.
Since it's an error-prone task (it requires extra attention on setup, mainly) I've focused on error-checking, so if you follow what the windows' titles tell, you won't get wrong - and if you get it wrong, script will punish you!
Some highlights adapted from Themes Switcher that worth a look:
-added a way to easily set a predefined path for the script to work, thus avoiding initial browsing for the main app's dir AFTER the initial setup.
-now it lists similar base-named files to inform user on what names *not* to use, prior to chose an alternative name.
-additional error-checking code to stop script if it detects potential name collision that would lead to unwanted effects (I've tried to force the script to reload itself at this point and it did it great, but it showed an annoying duplication of a "select file" step that, despite not harmful, could lead to boring/mistakes).
-better warnings, rephrasing (shorter windows to display messages lead to some non-standard but working solutions... sorry about that!).
-improved preview of what will happen and to show who's who, now and after the changes if they are accepted by user.
-better ending status messages that will tell the user what is what, and keeps the last used main file selected in list.
-suggestion: if you intend to use this on several files spread across HDD, you may also want to make use of <curpath> at the beginning of the code, here:
Code: Select all
inputfolder $mpath, "<curpath>"Code: Select all
"File Names Switcher|:steps"
$cfn = "<curname>";
inputfolder $mpath, "c:\", "Please indicate your main application's path<crlf>which contains the target main file:"; //disable this to get no prompts on main app's path.
// goto "c:\path\"; $mpath = "<curpath>"; //enable this to get no prompts regarding main app's path; optional - disable the next command.
goto $mpath;
$ini = "$mpath\z_FileNamesHandler.ini"; IF (exists($ini) == 0)
{
new $ini, file;
echo "$ini was not present and it has been created!<crlf>It is STRONGLY recommended that you set it up now.<crlf>Please click 'OK' to proceed."; focus; selfilter """$cfn"""; //CFN
sub "_SETUP";
}
elseif
{
selfilter """z_FileNamesHandler.ini""";
$size=report("{Size}", 1);
IF (($size) == 0) {
focus; selfilter """$cfn"""; $cnfrm = confirm("The program requires a basic setup which was not completed thus far.<crlf><crlf>Please click 'OK' to config it now<crlf> or 'Cancel' to end this script.")?"YES":"NO");
IF ($cnfrm == YES) { sub "_SETUP" }
ELSE ($cnfrm == NO) { sub "_NOP" }
}
$th = getkey("Default", "1_ThemesDEF", "$ini");
$base = getkey("Path", "2_Path", "$ini");
$ini = getkey("ini", "2_INI", "$ini");
$tback = getkey("Old", "1_ThemesOLD", "$ini");
$tfut = getkey("Current", "1_ThemesCUR", "$ini");
selfilter """$cfn"""; //CFN
$answer = confirm("New '$th' main file will be<crlf>'$tback'.<crlf>Current file '$th' will be renamed as<crlf>'$tfut'<crlf>and stored for future fast switches.<crlf><crlf>Please click 'OK' to continue or 'Cancel' to choose another file.")?"_YES":"_NO";
IF ($answer == _YES) { }
ELSE ($answer == _NO) { sub "_NON" }
$tback = getkey("Old", "1_ThemesOLD", "$ini");
$tt = getkey("Default", "1_ThemesDEF", "$ini");
selfilter """$tt""";
$tc = getinfo("CountSelected");
IF (($tc) != 1) { status "Fatal error detected: '$th' not present.", BF0000, stop; end 1, "The file '$th' is not available and it's required for the script to work.<crlf>Please check what's wrong and fix it!<crlf><crlf>This script will now be stopped.<crlf><crlf>Have a nice day! ^^"; }
// #485; //refresh list - avoid surprises on list...
selfilter """$tt""";
rename r, "$tt > themeTMP";
// #485; //refresh list - see what's going on...
$tback = getkey("Old", "1_ThemesOLD", "$ini");
selfilter """$tback""";
rename r, "$tback > $tt";
// #485; //refresh list - see what's going on...
$ct = getkey("Current", "1_ThemesCUR", "$ini");
selfilter """themeTMP""";
rename r, "themeTMP > $ct";
setkey "$mpath", "Path", "1_Path", "$ini";
setkey "$tback", "Current", "1_ThemesCUR", "$ini";
$nt = getkey("Default", "1_ThemesDEF", "$ini");
setkey "$nt", "Default", "1_ThemesDEF", "$ini";
setkey "$ct", "Old", "1_ThemesOLD", "$ini";
#485; //refresh list - to display updated .ini...
echo "'$tback' is your new '$nt' main file!";
status "'$tback' is now '$nt'.";
"_NON"
$ini = "<curpath>\z_FileNamesHandler.ini";
$tnn = getkey("Default", "1_ThemesDEF", "$ini");
$resp = confirm("Please click 'OK' to select another file which will be set<crlf>as current '$tnn' main file or 'Cancel' to end this script.")?"_YO":"_NOP";
IF ($resp == _YO) { sub "_YO"; }
ELSE ($resp == _NO) { sub "_NOP"; }
"_NOP"
// msg "Operation canceled by user.<crlf>Have a nice day! ^^";
// status text, [color RRGGBB], [icon=ready|progress|alert|stop]
status "Operation canceled by user.", BF0000, alert;
end 1;
"_SETUP"
$ini = "<curpath>\z_FileNamesHandler.ini";
inputfile $th, "<curpath>", , "Please enter the default main file name:";
$c2r = "$th _";
replace $c2r, $c2r, "$th _", "$th _"; replace $c2r, $c2r, " ", "";
replace $c2r, $c2r, "<curpath>\", "";
replace $th, $th, "<curpath>\", "";
selfilter "$th";
$rep = get("SelectedItemsNames", "<crlf>");
$ct = input("Cur. '$th' file - set alternative name 4 it:", "('$c2r' will be auto-prepended)<crlf><crlf>Currently on list (you should REALLY avoid them):<crlf>$rep", "ALT", "S");
$sug = replace ("$ini", "z_FileNamesHandler.ini", "");
$cr1 = "$th $ct";
replace $cr1, $cr1, "$th $ct", "$th _$ct"; replace $cr1, $cr1, " ", "";
selfilter """$cr1""";
$ex = getinfo("CountSelected");
IF ($ex == 1) { status "Error: '$cr1' would crash script.", BF0000, stop; end 1, "~~~~~~~~~~---------~~~~~~~~~~<crlf> ~~~~~~~-WARNING-~~~~~~~<crlf>~~~~~~~~~~---------~~~~~~~~~~<crlf><crlf>Setup has detected that a file of same name<crlf>already exists and it could lead to several problems.<crlf><crlf>Please run the script again and pay attention to the list<crlf>presented list during setup and avoid duplicates.<crlf><crlf>Fatal error detected: '$cr1' would be<crlf>duplicated and could crash either the script or the operation.<crlf>Have a nice day! ^^<crlf><crlf>~~~~~~~~~~---------~~~~~~~~~~"; }
ELSE ($ex != 1) { }
inputfile $old, "$sug", , "Replacement 4 cur file in future swaps/!cur. '$th' >be: '$cr1':";
$old = replace ("$old", "$sug", "");
setkey "<curpath>", "Path", "2_Path", "$ini";
setkey "$ini", "ini", "2_INI", "$ini";
//test: ::$th = 1; $ct = ab; $y = "$th $ct"; replace $crr, $y, "$th $ct", "$th_$ct"; echo $crr;
//test OK: ::$th = 1; $ct = ab; $y = "$th $ct"; replace $crr, $y, "$th $ct", "$ct _$th"; replace $crr, $crr, " ", ""; echo $crr;
$crr = "$th $ct";
replace $crr, $crr, "$th $ct", "$th _$ct"; replace $crr, $crr, " ", "";
setkey "$crr", "Current", "1_ThemesCUR", "$ini";
setkey "$th", "Default", "1_ThemesDEF", "$ini";
setkey "$old", "Old", "1_ThemesOLD", "$ini";
#485; //refresh list - to display updated .ini; generates a bug if not present (runs setup a 2nd time)...
"_YO"
$ini = "<curpath>\z_FileNamesHandler.ini";
$tnow = getkey("Current", "1_ThemesCUR", "$ini");
$ty = getkey("Default", "1_ThemesDEF", "$ini");
$tback = getkey("Old", "1_ThemesOLD", "$ini");
inputfile $oldy, "<curpath>", ,"Prev. choice: $tback| Cur. is: $tnow| Choose new:";
$oldy = replace ("$oldy", "<curpath>\", "");
setkey "$oldy", "Old", "1_ThemesOLD", "$ini";
XYplorer Beta Club