Author Topic: Trying to rename files with basename - Newbie  (Read 3177 times)

James Penner

  • Jr. Member
  • **
  • Posts: 24
Trying to rename files with basename - Newbie
« on: May 04, 2014, 12:47:41 AM »
Hello,

I've spent most of the day searching and experimenting with ExifTool. I think it'll do everything I want, but I'm a little over my head and I'm not sure how to get it working right. I'm especially confused by .ExifTool_config, which I seem to have installed correctly, but I can't seem to configure it properly.

My goal:

I want to learn how to write a batch file to get ExifTool to rename images that will be in the same directory.

The files would be renamed be prepending the created date (or if that isn't available, the file modified date)  in the following format: yyyymmdd.
I would like to separate the date and existing file name with an underscore and then replace all spaces with hyphens.

So an original file name might look like this:
    this is a picture.jpg

And the final result after running the batch would be
   20140430_this-is-a-picture.jpg

Any assistance would be greatly appreciated.

Thank you for your time.

  - James

Phil Harvey

  • ExifTool Author
  • Administrator
  • ExifTool Freak
  • *****
  • Posts: 17044
    • ExifTool Home Page
Re: Trying to rename files with basename - Newbie
« Reply #1 on: May 04, 2014, 07:25:25 AM »
Hi James,

You don't need a config file to do this.  The following command will do what you want:

exiftool "-filename<${filemodifydate}_${filename;tr/ /_/}" "-filename<${createdate}_${filename;tr/ /_/}" -d %Y%m%d DIR

where DIR is the name of a directory containing the images.

(the above quoting is for Windows.  use single instead of double quotes if you are on Mac or Linux)

See this documentation for more help with renaming files.  Also see the -tagsFromFile option in the application documentation for more information.  (This file renaming feature is just one use of the -tagsFromFile option.)

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

James Penner

  • Jr. Member
  • **
  • Posts: 24
Re: Trying to rename files with basename - Newbie
« Reply #2 on: May 04, 2014, 11:32:11 AM »
Phil, thank you for taking the time to help me out; it works great! ;D 

I'm going to have to break down the command to better understand what's going on, but I'm really excited to learn more about this tool and how to integrate it into my workflow.

Thanks again!

Phil Harvey

  • ExifTool Author
  • Administrator
  • ExifTool Freak
  • *****
  • Posts: 17044
    • ExifTool Home Page
Re: Trying to rename files with basename - Newbie
« Reply #3 on: May 04, 2014, 01:10:19 PM »
Great.  I just re-read your original post and noticed that you wanted spaces translated to hyphens, not underlines, so I should have used tr/ /-/ in the advanced formatting expressions.

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

James Penner

  • Jr. Member
  • **
  • Posts: 24
Re: Trying to rename files with basename - Newbie
« Reply #4 on: May 04, 2014, 06:26:46 PM »
I was able to figure that part out on my own, but thank you for following up to clarify :)  I think I'm starting to get a better handle on the syntax, especially being able to break down your example and using it to explore other ways of rewriting file names.   

I do have a couple of questions about "tr/ /_/", though, if you don't mind. I see how it's used to replace (in this example) a space with an underscore, but can it be used to simply remove all instances of a character? I tried "tr/ //" to remove all spaces and replace with nothing, but that didn't seem to do anything.

Also, I read in the documentation that:
  "A default expression of tr(/\\?*:|"<>\0)()d is assumed if the expression is empty, which removes the characters / \ ? * : | < > and null from the printed value."

Could I use that default expression in the renaming command you showed me to remove any of those characters from a file name? I tried a couple of different ways, but couldn't get it to work properly either.

Thank you again for your time and assistance.

  - James


Phil Harvey

  • ExifTool Author
  • Administrator
  • ExifTool Freak
  • *****
  • Posts: 17044
    • ExifTool Home Page
Re: Trying to rename files with basename - Newbie
« Reply #5 on: May 04, 2014, 09:02:45 PM »
Hi James,

I do have a couple of questions about "tr/ /_/", though, if you don't mind. I see how it's used to replace (in this example) a space with an underscore, but can it be used to simply remove all instances of a character? I tried "tr/ //" to remove all spaces and replace with nothing, but that didn't seem to do anything.

To do this, it is "tr/ //d" ("d" to delete).  Read about the Perl "tr" operator for a full description.

Quote
"A default expression of tr(/\\?*:|"<>\0)()d is assumed if the expression is empty, which removes the characters / \ ? * : | < > and null from the printed value."

Could I use that default expression in the renaming command you showed me to remove any of those characters from a file name? I tried a couple of different ways, but couldn't get it to work properly either.

To do just this, it would be "${filename;}", but to combine it with other things you would have to do something like this: "${filename;tr/ /-/;tr(/\\?*:|"<>\0)()d".

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

James Penner

  • Jr. Member
  • **
  • Posts: 24
Re: Trying to rename files with basename - Newbie
« Reply #6 on: May 04, 2014, 11:27:10 PM »
Beautiful, thank you!