Copy link to clipboard
Copied
Hello,
I want to check that given path is File or Directory in a jsx.
The 'isFileExist' function only check that path is exist or not in the file system.
I want to check it is 'folder' or 'file'
For example,
path = 'd:/folder1' then isFile method should return 'false'
path = 'd:/folder1/1.epr' then isFile method should return 'true'
How can I do that? I am using below code.
isFileExist : function(path) {
var file = new File(path);
return file.exists;
}
isFile : function(path) {
//How can I check that path is 'file' or 'directory'?
}
Premiere Pro version 11.0
Bruce Bullis wrote
Here's one way to tell the difference.
var fileOrFolder = new File('/Users/bbb/Desktop/example.epr');
var type = typeof fileOrFolder;
if (fileOrFolder.commonFiles){
alert("It's a folder.");
} else {
alert("It's a file.");
}
Wait, what? Did you test that code?
Copy link to clipboard
Copied
Rather confusingly, the File and Folder classes are somewhat interchangeable. This is because File() should be able to produce a new Folder instance automatically for you. This means that you need to do some constructor checking to be safe.
This explanation will initially use the Folder class instead of the File class due to issues with the File() function not working as described in the documentation. See below for more.
From the documentation:
Folder (path: string)
Core JavaScript Classes
Creates and returns a new Folder object referring to a given file-system location.
If the path name refers to an already existing disk file, a File object is returned instead. Returns the new Folder object.
path (optional): Data Type: string
The absolute or relative path to the folder associated with this object, specified in URI format.
The value stored in the object is the absolute path. The path need not refer to an existing folder. If the path refers to an existing file, rather than a folder:
- The Folder() function returns a File object instead of a Folder object.
- The new operator returns a Folder object for a nonexisting folder with the same name.
I colored the pertinent parts. Specifically, the Folder(pathToCheck) function will return an object of the corresponding type - if the path refers to a file, the returned object will be an instance of File, not Folder. However, if you use the new keyword, when calling that function, it will return you a Folder object for a "nonexisting folder" with the same name. Unfortunately, the Folder.exists property incorrectly (or, at the very least, unexpectedly) returns "true" in this case, even though you're working with a "nonexisting" folder.
According to the documentation, the same holds true for the File class constructor. However, in my local testing, this does not appear to be the case. Confusingly, the File() function (without using new) returns a File object, even when run on a directory (see below for more).
In order to perform the checks you're looking for, do the following:
function doesPathExist(path)
{
// File() always returns a File object instance.
// Use 'exists' to determine if there is a file system entry at the specified path.
return File(path).exists;
}
function doesFileExist(path)
{
var file = Folder(path); // Returns a File object if the path exists and is a file.
return file.constructor == File;
}
function doesDirectoryExist(path)
{
var dir = Folder(path); // Returns a Folder object if the path is a folder or doesn't exist.
return dir.constructor == Folder && dir.exists;
}
That should get you what you're looking for.
It should also be noted that the File documentation does not align with how things currently work. This may be a bug (Bruce Bullis? Any insights?). According to the File constructor documentation:
File (path: string )
Core JavaScript Classes
Creates and returns a new File object referring to a given file system location.
path (optional): Data Type: string
The full or partial path name of the file, in platform-specific or URI format.
The value stored in the object is the absolute path. The file that the path refers to does not need to exist. If the path refers to an existing folder:
- The File function returns a Folder object instead of a File object.
- The new operator returns a File object for a nonexisting file with the same name.
The portion colored red is wrong (based on testing with Premiere Pro 11.1.2). Both "File(path)" and "new File(path)" always return File object instances.
It should be noted that the orange colored bullet suffers the same problem as explained above with the Folder class: even though no file exists at the path specified, the File.exists property will return true.
I hope this is helpful.
Copy link to clipboard
Copied
> Any insights?
As always, trust working code over docs.
Copy link to clipboard
Copied
Bruce Bullis wrote
> Any insights?
As always, trust working code over docs.
Of course!
As a followup question, is this a bug? It certainly seems like one given the seemingly intended symmetry of the classes.
If it's a bug, how best should one report it?
Copy link to clipboard
Copied
Here's one way to tell the difference.
var fileOrFolder = new File('/Users/bbb/Desktop/example.epr');
var type = typeof fileOrFolder;
if (fileOrFolder.commonFiles){
alert("It's a folder.");
} else {
alert("It's a file.");
}
Copy link to clipboard
Copied
Bruce Bullis wrote
Here's one way to tell the difference.
var fileOrFolder = new File('/Users/bbb/Desktop/example.epr');
var type = typeof fileOrFolder;
if (fileOrFolder.commonFiles){
alert("It's a folder.");
} else {
alert("It's a file.");
}
Wait, what? Did you test that code?
Indeed, testing in ESTK shows that no matter what you pass to the new File() function you always get "It's a file." as output. In order to make this work, you'd have to use the following code:
var fileOrFolder = Folder('/tmp'); // Use Folder to actually get the switch.
if (fileOrFolder.constructor.commonFiles) { // BAD! HARD TO UNDERSTAND. Adding "!= undefined" would be an improvement.
alert("It's a folder.");
} else {
alert("It's a file.");
}
But, honestly, it is far more clear if you do:
var fileOrFolder = Folder('/tmp');
if (fileOrFolder.constructor == Folder) { // CHECK AGAINST CONSTRUCTOR. Better. This says "I'm verifying the type of the variable."
alert("It's a folder.");
} else {
alert("It's a file.");
}
Then again, you currently claim that something is a folder if it doesn't exist as the Folder() function will still return a Folder object even if there is nothing at the specified path. This would be even better:
var fileOrFolder = Folder('/tmp');
if (fileOrFolder.constructor == Folder) {
if (fileOrFolder.exists) // CHECK THAT THE FOLDER EXISTS.
alert("It's a folder.");
else
alert("It's nothing.");
} else {
alert("It's a file.");
}
The above code works in ESTK (on a mac, at least).
This was all covered in my original answer, and in greater depth. Is there a reason that that answer simply wasn't marked as the correct one? Am I missing something about how this should work? Or is what I wrote mac-specific?
Copy link to clipboard
Copied
> Did I test that code?
Without that extraneous line you noticed, yes. Sorry, copy-paste bug. And yes, it's pretty much what you'd said.
My point was that, with zero changes on Adobe's side, it's possible to detect the result and respond accordingly.
Copy link to clipboard
Copied
Bruce Bullis wrote
> Did I test that code?
Without that extraneous line you noticed, yes. Sorry, copy-paste bug.
Heh, thought that may be the case!
That said, how thoroughly did you test your code? The use-case you presented in the code itself should work perfectly. However, based on documentation and testing, passing "/Users" into your test should also return "It's a file."
Bruce Bullis wrote
And yes, it's pretty much what you'd said.
Well, it is and it isn't. You use the new File() approach, which simply shouldn't work according to the documentation (and my own testing lines up with that).
Bruce Bullis wrote
My point was that, with zero changes on Adobe's side, it's possible to detect the result and respond accordingly.
I mean, I pointed that out in my initial response as well. Here is the working code, taken from that post:
function doesPathExist(path)
{
// File() always returns a File object instance.
// Use 'exists' to determine if there is a file system entry at the specified path.
return File(path).exists;
}
function doesFileExist(path)
{
var file = Folder(path); // Returns a File object if the path exists and is a file.
return file.constructor == File;
}
function doesDirectoryExist(path)
{
var dir = Folder(path); // Returns a Folder object if the path is a folder or doesn't exist.
return dir.constructor == Folder && dir.exists;
}
The above code works with zero changes on Adobe's side.
I simply also pointed out that Adobe does have inconsistencies/errors in the documentation and was wondering what the intent of the APIs was... 😜
Copy link to clipboard
Copied
Here's another way for this to work: - https://www.youtube.com/watch?v=Jn2tZxVVbQ0&t=8s.