Renaming multiple files in a folder


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Renaming multiple files in a folder
# 1  
Old 02-24-2011
MySQL Renaming multiple files in a folder

Hi All,
I am trying to change the XML filename in a folder to the rootelement in the XML file.
Example: TestMsg2010-10-19_20_20_54.xml
Code:
<?xml version="1.0" encoding="utf-8" ?> 
<R1Msg TimeStamp="2010-10-19T08:49:08.000000Z>
<TestRec>
<ID>1000</ID> 
</TestRec>
</R1Msg>

Wanted to extract the R1Msg and change the filename TestMsg2010-10-19_20_20_54.xml
to R1Msg2010-10-19_20_20_54.xml
.
Now I have something like this.
Code:
for i in *.xml; do echo $i;awk -F"[<>]" 'NR==2{print $2}' $i; done

Output of above command:
Code:
TestMsg2010-10-19_20_20_54.xml
R1Msg TimeStamp="2010-10-19T08:49:08.000000Z"
TestMsg2010-10-19_20_20_58.xml
R2Msg TimeStamp="2010-10-19T08:49:08.000000Z"
Renamed File Output what I want to achieve
R1Msg2010-10-19_20_20_54.xml
R2Msg2010-10-19_20_20_58.xml
R3Msg2010-10-19_20_21_00.xml

Is there a way that I can tweak the above command to achieve the same? Please help me as I am new to this side of programming.
Thanks in advance.

Last edited by Franklin52; 02-24-2011 at 03:39 AM.. Reason: Please use code tags, thank you
# 2  
Old 02-24-2011
Try this but check the output before you pipe the output to sh (colored part):
Code:
for i in *.xml
do 
  awk -F"[ <]" 'BEGIN{f=FILENAME; sub(".*Msg","",f)}
  /TimeStamp=/ {f=$2 f; print "mv " FILENAME " " f; exit}' "$i" | sh
done

This User Gave Thanks to Franklin52 For This Post:
# 3  
Old 02-24-2011
It gives me the output R1Msg R2Msg R3Msg .
Help me how would I make it as below.

R1Msg2010-10-19_20_20_54.xml
R2Msg2010-10-19_20_20_58.xml
R3Msg2010-10-19_20_21_00.xml

Thanks for your quick response.Smilie
-Chari.
# 4  
Old 02-24-2011
With the provided xml file I get this output:
Code:
$ ls *.xml
TestMsg2010-10-19_20_20_54.xml
$ cat script
for i in *.xml
do 
  awk -F"[ <]" 'BEGIN{f=FILENAME; sub(".*Msg","",f)}
  /TimeStamp=/ {f=$2 f; print "mv " FILENAME " " f; exit}' "$i"
done
$ ./script
mv TestMsg2010-10-19_20_20_54.xml R1Msg2010-10-19_20_20_54.xml
$

# 5  
Old 02-27-2011
Franklin52 ,
I see only mv TestMsg2010-10-19_20_20_54.xml R1Msg as output. Any clues what goes wrong will be appreciated.Thanks.
# 6  
Old 02-28-2011
Quote:
Originally Posted by sree_chari
Franklin52 ,
I see only mv TestMsg2010-10-19_20_20_54.xml R1Msg as output. Any clues what goes wrong will be appreciated.Thanks.
Move the assignment of file name from the BEGIN block.


Code:
for i in *.xml
do 
  awk  -F="[ <]" '{f=FILENAME; sub(".*Msg","",f)}
  /TimeStamp=/ {f=$2 f; print "mv " FILENAME " " f; exit}' "$i"
done


Last edited by Aia; 02-28-2011 at 01:09 AM..
# 7  
Old 02-28-2011
Aia,
Thanks for your response. Closer but not there yet. The out is now mv TestMsg2010-10-19_20_20_54.xml 2010-10-19_20_20_54.xml . The Root Element is missing. Regards.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Renaming multiple files in sftp server in a get files script

Hi, In sftp script to get files, I have to rename all the files which I am picking. Rename command does not work here. Is there any way to do this? I am using #!/bin/ksh For eg: sftp user@host <<EOF cd /path get *.txt rename *.txt *.txt.done ... (7 Replies)
Discussion started by: jhilmil
7 Replies

2. Shell Programming and Scripting

Renaming files & folder according to the similarities in filenames

hello does someone want to help me for this one ? i want to rename files & a folder according to the similarities in filenames for example : the file with the good name cglogo tougl1953 dgmel bogd 01 -- ttgductoog ggdté gollogtd.ext1the others files needed to be renamed cglogo... (5 Replies)
Discussion started by: mc2z674gj
5 Replies

3. UNIX for Dummies Questions & Answers

Renaming files in a folder

Hi, I have 1000 *.txt files in a folder and I would like to rename them data1.txt through data1000.txt. The order does not matter or the names of the initial input files. How would I go about doing that? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

4. UNIX for Dummies Questions & Answers

Renaming files in a folder

Hi, Hope someone can help... I'm pretty much new to unix and can only do basic commands. I have an issue where by every day I need to schedule a job to look in a folder with a name format of .IP. will be different each day (when the day is over the folder extension changes), and rename any... (5 Replies)
Discussion started by: Harsharan
5 Replies

5. Shell Programming and Scripting

Help with renaming files in folder

Hello, I want to rename more than 100 files. These files have "?" in there existing file name but I want to rename files to there first part before "?" token. I have tried using for loop but it isn't working so help following is the sample filename and for loop that am trying to use: example of... (3 Replies)
Discussion started by: ls_lrta
3 Replies

6. Shell Programming and Scripting

renaming files or adding a name in the beginning of all files in a folder

Hi All I have a folder that contains hundreds of file with a names 3.msa 4.msa 21.msa 6.msa 345.msa 456.msa 98.msa ... ... ... I need rename each of this file by adding "core_" in the begiining of each file such as core_3.msa core_4.msa core_21.msa (4 Replies)
Discussion started by: Lucky Ali
4 Replies

7. Shell Programming and Scripting

Renaming multiple files in a folder

Hi , Need help to rename the files in a folder. The .xml needs to be removed from the middle and appended to the end as in the example shown . Example From: TestMessage.xml2010-10-19_20_21_08 TestMessage.xml2010-10-20_20_21_09 TestMessage.xml2010-10-21_20_21_08... (4 Replies)
Discussion started by: sree_chari
4 Replies

8. Shell Programming and Scripting

renaming files in a folder

I splited a file into parts, then i wrote a script to convert the format of the file names to this: part1.dat part2.dat but I wonder why part.dat is also created knowing that the k variable starts from 1 rm -f part* $(split -b $1 file.1 part_) set k = 1 for i in $(ls part_*) do mv -f... (1 Reply)
Discussion started by: saman_glorious
1 Replies

9. UNIX for Advanced & Expert Users

Renaming all files in a folder

Guys, i need to replace a particular pattern in lots of files in a folder with another pattern . Is there any command line script I can use to do the functionality. Eg: aalex > ls green_1234_colour.max red_1234_pen.max yellow1234flower.max Need to replace the 1234 in the file name with 9876... (4 Replies)
Discussion started by: appualex
4 Replies

10. Shell Programming and Scripting

Getting error by renaming all the files in a folder

Hi All, I have a folder name as postscript folder and it contains the following postscript files. package1.ps package2.ps package3.ps when i am renaming all the ps files to xps files by using the following command mv /postscript/*.ps /postscript/*.xps Then i am getting the... (4 Replies)
Discussion started by: sunitachoudhury
4 Replies
Login or Register to Ask a Question