FileName and Directory tags

## Writing "FileName" and "Directory" tags

By writing the FileName and/or Directory tags, ExifTool can be used to rename and/or move images into directories according to any information contained in the image. The most useful application of this feature is to organize images by date/time, but any other tag value may also be used.

Writing the Directory tag moves a file to a specified directory. The directory is created if it didn't already exist.

Writing the FileName tag renames a file. If the new FileName has a directory specification (ie. contains a '`/`' character), then the file is also moved to the specified directory (see example 6 below). Existing files will not be overwritten.

The `%d`, `%f`, `%e` file name format codes may be used to represent the directory, name and extension of the original file when specifying FileName and Directory tags via the command-line interface. (In a similar way to the `-o`, `-w` and `-tagsFromFile` options.) Also, `%c` may be used to add a copy number to the output file name to avoid collisions with existing file names. Note that these codes must be escaped with an extra `%` if used within a date format string. Modifiers may also be used to change the default behaviour of these format codes. See the `-w` option in the application documentation for details.

When organizing files by date/time, use of the `-d` (date format) option is essential to specify a format for the filename (and/or directory name). The examples below demonstrate the use of this feature. Also, a quick reference of common date format codes is included.

### Notes:

Writing the FileName and/or Directory tags alone causes the file to be renamed or moved, not copied. However, if any other tags are written at the same time, then the file is rewritten to the new destination and the original file is left unchanged. (The only exception to this is FileModifyDate tag, which will not cause the file to be rewritten because it is the only other writable tag not representing meta information within the file.) But if desired, the `-overwrite_original` option may be used in this case to remove the original copy.

Conversely, the `-o` option may be used to force exiftool to always create a copy of the file, even if no meta-information tags are written. This is true even if the `-o` option is superseded by writing the Directory tag (which may be useful if you want to derive the directory name from the values of other tags, see example 5 below).

If the `-d` option is used, the unformatted date/time value must be valid (ie. in the form "`YYYY:mm:dd HH:MM:SS`"), otherwise the date formatting will fail and the file will not be renamed or moved.

In a Windows batch file, all `%` characters must be escaped as `%%`. This can result in extreme format codes like `%%%%f` when using the `-d` option.

## Examples

1. | `exiftool -d %Y%m%d_%H%M%%-c.%%e "-filename<CreateDate" DIR`

> Rename all images in directory '`DIR`' to names like '`20060327_1058-2.jpg`', with individual file names derived from the value of the CreateDate (plus a copy number with a leading '`-`' if a file with the same name already exists), and with the same extension as the original image.

> FAQ: "Why doesn't this command rename my files?"

> There are 2 common reasons for this:

> a) When a directory name is specified, this command will only rename "writable" files in the directory. Use '`exiftool -listwf`' to list the extensions of currently writable file types. The `-ext` option may be used to rename other file types (ie. '`-ext avi`').

> b) For this command to work, the CreateDate tag must exist in the source file. Use '`exiftool -createdate FILE`' to see if a file contains this information. If it doesn't, you may need to use another date/time tag such as DateTimeOriginal or FileModifyDate.

2. | `exiftool -d %Y-%m-%d "-directory<datetimeoriginal" image.jpg`

> Move '`image.jpg`' into a directory with a name given by DateTimeOriginal, in the form '`2006-03-27`'.

3. | `exiftool '-filename<%f_$imagesize.%e' dir`

> This example uses an expression to add the image size to the name of all images in directory '`dir`'. For example, this would remame a 640x480 image called '`image.jpg`' to '`image_640x480.jpg`'. (Note that the single quotes are necessary in Unix shells due to the '`$`' symbol, but double quotes must be used instead when running in a Windows cmd shell.)

4. | `exiftool -r -directory=%e_images/%d pics`

> Recursively move all images based in directory '`pics`' to separate directory trees organized by file extension. For instance, in this example the file '`pics/toys/new_car.jpg`' is moved to '`jpg_images/pics/toys/new_car.jpg`'.

5. | `exiftool -r -o %e_images/%d pics exiftool -r -o dummy/ -directory=%e_images/%d pics`

