Rename folder based on containing XML file

Hi everyone.

I'm in need of a solution where i need to rename a folder to a name that's inside an XML file in that folder.

OS is Ubuntu 9.10 with Gnome.

I've tried using grep, sed and xpath, but can't seem to find a solution.

This is the simplified folder structure:

FOLDER-NAME

  • desc.xml
  • other files...

The script will be called with the full path of the folder in the first argument (from nautilus-actions in gnome).
eg: /path/to/script/rename.sh /path/to/folder

The script should read the containing desc.xml file and look for the <ProjectName> tag.

Simplified version of the desc.xml file:
<Project>
<ProjectName>SOME-PROJECT-NAME_A_B_C</ProjectName>
</Project>

Eventually the folder should be renamed to "SOME-PROJECT-NAME".
Note that the text is trimmed from the first char to the first _ (underscore).

The closest i've come is to get the SOME-PROJECT-NAME_A_B_C name, but don't know exactly how to strip it to SOME-PROJECT-NAME and eventually rename the folder that way.

Any help would be much appreciated!

Greetings.

Hi,

i had a similar problem with XML parsinig,
the following link helped a lot, hope it helps u 2 :-
SED to parse and modify XML element nodes

Cheers

sed 's/\(<ProjectName>SOME-PROJECT-NAME\).*\(<\/ProjectName>\)/\1\2/' urfile

Thanks to both, you pointed me in the right direction.

Got it working now.

btw, is it possible to show some kind of message box from the shell to inform when something went wrong without using the dialog package?

u may use signal handling for it ,
try something like :-

errorDebugger(){
        set +x
        echo "$(date ++%d-%m-%Y_%l:%M:%S) :::  Script :- $0 :::  Line No :- $2 :::  Line ====  $(sed -n  "$2 p" $0)" >> //loaction of ur error log file
}

trap 'errorDebugger $? $LINENO' ERR


this will display the time, line no and script name if an ERR signal is thrown.

Hope it helps

Cheers