CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Parsing data from TXT file into XMP format with ExifTool

 



teddoff
Novice

May 9, 2015, 8:33 PM

Post #1 of 38 (16644 views)
Parsing data from TXT file into XMP format with ExifTool Can't Post

I recently got in contact with Phil Harvey who made ExifTool, this tool specifically deals with metadata formats like JPEG's IPTC and others. For me I am looking to streamline my workflow that presently has me copying text from a TXT file into a photo editing application. Instead I would like to harness its built-in ability to import corresponding XMP side-car data files to automatically do this on import and save me the time and RSI!

Presently three sets of data exist in a text file:


Code
Title: 
You've Been Peeling An Orange Wrong - Tablespoon

URL:
http://www.tablespoon.com/posts/youve-been-peeling-an-orange-wrong/14800c66-27f2-4715-9936-d299ec7bfa9f?utm_source=zergnet.com&utm_medium=referral&utm_campaign=zergnet_280460

Comment:
This technique works best with mandarin oranges, but just for good measure I tried it out on a Valencia orange as well. Simply lop off the ends, make a small incision in the side and carefully open to reveal easy-to-eat orange segments. (Or as I prefer to call it, an orange caterpillar.)



Outputting the data in XMP structures it like thus:


Code
<?xpacket begin='' id=''?> 
<x:xmpmeta xmlns:x='adobe:ns:meta/' x:xmptk='XMP toolkit 2.9-9, framework 1.6'>
<rdf:RDF xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#' xmlns:iX='http://ns.adobe.com/iX/1.0/'>
<rdf:Description rdf:about='' xmlns:Iptc4xmpCore='http://iptc.org/std/Iptc4xmpCore/1.0/xmlns/'>
<Iptc4xmpCore:SubjectReference>http://www.tablespoon.com/posts/youve-been-peeling-an-orange-wrong/14800c66-27f2-4715-9936-d299ec7bfa9f?utm_source=zergnet.com&amp;utm_medium=referral&amp;utm_campaign=zergnet_280460</Iptc4xmpCore:SubjectReference>
<Iptc4xmpCore:SubjectCode>
<rdf:Bag>
<rdf:li>http://www.tablespoon.com/posts/youve-been-peeling-an-orange-wrong/14800c66-27f2-4715-9936-d299ec7bfa9f?utm_source=zergnet.com&amp;utm_medium=referral&amp;utm_campaign=zergnet_280460</rdf:li>
</rdf:Bag>
</Iptc4xmpCore:SubjectCode>
</rdf:Description>
<rdf:Description rdf:about='' xmlns:photoshop='http://ns.adobe.com/photoshop/1.0/'>
<photoshop:Headline>You&apos;ve Been Peeling An Orange Wrong - Tablespoon</photoshop:Headline>
</rdf:Description>
<rdf:Description rdf:about='' xmlns:dc='http://purl.org/dc/elements/1.1/'>
<dc:description><rdf:Alt><rdf:li xml:lang='x-default'>This technique works best with mandarin oranges, but just for good measure I tried it out on a Valencia orange as well. Simply lop off the ends, make a small incision in the side and carefully open to reveal easy-to-eat orange segments. (Or as I prefer to call it, an orange caterpillar.)</rdf:li></rdf:Alt></dc:description>
</rdf:Description>
<rdf:Description rdf:about='' xmlns:photomechanic='http://ns.camerabits.com/photomechanic/1.0/'>
</rdf:Description>
<rdf:Description rdf:about='' xmlns:xap='http://ns.adobe.com/xap/1.0/'>
<xap:Rating>0</xap:Rating>
</rdf:Description>
</rdf:RDF>
</x:xmpmeta>
<?xpacket end='w'?>



My requirements are simple, I need to convert the existing TXT files into the structured format of the XMP format, however there is a variable in that sometimes a "Comment" is not present, only the "Title" and "URL" values. I am looking to translate the following into IPTC fields: "SubjectReference", "Headline" and "Description", all as seen in the example above.


