Show/Hide everything (need help with script)

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
John_C
Posts: 336
Joined: 16 May 2018 20:04

Show/Hide everything (need help with script)

Post by John_C »

Here are two scripts which will show/hide any hidden files, folders or drives.

Is there a way to combine them somehow? I mean, I press custom button on toolbar and this button will toggle between these two scripts. Is it possible?

Code: Select all

"Show Everything"
    $HideProtectedOperatingSystemFiles = get("#388");
    $ShowHiddenDrives = get("#492");
    $ShowHiddenFilesAndFolders = get("#493");
    $ShowSystemFilesAndFolders = get("#494");

    if ($HideProtectedOperatingSystemFiles == 1) { #388; }
    if ($ShowHiddenDrives == 0) { #492; }
    if ($ShowHiddenFilesAndFolders == 0) { #493; }
    if ($ShowSystemFilesAndFolders == 0) { #494; }

"Hide Everything"
    $HideProtectedOperatingSystemFiles = get("#388");
    $ShowHiddenDrives = get("#492");
    $ShowHiddenFilesAndFolders = get("#493");
    $ShowSystemFilesAndFolders = get("#494");

    if ($HideProtectedOperatingSystemFiles == 0) { #388; }
    if ($ShowHiddenDrives == 1) { #492; }
    if ($ShowHiddenFilesAndFolders == 1) { #493; }
    if ($ShowSystemFilesAndFolders == 1) { #494; }

highend
Posts: 15004
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Show/Hide everything (need help with script)

Post by highend »

Code: Select all

    perm $P_Show_Hide_State; // 1 = hidden, 2 = shown
    if (!regexmatches($P_Show_Hide_State, "(1|2)")) { $P_Show_Hide_State = 1; }

    // Show everything
    if ($P_Show_Hide_State == 1) {
        $HideProtectedOperatingSystemFiles = get("#388");
        $ShowHiddenDrives = get("#492");
        $ShowHiddenFilesAndFolders = get("#493");
        $ShowSystemFilesAndFolders = get("#494");

        if ($HideProtectedOperatingSystemFiles == 1) { #388; }
        if ($ShowHiddenDrives == 0) { #492; }
        if ($ShowHiddenFilesAndFolders == 0) { #493; }
        if ($ShowSystemFilesAndFolders == 0) { #494; }
        $P_Show_Hide_State = 2;

    // Hide everything
    } elseif ($P_Show_Hide_State == 2) {
        $HideProtectedOperatingSystemFiles = get("#388");
        $ShowHiddenDrives = get("#492");
        $ShowHiddenFilesAndFolders = get("#493");
        $ShowSystemFilesAndFolders = get("#494");

        if ($HideProtectedOperatingSystemFiles == 0) { #388; }
        if ($ShowHiddenDrives == 1) { #492; }
        if ($ShowHiddenFilesAndFolders == 1) { #493; }
        if ($ShowSystemFilesAndFolders == 1) { #494; }
        $P_Show_Hide_State = 1;
    }
Alternatively use get("trigger"); and execute either one on left / right clicking
the button...
One of my scripts helped you out? Please donate via Paypal

John_C
Posts: 336
Joined: 16 May 2018 20:04

Re: Show/Hide everything (need help with script)

Post by John_C »

highend wrote:...
Thank you very much :)

John_C
Posts: 336
Joined: 16 May 2018 20:04

Re: Show/Hide everything (need help with script)

Post by John_C »

highend, could you describe what is the purpose of "if (!regexmatches..." line?

Code: Select all

"Show Everything"
    perm $P_Show_Hide_State; // 0 = Hidden, 1 = Shown
    if (!regexmatches($P_Show_Hide_State, "(0|1)")) { $P_Show_Hide_State = 0; }

    // #388 - Hide protected operating system files
    // #493 - Show hidden files and folders
    // #494 - Show system files and folders

    // Show everything
    if ($P_Show_Hide_State == 0) {
        if (get("#388") == 1) { #388; }
        if (get("#493") == 0) { #493; }
        if (get("#494") == 0) { #494; }
        $P_Show_Hide_State = 1;
        ctbstate(1);
    }

    // Hide everything
    elseif ($P_Show_Hide_State == 1) {
        if (get("#388") == 0) { #388; }
        if (get("#493") == 1) { #493; }
        if (get("#494") == 1) { #494; }
        $P_Show_Hide_State = 0;
        ctbstate(0);
    }
And question about coding style. In your scripts, I see that you prefer lowerCamelCase for local variables. But in this example you use Upper_Snake_Case for permanent variable. Is it your general coding style convention:

Code: Select all

localVariable
P_Permanent_Variable
?

highend
Posts: 15004
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Show/Hide everything (need help with script)

Post by highend »

Try this as a script:

Code: Select all

    text "_" . $myVar . "_";
    $myVar = "some value";
    text "_" . $myVar . "_";
In other words: If a variable has NO value at all, it's value is the name of the variable itself
So: That regex is checking if the variable is set to either 1 or 2 and if this is NOT the case, init
it to a default value (you're using an older version of my script which used 0 | 1 values, which
is not an ideal way to check them -> switched them to 1 | 2)

Everyone likes different variable names. $P_... are used for permanent ones. Iirc even XYs help file recommends
this (and I agree on this). You are free to use your own scheme (as long as the names are valid)
$MyVar
$myVar (I like this one)
$My_Var
$my_var
etc.
One of my scripts helped you out? Please donate via Paypal

John_C
Posts: 336
Joined: 16 May 2018 20:04

Re: Show/Hide everything (need help with script)

Post by John_C »

highend wrote:If a variable has NO value at all, it's value is the name of the variable itself
So: That regex is checking if the variable is set to either 1 or 2 and if this is NOT the case, init
it to a default value
Yeah, I understand. But it seems, as I tested, the initial value is always 0.
... 0 | 1 values, which is not an ideal way to check them -> switched them to 1 | 2
But why?

highend
Posts: 15004
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Show/Hide everything (need help with script)

Post by highend »

It's 0 because the way to check for it isn't a viable way (it will always be reinitialized to 0 because of the return value of regexmatches). Because of that I switched it to 1/2
One of my scripts helped you out? Please donate via Paypal

klownboy
Posts: 4469
Joined: 28 Feb 2012 19:27
Location: Windows 11, 25H2 Build 26200.8737 at 100% 2560x1440

Re: Show/Hide everything (need help with script)

Post by klownboy »

Hi highend, when establishing the initial state of the permanent variable $P_Show_Hide_State, couldn't we use SC isset in this case? Of course we can't initialize the perm variable before the use of isset check since the result would always be "1", but you could set it perm afterwards. If everything works as it should, the perm variable check is only done the first time the script is used. For the very first run you don't really care if it's 1 or 2 only that it's not 1 or 2. Something like the following works (but you prefer using regex :) ):

Code: Select all

    if(! isset($P_Show_Hide_State)) {$P_Show_Hide_State = 1; }
    perm $P_Show_Hide_State;  // 1 = hidden, 2 = shown

highend
Posts: 15004
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Show/Hide everything (need help with script)

Post by highend »

Hi Ken,

sure, this would work. But I'm additionally initializing the variable to a default value if it isn't set correctly at all (but still set) in a single step.
That's why I prefer "my" way :mrgreen:
One of my scripts helped you out? Please donate via Paypal

Post Reply