> Both of these commands have the same effect as example 3 above except that images are copied instead of moved since the `-o` option was used. In the second command the trailing '`/`' on '`dummy/`' is necessary because otherwise '`dummy`' would be interpreted as a file name. This technique of using `-o` with a dummy directory name is necessary when the directory name is derived from the value of other tags (ie. '`-directory<createDate`') and you want the files to be copied instead of moved. (Because the values of other tags may not be used with the `-o` option.)

6. | `exiftool -r -d %Y/%m/%d/image_%H%M%S.%%e "-filename<filemodifydate" DIR`

> Recusively rename all images in '`DIR`' and any contained subdirectories to the form '`image_HHMMSS.EXT`' (where '`ext`' is the original file extension), and move them into a new directory hierarchy based on date of file modification, with path names like '`2006/03/27/image_105859.jpg`'.

The following examples demonstrate the interaction of this feature with other ExifTool options:

7. | `exiftool -filename=new.jpg dir/image.jpg`

> Rename '`dir/image.jpg`' to '`dir/new.jpg`'.

8. | `exiftool -filename=new.jpg -comment=xxx dir/image.jpg`

> Copy '`dir/image.jpg`', add a new comment, and write output to '`dir/new.jpg`'. The original file '`dir/image.jpg`' is not changed.

9. | `exiftool -filename=new.jpg -comment=xxx -overwrite_original dir/image.jpg`

> Rewrite '`dir/image.jpg`', adding a new comment and writing output to '`dir/new.jpg`'. The original file '`dir/image.jpg`' is removed.

10. | `exiftool -o tmp/ -filename=new.jpg image.jpg exiftool -o tmp/xxx.jpg -filename=new.jpg image.jpg exiftool -o tmp/new.jpg image.jpg`

> A file name or directory specified via the FileName or Directory tag takes precedence over that specified by the `-o` option, so these three commands all have the same effect: '`tmp/new.jpg`' is created without changing '`image.jpg`'. Note that in the first command, the trailing '`/`' on '`tmp/`' is necessary if the '`tmp`' directory doesn't already exist, otherwise '`tmp`' would be taken as a file name and '`new.jpg`' would be created in the current directory. As illustrated in example 4 above, the file is rewritten instead of simply being renamed when the '`-o`' option is used.

11. | `exiftool -directory=dir1 -filename=dir2/out.jpg -o dir3/ dir4/image.jpg`

> This example demonstrates the priorities of directory names specified using different techniques. The output directory is taken from the first directory specified from the following list: 1) the Directory tag, 2) the FileName tag, 3) the `-o` option, or 4) the original source file, in that order. Note that both the FileName tag and the `-o` option may be used without a directory specification, in which case the directory with the next highest priority is used. So in this example, the target file is copied (not moved, because the `-o` option was used) to '`dir1/image.jpg`'. The order of the arguments on the command line is not significant.

## Common Date Format Codes

Date format codes are used in the argument to the `-d` option to represent components of the date/time string. The codes listed below are common to most systems, but additional codes may be available on your specific system -- see your strftime man page for details.

> %a | - abbreviated locale weekday name
> %A | - full locale weekday name
> %b | - abbreviated locale month name
> %B | - full locale month name
> %c | - preferred locale date/time representation
> %d | - day of month (01-31)
> %H | - hour on a 24-hour clock (00-23)
> %I | - hour on a 12-hour clock (01-12)
> %j | - day of year (001-366)
> %m | - month number (01-12)
> %M | - minute (00-59)
> %p | - 'AM' or 'PM'
> %S | - seconds (00-59)
> %w | - weekday number (0-6)
> %W | - week number of the year (00-53)
> %x | - preferred locale date representation
> %X | - preferred locale time representation
> %y | - 2-digit year (00-99)
> %Y | - 4-digit year (ie. 2006)
> %Z | - time zone name
> %% | - a literal '%' character

Note that ExifTool file name format codes may be used inside a date format string when a date/time tag is used to set the value of the FileName or Directory tags via the command-line interface. In this case, an extra '`%`' must be added to pass the format code through the date/time parser:

> %%d | - original file directory
> %%f | - original file name
> %%e | - original file extension
> %%c | - copy number (output files only)

See the `-w` option in the exiftool application documentation for details about special features available with these name format codes.

<-- Back to ExifTool home page