According to Mr. Harvey I would need to parse the TXT file in Perl, and call "SetNewValue()" with appropriate tag/value pairs, then call "WriteInfo()"
, an example from his website:


Code
use Image::ExifTool; 
my $exifTool = new Image::ExifTool;
$exifTool->SetNewValue(Author => 'Phil Harvey');
$exifTool->WriteInfo('image.jpg','modified_image.jpg');


ref: http://owl.phy.queensu.ca/~phil/exiftool/ExifTool.html


However this seems like it would always set the "Author" to a fixed value, instead I would need "Phil Harvey" to be the text after "Title" for example, but only up until the next value to look out for e.g. "URL".

This of which I do not know how to do, can anybody advise please.


Laurent_R
Veteran / Moderator

May 10, 2015, 3:07 AM

Post #2 of 38 (16607 views)
Re: [teddoff] Parsing data from TXT file into XMP format with ExifTool [In reply to] Can't Post

Maybe I don't understand you correctly, but I assume that, somewhere in your script you are already reading the input exif data. Then you can just take the first line after the "Title:" tag and then set that as the author.


teddoff
Novice

May 10, 2015, 7:17 AM

Post #3 of 38 (16589 views)
Re: [Laurent_R] Parsing data from TXT file into XMP format with ExifTool [In reply to] Can't Post

No, what I am after is converting the TXT files data into XMP ones. But the data from "Title" would go into the IPTC:Headline field and so on.


Laurent_R
Veteran / Moderator

May 10, 2015, 11:37 PM

Post #4 of 38 (16471 views)
Re: [teddoff] Parsing data from TXT file into XMP format with ExifTool [In reply to] Can't Post

Alright, then you probably have to read the exif data, find the line immediately after the title tag and correct the XML.


teddoff
Novice

May 11, 2015, 9:43 PM

Post #5 of 38 (16453 views)
Re: [Laurent_R] Parsing data from TXT file into XMP format with ExifTool [In reply to] Can't Post

Not being a coder of any sort, I actually don't know how to do that. But thanks for the info, will look into it..


Laurent_R
Veteran / Moderator

May 11, 2015, 11:14 PM

Post #6 of 38 (16451 views)
Re: [teddoff] Parsing data from TXT file into XMP format with ExifTool [In reply to] Can't Post

Well, show the code that you have presently, we should be able to tell you what to add.


teddoff
Novice

May 12, 2015, 6:10 AM

Post #7 of 38 (16446 views)
Re: [Laurent_R] Parsing data from TXT file into XMP format with ExifTool [In reply to] Can't Post

I don't actually have any. I was first and foremost trying to get information about what would need to be done.

I actually can't code.


Laurent_R
Veteran / Moderator

May 12, 2015, 10:17 AM

Post #8 of 38 (16441 views)
Re: [teddoff] Parsing data from TXT file into XMP format with ExifTool [In reply to] Can't Post

But you ran some script to know what the ExifTool module is producing, didn't you? If so, show what you ran, even if it is far from what you ultimately need.


teddoff
Novice

May 13, 2015, 7:35 PM

Post #9 of 38 (16424 views)
Re: [Laurent_R] Parsing data from TXT file into XMP format with ExifTool [In reply to] Can't Post

No, the XMP an export from the photo management application I am using, the other file is just a text file.


teddoff
Novice

May 14, 2015, 4:20 PM

Post #10 of 38 (16410 views)
Re: [teddoff] Parsing data from TXT file into XMP format with ExifTool [In reply to] Can't Post

