How to check selected item is a folder?

Discuss and share scripts and script files...
Post Reply
Greylock
Posts: 15
Joined: 28 Jan 2014 13:10

How to check selected item is a folder?

Post by Greylock »

Hi,

I'm fairly new to XYplorer scripting so I'm experimenting with a simple script to append a word to the selected folder name. Can anyone tell me if there's a way to verify that the selected item is a folder before performing the rename?

Many Thanks!

bdeshi
Posts: 4256
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612
Contact:

Re: How to check selected item is a folder?

Post by bdeshi »

The exists(item) function returns 2 if "item" is a folder
::rtfm "idh_scripting_comref.htm#idh_sc_exists";

Code: Select all

 if (exists("arg1") == 2) {
  // if arg1 is a folder, execute this codeblock
 }
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

Greylock
Posts: 15
Joined: 28 Jan 2014 13:10

Re: How to check selected item is a folder?

Post by Greylock »

Thank you for the quick response.

It would appear that '2' indicates that the item is a "folder; drive; share; server". Is this as accurate a distinction as it is possible to make?

Running my rename script on a drive (admittedly not something that should occur in use) results in a script error as the exists(item) == 2 evaluates to true.

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

Re: How to check selected item is a folder?

Post by highend »

You could refine the check by e.g.:

Code: Select all

    $driveName = "C:";
    if (regexmatches($driveName, "^[A-Z]:[\\]?$")) {
        msg "It is a drive (without any additional folders given)";
    }
Regex explanation:
^ = Beginning of line
[A-Z] = Range capital A - Z
: = :
[\\]? = \\ = literal \ | []? = make it's existance optional
$ = End of line

So if a drive consists of either
C:
or
C:\

the check will return true

but if it's e.g. a folder like
C:\Windows

it'll return false
One of my scripts helped you out? Please donate via Paypal

bdeshi
Posts: 4256
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612
Contact:

Re: How to check selected item is a folder?

Post by bdeshi »

slightly faster:

Code: Select all

    $driveName = "C:";
    if (strlen($driveName) < 4) && (strpos($drivename, ':') != -1) {
        msg "It is a drive (without any additional folders given)";
    }
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

Re: How to check selected item is a folder?

Post by TheQwerty »

I was going to suggest using the Report SC:

Code: Select all

$type = Report('{Dir Directory|File|Drive}', $item);
$type = ($type == '') ? 'Unknown' : $type;
But that will currently crash XY if $item is a drive.

If checking the string is enough I think I'd go with either two Likes or a trimmed Like:

Code: Select all

$item = 'C:';
// Could test: $item LikeI '[A-Z]:' OR $item LikeI '[A-Z]:\'
if (trim($item,'\','R') LikeI '[A-Z]:') {
  // Item is a drive
}

autocart
Posts: 1328
Joined: 26 Sep 2013 15:22

Re: How to check selected item is a folder?

Post by autocart »

Since everyone is posting alternatives:
To figure out if it is a folder you can also do a:
strpos(property("Attributes"), "D") > -1
Example:
::echo strpos(property("Attributes"), "D") > -1 ? "It is a folder." : "It is not a folder.";
The theoretical advantage would be that you do not "have" to manually collect the currently selected file/folder, since the property-function will do this for you.
HOWEVER, if no item is selected, then, according to my tests, it will either refer to the folder, which's content is currently displayed in the list, or return nothing (e.g. if parent is "C:\").
EDIT:
So you will not get around using <curitem>:
::echo strpos(property("Attributes"), "D", <curitem>) > -1 ? "It is a folder." : "It is not a folder.";

Using <curitem> changes NOTHING regarding the property-function.
EDIT 2:
Unless, "C:\" is selected in the "Computer" tab. In this case <curitem> changes the results of property("Attributes").
Best thing: Check <curitem> if it is an empty string "". Only do something, if <curitem> contains something.
// I do not like the XY scripting environment. It's not clear and consistent enough to me and also lacking some basic functions at times. Often you have to find a work-around. And anyway do a lot of testing, double checking,... always

Post Reply