ExifTool Forum

ExifTool => Newbies => Topic started by: Ric on May 26, 2021, 12:57:04 AM

Title: Formatting tag values when using the -csv option
Post by: Ric on May 26, 2021, 12:57:04 AM
Hi,
I have successfully used:
   C:/Tools/ExifTool/exiftool.exe -r -ext PDF -g -csv -FilePath -FileName -FileTypeExtension -PDF:Title DIR   > test.csv
to grab some basic metadata from PDF files.

Since I am running on Windows 10 I wanted to change forward slashes to backward slashes.  Naively I used:
   C:/Tools/ExifTool/exiftool.exe -r -ext PDF -g -csv -p "${FilePath;s/\//\\/g}" -FilePath -FileName -FileTypeExtension -PDF:Title DIR   > test.csv
but it is a no go.

Suggestions?
Title: Re: Formatting tag values when using the -csv option
Post by: Luuk2005 on May 26, 2021, 06:06:48 AM
Yes, the -p option wont modify -csv output. So at first, Im going to say just use...  -api Filter="s|/|\\|g"
But Filter only changes tag-values (not SourceFile), so if needing \SourceFile, Im thinking to replace SourceFile with $Directory\$Filename like...

exiftool -m -r -g -ext pdf -api filter="s|/|\\|g"
   -p "#[HEAD]SourceFile,FilePath,FileName,FileTypeExtension,PDF:Title" 
   -p "$Directory\$Filename,$FilePath,$FileName,$FileTypeExtension,$PDF:Title" DIR >test.csv

exiftool -m -r -ext pdf -api filter="s|/|\\|g"  -p "#[HEAD]SourceFile,FilePath,FileName,FileTypeExtension,PDF:Title"  -p "$Directory\$Filename,$FilePath,$FileName,$FileTypeExtension,$PDF:Title" DIR >test.csv
Edit: Fixed extension to pdf!

The bad part of using -p instead of -csv is values with comma arent "auto-quoted" without something like -api Filter="tr|/|\\|; s/,/\x22,\x22/g"
And the problem with adding too many filters, is that all matching tag-values get replaced, so Im probably just use something simple like...
exiftool -r -g -ext pdf -api filter="s|/|\\|g" -csv -FilePath -FileName -FileTypeExtension -PDF:Title DIR >test.csv
(Then use another app for converting all of the / in SourceFile)
If there is a way to edit just one Tag for the -csv output, Im very interested to discover the method!



Title: Re: Formatting tag values when using the -csv option
Post by: StarGeek on May 26, 2021, 11:39:45 AM
Exiftool is in Perl and Perl internally converts all backslashes in a path to slashes.  There isn't an option to change this in the sourcefile output for a csv.

If backslashes are needed instead of slashes, I find it easier to just load the csv into a spreadsheet such as Excel or Libreoffice and do a search/replace on the column.

