Ask Dave Taylor
  • Facebook
  • Instagram
  • Linkedin
  • Pinterest
  • Twitter
  • YouTube
  • Home
  • YouTube Videos
  • Top Categories
  • Subscribe via Email
  • Ask A Question
  • Meet Dave
  • Home
  • Linux Help
  • Can I script renaming files based on an XML data map?

Can I script renaming files based on an XML data map?

November 3, 2010 / Dave Taylor / Linux Help, Linux Shell Script Programming / No Comments

I have a folder full of files which are named with four digits and a file extension e.g. 0312.file and an XML-file describing the contents of these files. I am trying to do a shell scipt that will create folders and rename these files according to the xml, but I don’t know how?
This is a template of what the xml looks like:
<section label=”AA. category”>
<children label=”topic” source=”AABB.file” />
</ section>
The script should represent the categories as folders containing the related topics, which should be represented as filenames.

The complexity of this shell script is directly related to how well formed the XML data file you’re working with is. That is, if it’s laid out so that each filename map appears on its own line, we can do that, and do that pretty directly. If we need to actually parse the XML data file, extract value pairs and then parse that, it’s going to be more difficult. In fact, at that point I would suggest you move to Perl or anther language that has better XML support rather than brute-force it with a Linux shell script.
Still, you sent along some sample data and source files and I have a pretty good sense of what you’re trying to accomplish, so let’s give it a shot!
The basic idea of what we’re going to do is to step through the XML file looking for lines that contain a filename mapping, extract the source and target values, then apply them with a rename operation.
Here’s the data file we’ll be working with:

<section label=”01. Introduction”>
  <children label=”Welcome” source=”0101.mp4″ />
  <children label=”How To do Stuff” source=”0102.mp4″ />
  <children label=”About some other things” source=”0103.mp4″ />
</section>
<section label=”02. Additional information”>
  <children label=”Important information” source=”0201.mp4″ />
  <children label=”More important information” source=”0202.mp4″ />
  <children label=”additional important information” source=”0203.mp4″ />
</section>

Looks complicated, but let’s tackle it by ignoring the section labels and instead just look for “children” lines. Each of those has source and label, the latter being the target filename.
For example, the first line has label=”Welcome” and source=”0101.mp4″. Using this as a base, we’d want to rename 0101.mp4 to Welcome.mp4. At least, that’s what this script will do!
Step one is to extract just the children lines in the XML file, something I’ll do with a slightly unusual shell script structure:

grep “<children” $datasource | while read inputline
do
  …
done

It’s little known that any shell block structure is a redirectable container, so I just slip it right into the pipeline and the input to the “while” loop are only the matching lines from the XML data file.
Since it’s well formed data, we can just use “cut” and mark the double-quote as the delimiter:

label=”$(echo $inputline | cut -d\” -f2)”
source=”$(echo $inputline | cut -d\” -f4)”

That’s all the hard work. Now let’s just test to ensure that the source file exists before we issue the actual file renaming instruction. I’ll put it all together here:

grep “<children” $datasource | while read inputline
do
  label=”$(echo $inputline | cut -d\” -f2)”
  source=”$(echo $inputline | cut -d\” -f4)”
  if [ -f $source ] ; then
    echo “Renaming $source to $label.mp4”
    mv “$source” “$label.mp4”
  else
    echo “Error: Matched $source but can’t find it. Skipped”
  fi
done

Now you added an additional complication here because you didn’t just want to look at the “children” lines, but the section labels too, using the latter as subfolder names. That’s more tricky, but still solvable: change the “grep” to include both types of lines in the data file, then have a conditional statement testing to see which you have. Use “cut” to extract category name, then when you’re doing the “mv”, simply tweak it to something like:

if [ ! -d “$category” ] ; then
  echo “Creating directory $category”
  mkdir “$category”
fi
mv “$source” $category/$label.mp4″

See what I’m doing? Not too much more complicated after all!

Let’s Stay In Touch!

Never miss a single article, review or tutorial here on AskDaveTaylor, sign up for my fun weekly newsletter!
Name: 
Your email address:*
Please enter all required fields
Correct invalid entries
No spam, ever. Promise. Powered by FeedBlitz
Please choose a color:
Starbucks coffee cup I do have a lot to say, and questions of my own for that matter, but first I'd like to say thank you, Dave, for all your helpful information by buying you a cup of coffee!

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Search

Recent Posts

  • How Do You Rearrange App Icons on an Android Phone?
  • How Can I Enable Emergency Alerts in Spanish on Android?
  • Switch from 24-Hour Time to AM/PM in Ubuntu Linux?
  • Protect Your Connection and Privacy with Surfshark VPN
  • Can I Send Texts in iMessage with Effects from my Mac System?

On Our YouTube Channel

Monoprice DT-3BT Bluetooth Desktop Speakers -- REVIEW

FATORK Wi-Fi Smart Portable Movie Projector -- DEMO & REVIEW

Categories

  • AdSense, AdWords, and PPC Help (106)
  • Amazon, eBay, and Online Shopping Help, (161)
  • Android Help (203)
  • Apple iPad Help (145)
  • Apple Watch Help (53)
  • Articles, Tutorials, and Reviews (344)
  • Auto Tech Help (12)
  • Business Advice (199)
  • Chrome OS Help (25)
  • Computer & Internet Basics (764)
  • d) None of the Above (165)
  • Facebook Help (383)
  • Google, Chrome & Gmail Help (180)
  • HTML & Web Page Design (245)
  • Instagram Help (48)
  • iPhone & iOS Help (607)
  • iPod & MP3 Player Help (173)
  • Kindle & Nook Help (93)
  • LinkedIn Help (85)
  • Linux Help (167)
  • Linux Shell Script Programming (87)
  • Mac & MacOS Help (895)
  • Most Popular (16)
  • Outlook & Office 365 Help (26)
  • PayPal Help (69)
  • Pinterest Help (53)
  • Reddit Help (18)
  • SEO & Marketing (81)
  • Spam, Scams & Security (93)
  • Trade Show News & Updates (23)
  • Twitter Help (217)
  • Video Game Tips (66)
  • Web Site Traffic Tips (62)
  • Windows PC Help (922)
  • Wordpress Help (204)
  • Writing and Publishing (72)
  • YouTube Help (46)
  • YouTube Video Reviews (159)
  • Zoom, Skype & Video Chat Help (57)

Archives

Social Connections:

Ask Dave Taylor


Follow Me on Pinterest
Follow me on Twitter
Follow me on LinkedIn
Follow me on Instagram


AskDaveTaylor on Facebook



microsoft insider mvp


This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this site or on any linked site. Further, please note that by submitting a question or comment you're agreeing to our terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site. Our lawyer says "Thanks for your cooperation."
© 2022 by Dave Taylor. "Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.
Privacy Policy - Terms and Conditions - Accessibility Policy