Page 1 of 1
How to check selected item is a folder?
Posted: 05 Aug 2014 14:50
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!
Re: How to check selected item is a folder?
Posted: 05 Aug 2014 14:58
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
}
Re: How to check selected item is a folder?
Posted: 05 Aug 2014 15:26
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.
Re: How to check selected item is a folder?
Posted: 05 Aug 2014 15:36
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
Re: How to check selected item is a folder?
Posted: 05 Aug 2014 16:53
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)";
}
Re: How to check selected item is a folder?
Posted: 05 Aug 2014 17:03
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
}
Re: How to check selected item is a folder?
Posted: 06 Aug 2014 08:51
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