The examples on the ExifTool site are very rigid, while I am looking for something which is more delicate (hence why I'd like to move away from an unstructured TXT file and to XMP) and believe the way to achieve this is to get Perl to look for next beginning on a new line after "Title:" and up until it comes across "URL:" on it's own line.

So the line "SetNewValue" option would be a little more involved.


Zhris
Enthusiast

May 14, 2015, 7:41 PM

Post #11 of 38 (16403 views)
Re: [teddoff] Parsing data from TXT file into XMP format with ExifTool [In reply to] Can't Post

Hi,

If I have understood correctly, your first step is to parse the txt file. It is well structured, in that it follows the pattern key \n value \n linebreak, therefore can be parsed with ease. There are numerous ways to achieve this, here is an example of parsing into a hash:


Code
use strict; 
use warnings;
use Data::Dumper;

my %hash = map s/[\s:]*$//r, grep /\S+/, <DATA>;

print Dumper \%hash;

__DATA__

Title:
You've Been Peeling An Orange Wrong - Tablespoon

URL:
http://www.tablespoon.com/posts/youve-been-peeling-an-orange-wrong/14800c66-27f2-4715-9936-d299ec7bfa9f?utm_source=zergnet.com&utm_medium=referral&utm_campaign=zergnet_280460

Comment:
This technique works best with mandarin oranges, but just for good measure I tried it out on a Valencia orange as well. Simply lop off the ends, make a small incision in the side and carefully open to reveal easy-to-eat orange segments. (Or as I prefer to call it, an orange caterpillar.)


Output:


Code
$VAR1 = {                                                                                                                                                         
'Comment' => 'This technique works best with mandarin oranges, but just for good measure I tried it out on a Valencia orange as well. Simply lop off the ends, make a small incision in the side and carefully open to reveal easy-to-eat orange segments. (Or as I prefer to call it, an orange caterpillar.)',
'URL' => 'http://www.tablespoon.com/posts/youve-been-peeling-an-orange-wrong/14800c66-27f2-4715-9936-d299ec7bfa9f?utm_source=zergnet.com&utm_medium=referral&utm_campaign=zergnet_280460',
'Title' => 'You\'ve Been Peeling An Orange Wrong - Tablespoon'
};


CHris


(This post was edited by Zhris on May 14, 2015, 7:44 PM)


teddoff
Novice

May 14, 2015, 9:26 PM

Post #12 of 38 (16391 views)
Re: [Zhris] Parsing data from TXT file into XMP format with ExifTool [In reply to] Can't Post

Great, thanks, it's very good of you to take the time to do this. I saved the code as an ".args" file, executing incorrectly it seems: "exiftool DIR exifgif.args" which produced an error: "Unknown file type" but it read one file, there was only one .txt image in the folder anyway.

I am looking to batch files in a directory also, everything in one pass.

Oh dear, not even able to figure out how to execute ExifTool!


Zhris
Enthusiast

May 15, 2015, 12:33 PM

Post #13 of 38 (16371 views)
Re: [teddoff] Parsing data from TXT file into XMP format with ExifTool [In reply to] Can't Post

Hi,

I don't know enough about exiftools but from briefly reading the documentation I don't think an .args file is used in the manner you tried. It appears you are trying to use the command line interface, I am following on from the advice you were provided by Mr. Harvey, write a custom Perl script that uses Image::ExifTool.


Quote
According to Mr. Harvey I would need to parse the TXT file in Perl, and call "SetNewValue()" with appropriate tag/value pairs, then call "WriteInfo()"
, an example from his website:

Code

use Image::ExifTool;
my $exifTool = new Image::ExifTool;
$exifTool->SetNewValue(Author => 'Phil Harvey');
$exifTool->WriteInfo('image.jpg','modified_image.jpg');


ref: http://owl.phy.queensu.ca/~phil/exiftool/ExifTool.html


However this seems like it would always set the "Author" to a fixed value, instead I would need "Phil Harvey" to be the text after "Title" for example, but only up until the next value to look out for e.g. "URL".


Once you have parsed a txt file into a hash, you can use the code you were provided to set new values then write info:


Code
use strict;  
use warnings;
use Image::ExifTool;

my $filepath = '/path/to/file.txt';
open my $filehandle, '<', $filepath or die "cannot open '$filepath': $!";
my %hash = map s/[\s:]*$//r, grep /\S+/, <$filehandle>;
close $filehandle;

my $exifTool = new Image::ExifTool;
$exifTool->SetNewValue(Author => $hash{Title});
$exifTool->WriteInfo('image.jpg','modified_image.jpg'); # 3rd arg should be 'XMP' if you want to create an XMP data file.


With regards to batches, we don't know enough about your file system structure i.e. how txt files map to image files, therefore cannot provide complete advice / code. In unix I would probably make use of pathname expansion and run the command perl script.pl /path/to/dir/*, then @ARGV will contain all the batch filepaths, which can be iterated over. This approach gives you more flexibility over each batch, rather than having to code this flexibility in yourself:


Code
for my $filepath ( @ARGV ) 
{
...
}


Chris


(This post was edited by Zhris on May 15, 2015, 1:09 PM)


teddoff
Novice

May 15, 2015, 5:58 PM

Post #14 of 38 (16359 views)
Re: [Zhris] Parsing data from TXT file into XMP format with ExifTool [In reply to] Can't Post

Thank you again for your efforts. As I interpret from your code "my $filepath = '/path/to/file.txt';" points to a specific file. Mentioned at the end of your post was my workflow, I am on OS X and the files I get are paired with TXT files, so a file called "abc.gif" (of which that format cannot store IPTC metadata) is accompanied by a text file with the name "abc.gif.txt" which makes it really easy to associate the metadata with the correlating image file.

Typically I'll have a folder of 20 or so files, so 40 then, and wanted to just type a command into Terminal (OS X) and then have ExifTool spit out XMP files with the data contained in the TXT file reorganised so when I import the image files (renamed as e.g. "abc.gif.xmp") the data will simply import at the same time.

It's a unique situation I know, not many people are going to be in this situation, but that's my present workflow.



Code
$exifTool->SetNewValue(Author => $hash{Title});   
$exifTool->WriteInfo('image.jpg','modified_image.jpg'); # 3rd arg should be 'XMP' if you want to create an XMP data file.


I think that your code might apply this one example I have given to many files, rather than to interpret each file differently, although I can't be sure?

I have a link to a small group of samples you will see what I mean, workflow wise: http://cl.ly/2c1I0I432B03

I was confused about the command you offered: "perl script.pl /path/to/dir/*" as I thought the code was for the ARGV file and not a perl script, even though this is a perl forum. Why would the ARGV file contain file paths, surely I could point at a directory and just have it convert everything in there.

Apologies if I am confusing the situation further.


(This post was edited by teddoff on May 15, 2015, 6:01 PM)


Zhris
Enthusiast

May 15, 2015, 7:10 PM

Post #15 of 38 (16352 views)
Re: [teddoff] Parsing data from TXT file into XMP format with ExifTool [In reply to] Can't Post

Hi,

Do you have Perl installed on your system and have ran a Perl script before?

Chris


teddoff
Novice

May 15, 2015, 9:03 PM

Post #16 of 38 (16347 views)
Re: [Zhris] Parsing data from TXT file into XMP format with ExifTool [In reply to] Can't Post

No, I'll have to look into it.


teddoff
Novice

May 16, 2015, 5:43 AM

Post #17 of 38 (16338 views)
Re: [teddoff] Parsing data from TXT file into XMP format with ExifTool [In reply to] Can't Post

I just thought I could feed the Perl script into ExifTool, I didn't know it was going to encompass a new discipline.


Zhris
Enthusiast

May 16, 2015, 11:23 AM

Post #18 of 38 (16328 views)
Re: [teddoff] Parsing data from TXT file into XMP format with ExifTool [In reply to] Can't Post

Hi,

As far as I can tell the command line interface allows Perl expressions to be applied to values but your requirements are more comprehensive. Can you paste the contents of one of the .args files that comes with the distribution, I want to confirm they don't contain Perl code. I believe os x comes with Perl already installed, if you run the command perl -v and it returns a version, this will confirm that.

Chris


teddoff
Novice

May 16, 2015, 12:44 PM

Post #19 of 38 (16326 views)
Re: [Zhris] Parsing data from TXT file into XMP format with ExifTool [In reply to] Can't Post

It does: "This is perl, v5.8.9 built for darwin-2level"

ARGS example:


Code
# 
# File: xmp2iptc.args
#
# Description: Tag name translations for converting from XMP to IPTC
#
# Usage: exiftool -tagsFromFile src.jpg -@ xmp2iptc.args dst.jpg
#
# Author: 2005/05/14 - P. Harvey
#
# References: http://www.iptc.org/IPTC4XMP/
# http://www.iptc.org/IIM/
# http://www.adobe.com/products/xmp/pdfs/xmpspec.pdf
#
# Notes: The following tags have the same names in both IPTC and XMP,
# so they are covered by the first argument: Category, City,
# Credit, DateCreated, Headline, Source, SupplementalCategories
# and Urgency.
#
-XMP:* > IPTC:*
-XMP-dc:Title > IPTC:Headline
-EXIF:XPTitle > IPTC:Headline
# end



(This post was edited by teddoff on May 16, 2015, 1:04 PM)


Zhris
Enthusiast

May 16, 2015, 1:34 PM

Post #20 of 38 (16320 views)
Re: [teddoff] Parsing data from TXT file into XMP format with ExifTool [In reply to] Can't Post

Hi,

Thanks for the .args example, looks like some kind of mapping schema, I doubt raw Perl code is acceptable as suspected.

Now you can check if you have the Image::ExifTool module installed:


Code
perl -MImage::ExifTool -e ';'


If its not installed I would recommend installing cpan minus to your home first since os x uses Perl internally and it may be risky to mess with it. This suggests you install using the following commands:


Code
fetch -o- http://cpanmin.us | perl - -l ~/perl5 App::cpanminus local::lib 
eval `perl -I ~/perl5/lib/perl5 -Mlocal::lib`
echo 'eval `perl -I ~/perl5/lib/perl5 -Mlocal::lib`' >> ~/.profile


Then you can install Image::ExifTool using:


Code
cpanm Image::ExifTool


Now you can harness the full power of exiftool via Perl. I suggest you take a look at a beginners Perl tutorial, but you can jump straight in and play around with the scripts you have been provided. I wouldn't worry about batches for now.

Chris


Zhris
Enthusiast

May 16, 2015, 3:20 PM

Post #21 of 38 (16315 views)
Re: [teddoff] Parsing data from TXT file into XMP format with ExifTool [In reply to] Can't Post

Hi,

Had some time to throw together a rough demo. It searches for all gifs in the same folder as the script, uses these to construct the txt and xmp filepaths, parses the txt files, then generates the xmp files.

http://demo.massweb.co.uk/image/exiftool/exiftool.pl

Which loosely uses the following code (I used a slightly modified version to work in browser):


Code
use strict;   
use warnings;
use Image::ExifTool;

for my $filepath_gif ( <*.gif> )
{
my $filepath_txt = $filepath_gif . '.txt';
my $filepath_xmp = $filepath_gif . '.xmp';

open my $filehandle_txt, '<', $filepath_txt or die "cannot open '$filepath_txt': $!";
my %hash = map { my$s=$_;$s=~s/[\s:]*$//;$s } grep /\S+/, <$filehandle_txt>;
close $filehandle_txt;

my $exifTool = new Image::ExifTool;
$exifTool->SetNewValue(Author => $hash{Title}) or die;
$exifTool->WriteInfo( undef, $filepath_xmp, 'XMP' );
}


Chris


teddoff
Novice

May 17, 2015, 11:13 AM

Post #22 of 38 (16289 views)
Re: [Zhris] Parsing data from TXT file into XMP format with ExifTool [In reply to] Can't Post

This is all quite new to me, I have been going over various instructions and believe I understand most of it, but I cannot get cpan to install. I went to the link you referenced to and noticed that I could install Perlbrew and it would contain cpan but was not sure if the instructions on the site would work for me as it's not a good idea to install on the root:

http://perlbrew.pl

I didn't see the "perl - -l ~/perl5" command on the install instructions for Perlbrew ("\curl -L http://install.perlbrew.pl | bash") so didn't want to risk it, I just can't re-install OS X on my main machine, it's just not an option right now.

The command you had from another site produced the following: "-bash: fetch: command not found", it's not working for me.

Not really sure how to proceed, would like to know if Perlbrew is the way to go?

I don't know what it is about these command line installs but they never work first time, ever. I really what a difficult process ExifTool was to install, it was so difficult.


Also, the following command "perl -MImage::ExifTool -e ';'" produces the following error:


Code
Can't locate Image/ExifTool.pm in @INC (@INC contains: /opt/local/lib/perl5/site_perl/5.8.9/darwin-2level /opt/local/lib/perl5/site_perl/5.8.9 /opt/local/lib/perl5/site_perl /opt/local/lib/perl5/vendor_perl/5.8.9/darwin-2level /opt/local/lib/perl5/vendor_perl/5.8.9 /opt/local/lib/perl5/vendor_perl /opt/local/lib/perl5/5.8.9/darwin-2level /opt/local/lib/perl5/5.8.9 .). 
BEGIN failed--compilation aborted.


Any advice please?


(This post was edited by teddoff on May 17, 2015, 11:16 AM)


Zhris
Enthusiast

May 18, 2015, 7:30 AM

Post #23 of 38 (16267 views)
Re: [teddoff] Parsing data from TXT file into XMP format with ExifTool [In reply to] Can't Post

Hi,

You already have Perl installed therefore installing Perlbrew isn't necessary. Perlbrew is useful when you want to manage multiple Perl installations and don't have root access. Using your systems Perl shouldn't be a problem.

Try replacing the fetch command with a curl one as per App::cpanminus. You should have curl installed:


Code
curl -L https://cpanmin.us | perl - -l ~/perl5 App::cpanminus local::lib


Chris


teddoff
Novice

May 18, 2015, 5:46 PM

Post #24 of 38 (16255 views)
Re: [Zhris] Parsing data from TXT file into XMP format with ExifTool [In reply to] Can't Post

I read not to install via the "curl" method, I recognise that the "perl - -l ~/perl5 App::cpanminus local::lib" command is present, which I think installs on the users Home, but not sure about using curl - I don't want to mess my system up.


Zhris
Enthusiast

May 18, 2015, 7:03 PM

Post #25 of 38 (16251 views)
Re: [teddoff] Parsing data from TXT file into XMP format with ExifTool [In reply to] Can't Post

Hi,

Alright, my final suggestion is to install cpanm under your system Perl:


Code
cpan App::cpanminus


Or just install Image::ExifTool directly:


Code
cpan Image::ExifTool


I don't know enough about OS X to provide further advice on installing a program that retrieves content from the web like wget, fetch or curl. I'm also assuming you don't already have App::cpanminus installed.

Chris


(This post was edited by Zhris on May 18, 2015, 7:05 PM)


teddoff
Novice

May 19, 2015, 5:53 AM

Post #26 of 38 (8091 views)
Re: [Zhris] Parsing data from TXT file into XMP format with ExifTool [In reply to] Can't Post

I know it's tedious, sorry, but let me get back to you when I have it working.

Thanks


Laurent_R
Veteran / Moderator

Jun 22, 2016, 10:25 AM

Post #27 of 38 (7409 views)
Re: [teddoff] Parsing data from TXT file into XMP format with ExifTool [In reply to] Can't Post

Test post


garry664
Novice

Jun 22, 2016, 5:56 PM

Post #28 of 38 (7398 views)
Re: [Laurent_R] Parsing data from TXT file into XMP format with ExifTool [In reply to] Can't Post

I finally got Perl installed and working with ExifTool, sorry for the delay, I had a lot of things on my plate!

Using the code from Chris I found an error on line 10 every time I ran the script:


Code
    open my $filehandle_txt, '<', $filepath_txt or die "cannot open '$filepath_txt': $!";



Even with the script in the same folder as the txt files I get this. However, as the format of the image and accompanying text files means that the file image format is in the same name as the text file, I found the script got confused, so only when I removed that first instance did it even read the file:

Image file: "123.gif"
Sidecar text file: "123.gif.txt"

*this is so that the files stay next to the image file they belong to*


So it won't process the files in the folder, what exactly is going wrong please?


(This post was edited by garry664 on Jun 22, 2016, 6:09 PM)


Laurent_R
Veteran / Moderator

Jun 22, 2016, 11:20 PM

Post #29 of 38 (7389 views)
Re: [garry664] Parsing data from TXT file into XMP format with ExifTool [In reply to] Can't Post

Please provide the exact code that you are running and the exact error message that you get, as well as warnings if any.


garry664
Novice

Jun 23, 2016, 12:33 AM

Post #30 of 38 (7388 views)
Re: [Laurent_R] Parsing data from TXT file into XMP format with ExifTool [In reply to] Can't Post

This code was originally provided by Zhris:


Code
use strict; 
use warnings;
require Image::ExifTool;

for my $filepath_gif ( <*.gif> )
{
my $filepath_txt = $filepath_gif . '.txt';
my $filepath_xmp = $filepath_gif . '.xmp';

open my $filehandle_txt, '<', $filepath_txt or die "cannot open '$filepath_txt': $!";
my %hash = map { my$s=$_;$s=~s/[\s:]*$//;$s } grep /\S+/, <$filehandle_txt>;
close $filehandle_txt;

my $exifTool = new Image::ExifTool;
$exifTool->SetNewValue(Author => $hash{Title}) or die;
$exifTool->WriteInfo( undef, $filepath_xmp, 'XMP' );
}



In Terminal (OSX) I execute the command:


Code
perl test1.pl


The message I receive is:

"cannot open '123.gif.txt': No such file or directory at test1.pl line 10."

But, of course, the file does exist, so I'm not sure what the issue is?

Thanks!


FishMonger
Veteran / Moderator

Jun 23, 2016, 8:20 AM

Post #31 of 38 (7381 views)
Re: [garry664] Parsing data from TXT file into XMP format with ExifTool [In reply to] Can't Post

Maybe it's a case problem where the actual file ext is really '.TXT' instead of '.txt'.

What is the output of this command?

Code
ls -l 123.gif*



Laurent_R
Veteran / Moderator

Jun 23, 2016, 9:33 AM

Post #32 of 38 (7378 views)
Re: [garry664] Parsing data from TXT file into XMP format with ExifTool [In reply to] Can't Post

Perhaps you should try to replace:

Code
for my $filepath_gif ( <*.gif> )

with:

Code
for my $filepath_gif ( <*.gif.txt> )

and make the relevant changes in the loop. At least you would be sure to try to open files that really exist in the directory.

Or else add a code line to check for the existence of the file before trying to open it:


Code
my $filepath_txt = $filepath_gif . '.txt';  
say "File $filepath_txt does not exist" and next unless -f $filepath_txt ;
# ...


This is probably not going to solve the problem, but at least the diagnosis might become clearer.


garry664
Novice

Jun 27, 2016, 5:20 PM

Post #33 of 38 (7293 views)
Re: [Laurent_R] Parsing data from TXT file into XMP format with ExifTool [In reply to] Can't Post

I tried changing it to "<*.gif.txt>" before, I thought that it could be the reason, but it wasn't as it just adds another ".txt" on to the end:


Code
cannot open '123.gif.txt.txt': No such file or directory at test2.pl line 10.


The other code spat out the following:


Code
"my" variable $filepath_txt masks earlier declaration in same scope at test3.pl line 18. 
String found where operator expected at test3.pl line 19, near "say "File $filepath_txt does not exist""
(Do you need to predeclare say?)
syntax error at test3.pl line 19, near "say "File $filepath_txt does not exist""
Execution of test3.pl aborted due to compilation errors.


I added the code wishing the curly braces, at the end of the function, not sure if that was what you wanted?

Unfortunately, it didn't work.


garry664
Novice

Jun 27, 2016, 5:23 PM

Post #34 of 38 (7290 views)
Re: [FishMonger] Parsing data from TXT file into XMP format with ExifTool [In reply to] Can't Post


In Reply To
Maybe it's a case problem where the actual file ext is really '.TXT' instead of '.txt'.

What is the output of this command?

Code
ls -l 123.gif*




All extensions are in lower case, I think that I just wanted to emphasise that in the OP title.

Output:

Code
-rwxrwxrwx@ 1 Andi  staff  873930  4 Mar  2015 1425.gif  
-rwxrwxrwx@ 1 Andi staff 614 4 Mar 2015 1425.gif.txt



FishMonger
Veteran / Moderator

Jun 28, 2016, 9:36 AM

Post #35 of 38 (7265 views)
Re: [garry664] Parsing data from TXT file into XMP format with ExifTool [In reply to] Can't Post

Lets take a step back and remove all of the Image::ExifTool stuff and make a test script that just outputs a list of files to see what perl "sees".

The script needs to be in the same directory as the gif/txt files AND you need to cd into that directory to run the script.

Code
#!/usr/bin/perl 

use strict;
use warnings;
use diagnostics;

foreach my $file (<*.gif*>) {
print "$file\n";
}


After verifying that the script outputs the matching gif/txt files, then run this test script.

Code
#!/usr/bin/perl 

use strict;
use warnings;
use diagnostics;

foreach my $file (<*.gif>) {
chomp $file; # this isn't really needed but,
# I put it in on the odd chance that
# the diamand operator in the old perl
# appended a line terminator to the filename

print "$file\n";

my $txt_file = "$file.txt";
if (-e $txt_file) {
print "$txt_file exists ";

if (-r $txt_file) {
print "and is readable\n";
}
else {
print "BUT is NOT readable\n";
}
}
else {
print "$txt_file does not exist\n";
}

}


If the print statement from either of the else blocks gets outputted, then you need to take a closer look at either the spelling of the filename or the permissions on the file.


(This post was edited by FishMonger on Jun 28, 2016, 9:46 AM)


garry664
Novice

Jun 28, 2016, 12:19 PM

Post #36 of 38 (7256 views)
Re: [FishMonger] Parsing data from TXT file into XMP format with ExifTool [In reply to] Can't Post

Many thanks, I tried the sample code and everything seems to be in working order: all files are recognised and the txt files "exist" and are "readable".


teddoff
Novice

Jun 30, 2016, 12:53 PM

Post #37 of 38 (7187 views)
Re: [FishMonger] Parsing data from TXT file into XMP format with ExifTool [In reply to] Can't Post

I'm not sure then why the original code doesn't export the txt files as xmp ones?


Zhris
Enthusiast

Jul 2, 2016, 7:39 PM

Post #38 of 38 (7140 views)
Re: [teddoff] Parsing data from TXT file into XMP format with ExifTool [In reply to] Can't Post


Quote
"cannot open '123.gif.txt': No such file or directory at test1.pl line 10."


'123.gif' doesn't have a corresponding '123.gif.txt'.

I recommend you use Fishmonger's verbose code as a base and take the relevant parts from mine. Check through the output very carefully.

Chris

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives