Java 8+ version:
import java.nio.file.Paths;
import java.nio.file.Files;
Files.createDirectories(Paths.get("/Your/Path/Here"));
The Files.createDirectories() creates a new directory and parent directories that do not exist. This method does not throw an exception if the directory already exists.
file.mkdirs() not creating a directory
autocmd - How do I save a file in a directory that does not yet exist? - Vi and Vim Stack Exchange
How to create directory, if not exists
Create directory if it does not exist
Take a look at python's os library. They have useful functions in manipulating files and folders.
I would look specifically at os.path.isdir(). This will allow your computer to detect if the path you pass into the method exists and is a directory. To create folders you can call os.mkdir(). I also suggest using full paths if you run into any trouble with path names.
Videos
I've been trying to write a simple program that writes some text into a text file, but I haven't been able to create a directory for the files. This is what I have so far:
import java.io.*;
public class WritingTest3
{
public static void main(String[] args)
{
File f = null;
boolean bool = false;
try {
f = new File("C:/Users/Me/Desktop/java/testing.txt");
System.out.println(f.exists());
bool = f.mkdirs();
System.out.print("Directory created?" + bool);
} catch(Exception e) {
e.printStackTrace();
}
}
}
For checking whether the file exists, it is returning true, but when creating the directory it returns false. Thank you in advance.
As far as I know there is no setting or some such to do this. But not all is lost, we can of course use the BufWritePre autocommand.
This is executed before the buffer is written to the disk. So we can create the directory there if it doesn't exist yet.
For example:
augroup Mkdir
autocmd!
autocmd BufWritePre * call mkdir(expand("<afile>:p:h"), "p")
augroup END
<afile>refers to the file we're trying to save;:pis a modifier to expand it to the full pathname (rather than relative), and:hremoves the last path component (the file).- We then call
mkdir()if required. We need thepflag formkdir()to make all parents directories (i.e. in the case ofnonexistent/more_nonexisting/file), which also ensures it won't error out if the directory already exists.
You could, of course, also run the mkdir() command from the Vim commandline, or bind it to a keybind, ie:
nnoremap <Leader>m :call mkdir(expand("%:p:h"), "p")<CR>
Here I used % instead of <afile>, since that's only valid from within an autocommand (% refers to the currently active buffer, which would not work with :wa for example; <afile> refers to the filename of the buffer that triggers the autocmd).
You can also ask for a confirmation before writing a directory if you want. See this question for more details: How can I stop Vim from writing a file in BufWritePre autocommand?
The above snippet will create the directory on the first write (:w). You could, if you wanted, also create the directory when you first open it (i.e. just after typing vim ...) by using the BufNewFile autocmd instead of BufWritePre.
There is also a plugin called auto_mkdir which is effectively the same a the above.
On this page there is a slightly expanded snippet which also asks you if you want to create the directory first, which some may consider to be useful. It also has converts the filename of the encoding before writing it:
call mkdir(iconv(expand("%:p:h"), &encoding, &termencoding), 'p')
I'm not sure if this is actually required though, but if you mix encodings a lot and get weird filenames, you could try it.
I put all of the above in an auto_mkdir2.vim plugin for easier installation.
Another way with a vanilla Vim (without extra conf or plugins). in Vim:
:!mkdir -p /folder/you/want/
:w #save file
or
$ vim /folder/you/want/myfile.conf
$ ctrl+z # switch to the terminal then create the folders
$ mkdir -p /folder/you/want/
$ fg # return to Vim
$ :w #save file
Couple of quick thoughts:
- Add in a mkdir -p $(dirname FILE)
- Could you use a tar pipe to sort it all out?
Edit
tar -cf archive.tar `svn status -q | awk '{print $2}'`
Should create an archive of the modified files. Then:
tar -xf archive.tar -C /tmp/xen
should put it where you want. If it needs to be in one step, that's possible too.
rsync makes the pretty simple, it the tool I generally use for copying entire directory structures:
rsync -av /home/source/svn/repo /home/dest/svn/
This will create the repo directory and all it contents in the /home/dest/svn directory, so you will have /home/dest/svn/repo/...
So for you example:
rsync -rv ./ /tmp/xen/
-a, would preserve time stamps and be recursive among other things, 'man rsync'.
Update after realizing I read the question wrong:
To mimic the directory structure (tar alternative):
rsync -av --include='*/' --exclude='*' source destination
And then to copy the files before say 20 minutes ago:
find . -type f -mmin -20 -print0 | xargs -0 cp --target-directory /mydestdir/