Renaming file with CreateDate and Directory of current file

Started by gtzaskar, July 20, 2010, 04:27:43 AM

Previous topic - Next topic

gtzaskar

Hi
maybe you can give me an advice to solve my problem.
The result should be that I can rename a file over right click with "Send to" on a link to a batch file .... but first of all I try to rename the file ... here an example what should be happen:

"test.jpg" should be rename to  "2008_09_08-TestPictures-18_02_50.jpg" another result can be  "2008_09_08--18_02_50-TestPictures.jpg"

Now its clear that this "2008_09_08--18_02_50" is the CreationDate and "TestPictures" is the folder where the picture is saved. I found that this folder is stored in the TAG "Directory" on the JPG and so I want to use this folder (naturally without "\") in the file name. I hope this is possible.

With this:

"C:\Program Files (x86)\Grafik\ExifTool\exiftool" "-FileName<${CreateDate}" -d %%Y_%%m_%%d--%%H_%%M_%%S.%%%%e *.jpg

the file will be renamed to "2008_09_08--18_02_50.jpg" thats the first step, but I dont know how to add a second TAG into this line otherwise it is possible to add a second line after this in the batch like

"C:\Program Files (x86)\Grafik\ExifTool\exiftool" "-FileName<${CreateDate}" -d %%Y_%%m_%%d--%%H_%%M_%%S.%%%%e *.jpg
"C:\Program Files (x86)\Grafik\ExifTool\exiftool" "-FileName<${Directory}" *.jpg

and additionally the "Directory" contains "\" so I have to determine only all characters from the right hand side up to the character "\":

Directory = C:\Pictures\TestPictures
My value for the file name = TestPictures

Please give me an advice how to solve this problem. Many thanks in advance.

Best regards
gtzaskar
I ever look the alternative!

Phil Harvey

Hi gtzaskar,

The directory reported by ExifTool depends on what you type on the command line.

If you type exiftool ... *.jpg, then the directory will be ".".

If you type exiftool -ext jpg ... TestPictures, then the directory will be "TestPictures".

But when you click "Send to" I am guessing that the full path name of the file is placed on the command line.  In this case, I would ExifTool to report something like "C:/Pictures/TestPictures" for the directory name.

You can use a %d to represent the directory name when writing FileName.  A trailing "/" is added as you discovered.  Removing the trailing "/" is easy with %-.1d, but removing the other parent directory names is more difficult.  If all of the directory names were the same length, then we could use %-12.1d, and your batch file would look something like this:

exiftool "-filename<createdate" -d %%Y_%%m_%%d-%%%%-12.1d-%%H_%%M_%%S %*

But of course this isn't a general solution.  To do this properly can only be done with the use of user-defined tags.  I will work up a config file that will do this for you later today.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

Phil Harvey

Attached to this post is the following config file:


%Image::ExifTool::UserDefined = (
    'Image::ExifTool::Composite' => {
        MyName => {
            Require => {
                0 => 'Directory',
                1 => 'CreateDate',
            },
            # remove the extension from FileName
            ValueConv => q{
                $val[0] =~ m{([^\\/]+)[\\/]?$} or return undef;
                my $dir = $1;
                my $name = $prt[1];
                $name =~ s/DIR/$dir/g;
                return $name;
            },
        },
    },
);

1; # end


With this file in your home directory, the following command in a batch file should do what you want:

exiftool "-filename<myname" -d %%Y_%%m_%%d-DIR-%%H_%%M_%%S %*

The "MyName" tag is a composite of CreateDate with the "DIR" replaced by the last directory name.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

gtzaskar

Hi Phil,

thanks for your help ...  ;)

But it doesn´t work in my little test (see the picture). I changed only the last characters because without it no file was specified ...
How does it work with the config file? Should I change anything to make it possible to read this file for this batch or will be read the file because of the place where it was saved (beside the exiftool.exe)?

Thanks gtzaskar
Best regards
I ever look the alternative!

Phil Harvey

Hi gtzaskar,

Quote from: gtzaskar on July 23, 2010, 02:58:09 PM
But it doesn´t work in my little test (see the picture). I changed only the last characters because without it no file was specified ...

I can't test this right now, but I thought that '%*' in a batch file passed on all arguments which will exist if you drag and drop files and/or folders onto the .BAT file.  If not try '%1' instead, but this will only work for a single file.  But maybe you didn't want to do this by drag and drop, in which case this isn't what you want.

Quote
How does it work with the config file? Should I change anything to make it possible to read this file for this batch or will be read the file because of the place where it was saved (beside the exiftool.exe)?

See the config file documentation for more details, but it should work if it is named ".ExifTool_config" and placed in the proper directory.  If it doesn't work, see FAQ 11 for some help.  Also, the images must contain the CreateDate information for this to work.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

gtzaskar

Hi Phil,

many, many thanks for your help - now it works fine. The problem was, that some how the "." in front  of the config file was deleted. I renamed it and it works after I added in the line in the batch file the file extension.
Now the batch looks like:

"C:\Program Files (x86)\Grafik\ExifTool\exiftool.exe" "-filename<myname" -d %%Y_%%m_%%d-DIR-%%H_%%M_%%S.jpg %1

for Drag&Drop SingleFiles and

"C:\Program Files (x86)\Grafik\ExifTool\exiftool.exe" "-filename<myname" -d %%Y_%%m_%%d-DIR-%%H_%%M_%%S.jpg %*

for Drag&Drop MultiFiles

Wow -- great tool and thanks again to you.

gtzaskar
I ever look the alternative!