jEdit Community - Resources for users of the jEdit Text Editor
jEdit 4.2 question: How to tell if a file exists on a VFS using the FTP Plugin?
Submitted by txporter on Thursday, 25 May, 2006 - 03:35
I use the FTP plugin to give me remote access to files. I am trying to write a macro to look in the directory of the file I have open in a buffer and determine if another file exists in the same directory.

On a local file system I would simply create a File instance using a path string I constructed and check to see if it exists, like this:

dirname = dir(buffer.getPath());
FQPN = dirname + "new file name";
java.io.File newFile = new java.io.File(FQPN);
if (newFile.exists()) {
//do stuff
}


If I try this when my buffer is on my remote server, newFile.exists() always returns false, regardless of whether the file actually exists or not. I think this is because dirname is something like "ftp://myuserid@myremotehost:21/path/to/new_file_name" and the normal java.io.File.exists() method does not understand URI's like this.

Any ideas on how to tell if the remote file exists or not?

I tried unconditionally opening the file and checking the length of the buffer, expecting a new file to have zero length and then I could close it immediately, but since jEdit.openFile(view, pathToFile) does not seem to be a blocking operation, trying to get the buffer length did not work:


1. aBuffer = jEdit.openFile(view,FQPN);
2. if (aBuffer.getLength() == 0) {
3. // file is new, so close it, as I only want new buffer if file exists
4. }


Could I put a VFSManager.waitForRequests() call between line 1 & 2 to wait for the FTP I/O to complete before checking the buffer length? Is this a static call? If not how to I get access to the VFSManager instance associated with my FTP connection?

Thanks!!

Tom Porter
Comment viewing options
Select your preferred way to display the comments and click 'Save settings' to activate your changes.
remoteFileExists routine, works for FTP VFS type
by txporter on Thu, 25/05/2006 - 17:40
With a bit of poking around in the VFS API's I came up with a remoteFileExists() method to use instead of java.io.File.exists()
boolean remoteFileExists(String dirpath, String filename) {
  vfs = VFSManager.getVFSForProtocol("ftp");
  //vfs.reloadDirectory(dirpath);
  VFSManager.waitForRequests();
  session = vfs.createVFSSession(dirpath,view);
  de = vfs._getDirectoryEntry(session,dirpath + filename,view);
  if (de.name != null) {
    vfs._endVFSSession(session,view);
    return true;
  } else {
    vfs._endVFSSession(session,view);
    return false;
  }
}

In jEdit4.2 VFS.getDirectoryEntry is supposed to return null if the entry does not exist, but when I used:
if (de != null) {
...
}
in my test, it did not work right, always returned true. Hope this helps if any one else has this kind of problem. Tom P.
Got part of the solution, need a bit more help
by txporter on Thu, 25/05/2006 - 13:20
The following code works, but the IOException raised by the FTP VFS when the file does not exist causes a message box to appear.
aBuffer = jEdit.openFile(view, "ftp://me@myhost:21/path/to/file/FOO.CBL");
VFSManager.waitForRequests();
foo = aBuffer.getLength();
if (foo == 0) {
  jEdit.closeBuffer(view, aBuffer);
  Macros.message(view,"File does not exist");
}
Is there any way to intercept (try/catch block?) the FTPException or IOException inside my beanshell code? it seems that I cannot catch it because it occurs in the jEdit.openFile() call. Are there any property settings I can set to suppress the exception error message box? Thanks, Tom P.
User login
Browse archives
« August 2024  
MoTuWeThFrSaSu
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 
Poll
Are you interested in language packs for jEdit?
Yes, and I could help maintain translations
26%
Yes, I'd like to have translations
32%
Indifferent
35%
No, that'd be bad (please comment)
7%
Total votes: 1093
Syndication
file   ver   dls
German Localization light   4.4.2.1   95567
Context Free Art (*.cfdg)   0.31   46060
BBEdit scheme   1.0   18599
JBuilder scheme   .001   18500
ColdFusion scheme   1.0   18029
R Edit Mode - extensive version   0.1   17478
Advanced HTML edit mode   1.0   16211
Matlab Edit Mode   1.0   16073
jEdit XP icons   1.0   15234
XP icons for jEdit   1.1   14298