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!
How to check selected item is a folder?
Re: How to check selected item is a folder?
The exists(item) function returns 2 if "item" is a folder
::rtfm "idh_scripting_comref.htm#idh_sc_exists";
::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 ]
[ this user is asleep ]
Re: How to check selected item is a folder?
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.
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?
You could refine the check by e.g.:
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
Code: Select all
$driveName = "C:";
if (regexmatches($driveName, "^[A-Z]:[\\]?$")) {
msg "It is a drive (without any additional folders given)";
}
^ = 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
Re: How to check selected item is a folder?
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 ]
[ this user is asleep ]
Re: How to check selected item is a folder?
I was going to suggest using the Report SC: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
$type = Report('{Dir Directory|File|Drive}', $item);
$type = ($type == '') ? 'Unknown' : $type;
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?
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
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:
::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
[AHK] redirecting Windows Explorer to XY, [XYS] Mini Tree with open tabs (cur loc expanded, tab folders highlighted), [AHK] customInlineRenameKeys, [AHK] clipboardHelper_and_XYEscToList