Rename folder based on containing XML file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Rename folder based on containing XML file
# 1  
Old 12-02-2009
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.
# 2  
Old 12-02-2009
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
# 3  
Old 12-02-2009
Code:
sed 's/\(<ProjectName>SOME-PROJECT-NAME\).*\(<\/ProjectName>\)/\1\2/' urfile

# 4  
Old 12-03-2009
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?
# 5  
Old 12-04-2009
u may use signal handling for it ,
try something like :-

Code:
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash to copy file 3 times and rename based on another file

In the below bash I am trying to copy the only text file (always only one) in /home/cmccabe/Desktop/list/QC/metrics.txt and rename each of the 3 text files according to /home/cmccabe/Desktop/test/list.txt using lines 3, 4 ,5. This format (that is list.txt) is always 5 lines. Thank you :). ... (12 Replies)
Discussion started by: cmccabe
12 Replies

2. UNIX for Beginners Questions & Answers

Rename files based on simple text file

Hello! New here although not completely new to Unix. I wonder how I could rename files based on the data found in a simple textfile. It goes like this: I have 4 files 1 ldfgkkfjslkdfjsldkfjsf.wav 2 nndsdflksdjf.wav 3 sdflksjdf jjsdflsdfl.wav 4 dkadsdddd.wav Textfile.txt looks like... (14 Replies)
Discussion started by: Oortone
14 Replies

3. Shell Programming and Scripting

Split xml file into multiple xml based on letterID

Hi All, We need to split a large xml into multiple valid xml with same header(2lines) and footer(last line) for N number of letterId. In the example below we have first 2 lines as header and last line as footer.(They need to be in each split xml file) Header: <?xml version="1.0"... (5 Replies)
Discussion started by: vx04
5 Replies

4. Shell Programming and Scripting

Rename files based on name in text file

Hello, I have a text file "file.list" with the contents below. file1 filename1 file2 filename2 file3 filename3 file1, file2 and file3 are files existing in the same directory as the text file file.list. I want to rename file1 to filename1, file2 to filename2, as show in the text... (1 Reply)
Discussion started by: james2009
1 Replies

5. Shell Programming and Scripting

Remove or rename based on contents of file

I am trying to use the two files shown below to either remove or rename contents in one of those files. If in file1.txt $5 matches $5 of file2.txt and the value in $1 of file1.txt is not "No Match" then that value is substituted for all values in $5 and $1 of file2.txt. If however in $1 ... (5 Replies)
Discussion started by: cmccabe
5 Replies

6. Shell Programming and Scripting

Need help in writitng a script to rename file name and copy to other folder

Hi All, My requirement is as follows: A file (say abc) will be having list of the .txt file names. I need to read this abc file line by line and rename the .txt file names inside it and move them to other folder/path. Eg: abc ------- file1.txt file2.txt file3.txt Output (should... (1 Reply)
Discussion started by: pavan.yadalla
1 Replies

7. Shell Programming and Scripting

Rename files with tag in a .xml file

Hi Guys! 1st of all, i am sorry for my bad english! I got a Sat-Receiver and i recorded some Stuff for my Children, now the Harddrive is full and i want to convert and rename it, that i can put it on DVDs But I've got a big Problem! I want to rename the files , with a tag in a xml file... (4 Replies)
Discussion started by: crusty
4 Replies

8. Shell Programming and Scripting

Rename a file based on a specific separator

Hello, I am new to shell I have a folder which contains a list of files, all the files contain the separator : I need to replace this character for all the filenames (by batch) ex: hello:world should become hello-world please help Thanks (3 Replies)
Discussion started by: sikilaklak
3 Replies

9. UNIX for Advanced & Expert Users

Unix Command to rename a file in a zipped folder

Hi, I am into automation work. Is there any UNIX command to rename a file in a zipped folder, without unzipping it..??? Thanks in advance.. (1 Reply)
Discussion started by: Victoria.Sam
1 Replies

10. Shell Programming and Scripting

Rename File Based on Created Date

I am trying to rename files based on the created/born date of the file. I Have a total of 4000 files that i am trying to do this with and would like it to be log_yyyymmddhh.gz right now the files are maillog.???.gz. Can anyone point me in the right direction of how to get this done via scipt? ... (4 Replies)
Discussion started by: Paulb
4 Replies
Login or Register to Ask a Question