Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ant: Create directory containing file if it doesn't already exist?

Tags:

Basically, I get a path like "C:\test\subfolder1\subfolder2\subfolder3\myfile.txt", but it's possible that subfolders 1-3 don't exist already, which means I'd get an exception if I try to write to the file.

Is there a way to create the directory structure the target file is in, either by using some task that creates the structure when it outputs to the file and then deleting the file, or by parsing the directory part of the path and using the mkdir task first?

like image 377
Benny Avatar asked Jan 08 '10 22:01

Benny


People also ask

How do you create a directory if it does not exist?

When you want to create a directory in a path that does not exist then an error message also display to inform the user. If you want to create the directory in any non-exist path or omit the default error message then you have to use '-p' option with 'mkdir' command.

What does mkdir return if directory already exists?

Mkdir , it does not return an error of any if the directories in the path already exists.

How do you create a directory if it doesn't exist in Python?

To create a directory if not exist in Python, check if it already exists using the os. path. exists() method, and then you can create it using the os. makedirs() method.

What is Basedir in Ant?

The 'basedir' is the base directory from which any relative directories used within the Ant build file are referenced from. If this is omitted the parent directory of the build file will be used.


1 Answers

Ant will create the full tree of directories for you when you use the <mkdir> task. So you just need to use the <dirname> task to get the directory name from the file name.

 <dirname property="directoryProperty" file="${filePathProperty}"/>  <mkdir dir="${directoryProperty}" /> 

The first line extracts the directory portion of your file path and stores it in the directoryProperty property. The second line creates the directory (and any parent directories that don't exist).

like image 197
Dan Dyer Avatar answered Sep 27 '22 20:09

Dan Dyer