Formatting tag values when using the -csv option

Started by Ric, May 26, 2021, 12:57:04 AM

Previous topic - Next topic

Ric

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?

Luuk2005

#1
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!



Windows8.1-64bit,  exiftool-v12.92(standalone),  sed-v4.0.7

StarGeek

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 as Luuk2005 suggests.  See FAQ #12 (paragraph starting "But note that any values containing commas") for dealing with the possibility of commas and other special characters in the output.
"It didn't work" isn't helpful. What was the exact command used and the output.
Read FAQ #3 and use that cmd
Please use the Code button for exiftool output

Please include your OS/Exiftool version/filetype

Ric

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.

Luuk2005

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).
Windows8.1-64bit,  exiftool-v12.92(standalone),  sed-v4.0.7

Phil Harvey

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
...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 ($).

Ric

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.

StarGeek

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 and -p (-printFormat) option 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 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.  Also, see this post where another user created a FMT file to build the CSV output.
"It didn't work" isn't helpful. What was the exact command used and the output.
Read FAQ #3 and use that cmd
Please use the Code button for exiftool output

Please include your OS/Exiftool version/filetype