Copy link to clipboard
Copied
Hi All,
I am looking for an API (CEP FS) which checks a file is exist or not. I couldn't find such inside CEP fs library. I am referring to cep.fs object.
Note: I don't want to use JSX here as that would be asynchronous.
Premiere Pro version: 11.1.0
Extension Type: Panel
Thanks & Regards,
Meet Tank
meett9325076 wrote
Here is the actual scenario which fails. Let say we want to check for file (file1.txt) existence in a folder. However, that folder neither contains a folder nor a file having the name "file1.txt". In this case isDirectory() returns false and hence isFile() returns true!!. Here isFile() should also return false as file is not there.
You are correct that isFile() will return true when the file is not there. I would say that this is a bug in the CEP code. Bruce Bullis, could you t
...Copy link to clipboard
Copied
Hi,
check this out: CC Extension SDK manual
Quote:
IsDirectory()
Reports whether an item in the file system is a file or folder.
IsDirectory(path) [window.cep.fs.IsDirectory(yourFileOrDirectory)]
RETURNS: An object with these properties:
data: An object with these properties:
— isFile: (Boolean) True if the item is a file.
— isDirectory: (Boolean) True if the item is a folder.
— mtime: (DateTime) The modification timestamp of the item.
err: The status of the operation, one of:
NO_ERROR
ERR_UNKNOWN
ERR_INVALID_PARAMS
ERR_NOT_FOUND
Does this work for you?
Copy link to clipboard
Copied
Copy link to clipboard
Copied
e.d. wrote
check this out: CC Extension SDK manual
Woah. It's too bad Adobe didn't keep that manual up-to-date. It's got such a thorough and clear explanation of what's going on with these extensions. It just needs a bit of updating...
@meett93 wrote
IsDirectory() is nativ function which can't be called from cep.fs object.
It appears that the CEP API has changed since the document e.d. referenced was last updated. I poked around in the debugger and found something that works for me. Try the following:
window.cep.fs.stat(path).data.isDirectory(); // Returns 'true' if 'path' is a directory.
window.cep.fs.stat(path).data.isFile(); // Returns 'true' if 'path' is a file.
window.cep.fs.stat(path).err != window.cep.fs.ERR_NOT_FOUND; // Returns 'true' if 'path' exists.
The key is that the stat() function returns an object with two properties: data and err. You can wrap the above checks into your own utility functions like this:
function PathExists(path)
{
return window.cep.fs.stat(path).err != window.cep.fs.ERR_NOT_FOUND;
}
function IsDirectory(path)
{
return window.cep.fs.stat(path).data.isDirectory();
}
function IsFile(path)
{
return window.cep.fs.stat(path).data.isFile();
}
I hope this helps!
Copy link to clipboard
Copied
Thanks sberic. I have checked stat() method, but unfortunately it fails in below condition:
If a directory contains both file and folder with the same name, then isDirectory() returns true and isFile() returns false. This is because isFile() returns reverse the value of isDirectory()
So we can't rely on stat() method. I would rather suggest to have two separate methods like isFileExist() and isDirectoryExists() which returns true or false. Bruce Bullis What do you think of this?
Copy link to clipboard
Copied
meett9325076 wrote
Thanks sberic . I have checked stat() method, but unfortunately it fails in below condition:
If a directory contains both file and folder with the same name, then isDirectory() returns true and isFile() returns false. This is because isFile() returns reverse the value of isDirectory()
Is this really a problem? I tested this locally on my machine (macOS - a Unix flavor) and on a Windows 10 VM. I could not create a file with the same name as a pre-existing directory, nor vice-versa. Indeed, this would break a lot of assumptions with path handling for just about every file system I'm aware of. Do you have an example where a file and directory at the same path location? Were you able to actually test this?
An alternative to the CEP options I listed would be to use the NodeJS APIs (here is the documentation for the specific version of Node used in CEP 6.1 and 7). Specifically you should look at the File System module. As you will see, it contains wrappers for most Unix file-related APIs. I am also willing to bet that it is what the cep.fs system refers to internally, making the cep.fs.stat call the same as this one (except that you wouldn't require enabling NodeJS). Either way, I can't see a way to deal with the situation you describe (although I do believe it is an impossible scenario).
Copy link to clipboard
Copied
I'm with Eric; I'm not sure how that (isDirectory = !isFile) behavior would ever become a problem...?
Copy link to clipboard
Copied
Hi sberic Bruce Bullis You are right. It is not possible to have a file and a folder having the same name as siblings. I might get confused with another scenario. I apologies. Here is the actual scenario which fails. Let say we want to check for file (file1.txt) existence in a folder. However, that folder neither contains a folder nor a file having the name "file1.txt". In this case isDirectory() returns false and hence isFile() returns true!!. Here isFile() should also return false as file is not there.
Thanks,
Meet
Copy link to clipboard
Copied
Hey Meet,
if you do it like so:
var res = window.cep.fs.stat('AbsolutePathForMyItemtoCheck');
if (res.err == 0) {
alert ("Found file: "+res.data.isFile()+" :: Found folder: "+res.data.isDirectory());
} else {
alert ("Error: "+res.err);
}
you'll get (unambiguous) data when there is a file or folder, and and error when nothing has been found, the error being either
1: ERR_UNKNOWN
2: ERR_INVALID_PARAMS
3: ERR_NOT_FOUND
Copy link to clipboard
Copied
meett9325076 wrote
Here is the actual scenario which fails. Let say we want to check for file (file1.txt) existence in a folder. However, that folder neither contains a folder nor a file having the name "file1.txt". In this case isDirectory() returns false and hence isFile() returns true!!. Here isFile() should also return false as file is not there.
You are correct that isFile() will return true when the file is not there. I would say that this is a bug in the CEP code. Bruce Bullis, could you take a look at this? It's pretty obvious when you dive into the cep.fs.stat() function what's going on - it uses a native IsDirectory() function call to tell if it's a directory and assumes it's a file otherwise... It should be trivial to access the error first and then set those values based on whether the path was even resolved.
As for you, meett9325076, this does not stop you from using the CEP functions to get what you desire. As e.d. pointed out above, you can filter based on the error returned in the stat function result. Adjusted functions might look like:
function PathExists(path)
{
return window.cep.fs.stat(path).err != window.cep.fs.ERR_NOT_FOUND;
}
function IsDirectory(path)
{
var stat = window.cep.fs.stat(path);
return stat.err == window.cep.fs.NO_ERROR && stat.data.isDirectory();
}
function IsFile(path)
{
var stat = window.cep.fs.stat(path);
return stat.err == window.cep.fs.NO_ERROR && stat.data.isFile();
}
I hope this helps.