The only other option is to create your own CSV file using the -p (-printFormat) option (https://exiftool.org/exiftool_pod.html#p-FMTFILE-or-STR--printFormat) as Luuk2005 suggests.  See FAQ #12 (https://exiftool.org/faq.html#Q12) (paragraph starting "But note that any values containing commas") for dealing with the possibility of commas and other special characters in the output.
Title: Re: Formatting tag values when using the -csv option
Post by: Ric on May 26, 2021, 03:32:59 PM
Thanks Luuk2005 and StarGeek.
Although I have used ExifTool for over a decade off and on this is the first time I have used the "-p" option.  I know, hard to believe.  I would just use ET to do tag reads and then use Perl to do any manipulation.
I can see now that I misunderstood "-p" completely.
I want to stay with "-csv" to avoid any/all FAQ 12 issues. It is clear there is no control over how a tag is actually written to the csv file. Goal was some "/" handling and title tag use as a filename illegal character translation. I'll just push it off to Perl or maybe Excel.
Title: Re: Formatting tag values when using the -csv option
Post by: Luuk2005 on May 26, 2021, 05:45:38 PM
Greetings Ric. Im agree the best solution is always using -csv because of the exiftool logic for handling the special characters.
You can control how tags gets written by combining -csv with -api Filter="tr|/|\\|" but this conducts all tag-values having '/' --> '\'.
I was hoping to discover a way for only conducting certain tags, but now realizing its not possible (and easy to edit later anyways).
Title: Re: Formatting tag values when using the -csv option
Post by: Phil Harvey on May 26, 2021, 10:16:39 PM
Quote from: Ric on May 26, 2021, 03:32:59 PM
I can see now that I misunderstood "-p" completely.
[...]
It is clear there is no control over how a tag is actually written to the csv file.

Not true.  You have complete control over the formatting of every tag written using the -p option using the "advanced formatting" feature.

- Phil
Title: Re: Formatting tag values when using the -csv option
Post by: Ric on May 29, 2021, 01:12:19 AM
Quote from: Phil Harvey on May 26, 2021, 10:16:39 PM
Quote from: Ric on May 26, 2021, 03:32:59 PM
I can see now that I misunderstood "-p" completely.
[...]
It is clear there is no control over how a tag is actually written to the csv file.

Not true.  You have complete control over the formatting of every tag written using the -p option using the "advanced formatting" feature.

- Phil

Hmm.... perhaps I am missing something or have the syntax wrong. If there is complete control using -p etc. then why won't
C:/Tools/ExifTool/exiftool.exe -r -ext PDF -g -csv -p "${FilePath;s/\//\\/g}" -FilePath -FileName -FileTypeExtension -PDF:Title DIR   > test.csv
work, i.e. in the CSV file created by ExifTool the filepath field has the slashes replaced with backslashes and everything else is as it would be without the -p option/argument?

Or
C:/Tools/ExifTool/exiftool.exe -r -ext PDF -g -csv -p "${PDF:Title;s/\s/_/g}" -FilePath -FileName -PDF:Title DIR   > test.csv
which would eliminate whitespace from the title value everywhere in the CSV file.

The command
C:/Tools/ExifTool/exiftool.exe -r -ext PDF -g -csv -FilePath -PDF:Title test.pdf > test.csv
produces two lines in the CSV file
SourceFile,File:FilePath,PDF:Title
test.pdf,C:\Documents\_ExifTool_Play\test.pdf,LoRa®-based Wireless Software Package Quick Start Guide


Whereas
C:/Tools/ExifTool/exiftool.exe -r -ext PDF -g -csv -p "${PDF:Title;s/\s/_/g}" -FilePath -FileName -PDF:Title test.pdf   > test.csv
produces two lines in the CSV file
LoRa®-based_Wireless_Software_Package_Quick_Start_Guide
SourceFile


which seems to be trying but is missing the header row and three other fields. Also the rows appear to be out of order.
Title: Re: Formatting tag values when using the -csv option
Post by: StarGeek on May 29, 2021, 01:31:27 AM
The "SourceFile" column is not a tag, it's a special column to allow exiftool to import data back into the files.  It cannot be altered.

The -csv option (https://exiftool.org/exiftool_pod.html#csv-CSVFILE) and -p (-printFormat) option (https://exiftool.org/exiftool_pod.html#p-FMTFILE-or-STR--printFormat) are not options that work together.  You have to use one or the other to make a CSV file.  The CSV option only allows for editing of data using the -API "Filter=" option (https://exiftool.org/ExifTool.html#Filter) where any changes made with it are global, affecting all tags.

To use the -p option, you have to make your own CSV format.  See the links I provided above.  But with that, you have full control over what is printed by using the Advanced formatting feature (https://exiftool.org/exiftool_pod.html#Advanced-formatting-feature).  Also, see this post (https://exiftool.org/forum/index.php?topic=11158.msg59669#msg59669) where another user created a FMT file to build the CSV output.