Renaming and moving a file from a plugin

Hi,

I'd like to rename and move a file inside a plugin I'm developing. I was thinking of using the methods available on the VirtualFile ap instead of refactoring. Renaming a file is pretty straight forward, but moving seems kind of complicated. I have a path and a file name, and the new path may not yet exist. Do I have to write code to check to see if all the intermediate directories exist before moving? I was thinking of using FilenameIndex.getFilesByName to test to see if a directory exists, if not move up until I get one that does and then create the missing intermediate directories, and then once I have the final directory I need to move to, use the move method on VirtualFile. Is that the way to do it? Would be nice if one could just give a full path to rename and it both moved the file and renamed it and created any missing intermediate directories. I don't think that exists though.

2 comments
Comment actions Permalink

You can use VfsUtil.createDirectories() to create all non-existing directories in a path given as a string.

Note that using FilenameIndex.getFilesByName() is an extremely inefficient way to check if a file exists. A regular File.exists() is much, much better.

0
Comment actions Permalink

That worked great. I actually first tried Java's File .mkdirs and VirtualFileManager.syncRefresh but that only worked sometimes. It would always make the dirs, but not in time to get references to them even with sync. Thanks for the tip, super helpful!

0

Please sign in to leave a comment.