Need help how to create a file (xml) list all files from directory | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Need help how to create a file (xml) list all files from directory

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 10-14-2012
ggcc ggcc is offline
Registered User
 
Join Date: Dec 2011
Last Activity: 3 March 2013, 4:37 PM EST
Posts: 17
Thanks: 10
Thanked 0 Times in 0 Posts
Need help how to create a file (xml) list all files from directory

I have more than 10K songs in two directories on a hard drive. I would like to create a file list all of files name then change to .xml extension to upload to iPhone so I have a Karaoke list on my iPhone.

I need your help to create a file by using command in Linux.

Files names:

Code:
0001 More Than I Can Say.vob
0002 Morning Has Broken.vob 
....
0010 Unchained Melody.vob

1-Here are my two small directories called Directory-1 and Directory-2 with .vob files.

Directory-1:
total 0

Code:
-rw-r--r-- 1 root root 0 2012-10-14 11:15 0001 More Than I Can Say.vob
-rw-r--r-- 1 root root 0 2012-10-14 11:20 0002 Morning Has Broken.vob
-rw-r--r-- 1 root root 0 2012-10-14 11:16 0003 My Way Rumba.vob
-rw-r--r-- 1 root root 0 2012-10-14 10:56 0004 Papa.vob
-rw-r--r-- 1 root root 0 2012-10-14 11:19 0005 Touch By Touch.vob

Directory-2:
total 0

Code:
-rw-r--r-- 1 root root 0 2012-10-14 10:52 0006 Unchained Melody.vob
-rw-r--r-- 1 root root 0 2012-10-14 10:52 0007 Unchained Melody.vob
-rw-r--r-- 1 root root 0 2012-10-14 10:52 0008 Unchained Melody.vob
-rw-r--r-- 1 root root 0 2012-10-14 10:52 0009 Unchained Melody.vob
-rw-r--r-- 1 root root 0 2012-10-14 10:53 0010 Unchained Melody.vob

I would like to run command(s) to create a file name KaraokeDB.xml from the directories above, and the content of the KaraokeDB.xml as below.


Code:
<?xml version="1.0" encoding="UTF-8"?>
<Karaoke>
<item name= "0001 More Than I Can Say.vob" id="1">/Directory-1/0001 More Than I Can Say.vob</item>
<item name= "0002 Morning Has Broken.vob" id="2">/Directory-1/0002 Morning Has Broken.vob</item>  
<item name= "0003 My Way Rumba.vob" id="3">/Directory-1/0003 My Way Rumba.vob</item>  
<item name= "0004 Papa.vob" id="4">/Directory-1/0004 Papa.vob</item>
<item name= "0005 Touch By Touch.vob" id="5">/Directory-1/0005 Touch By Touch.vob</item>
<item name= "0006 Unchained Melody.vob" id="6">/Directory-2/0006 Unchained Melody.vob</item>
<item name= "0007 Unchained Melody.vob" id="7">/Directory-2/0007 Unchained Melody.vob</item> 
<item name= "0008 Unchained Melody.vob" id="8">/Directory-2/0008 Unchained Melody.vob</item>
<item name= "0009 Unchained Melody.vob" id="9">/Directory-2/0009 Unchained Melody.vob</item>
<item name= "0010 Unchained Melody.vob" id="10">/Directory-2/0010 Unchained Melody.vob</item>
</Karaoke>

Thank you so much for all help.

Last edited by Scott; 10-14-2012 at 10:28 PM.. Reason: Code tags
Sponsored Links
    #2  
Old 10-14-2012
spacebar's Avatar
spacebar spacebar is offline
Registered User
 
Join Date: Oct 2009
Last Activity: 2 June 2014, 3:23 PM EDT
Location: spaceBAR Central
Posts: 440
Thanks: 0
Thanked 70 Times in 70 Posts
This is one way to do it:

Code:
dir1="dir1"
dir2="dir2"
echo "<?xml version="1.0" encoding="UTF-8"?>" >xml_file.xml
echo "<Karaoke>" >>xml_file.xml
for x in `ls -1 ${dir1}/* ${dir2}/*`
do
  IFS=$'\n'
  file=`echo ${x} | sed 's/.*\/\|.*\///g'`
  dir=`echo ${x} | sed 's/\/.*//g'`
  echo "<item name= \"${file}\" id=\"1\">/$dir/$file</item>" >>xml_file.xml
done
echo "</Karaoke>" >>xml_file.xml


$ cat xml_file.xml
<?xml version=1.0 encoding=UTF-8?>
<Karaoke>
<item name= "0001 More Than I Can Say.vob" id="1">/dir1/0001 More Than I Can Say.vob</item>
<item name= "0002 Morning Has Broken.vob" id="1">/dir1/0002 Morning Has Broken.vob</item>
<item name= "0003 My Way Rumba.vob" id="1">/dir1/0003 My Way Rumba.vob</item>
<item name= "0004 Papa.vob" id="1">/dir1/0004 Papa.vob</item>
<item name= "0005 Touch By Touch.vob" id="1">/dir1/0005 Touch By Touch.vob</item>
<item name= "0006 Unchained Melody.vob" id="1">/dir2/0006 Unchained Melody.vob</item>
<item name= "0007 Unchained Melody.vob" id="1">/dir2/0007 Unchained Melody.vob</item>
<item name= "0008 Unchained Melody.vob" id="1">/dir2/0008 Unchained Melody.vob</item>
<item name= "0009 Unchained Melody.vob" id="1">/dir2/0009 Unchained Melody.vob</item>
<item name= "0010 Unchained Melody.vob" id="1">/dir2/0010 Unchained Melody.vob</item>
</Karaoke>


Last edited by fpmurphy; 10-15-2012 at 11:08 AM.. Reason: removed fonts and colors, fixed up XML output
The Following User Says Thank You to spacebar For This Useful Post:
ggcc (10-15-2012)
Sponsored Links
    #3  
Old 10-14-2012
ggcc ggcc is offline
Registered User
 
Join Date: Dec 2011
Last Activity: 3 March 2013, 4:37 PM EST
Posts: 17
Thanks: 10
Thanked 0 Times in 0 Posts
Hello Spacebar,

Thank you so much for your help. I copied your code and run it. The result is almost there. Would you please help to fix two small problems below.

1-The file name is broke down to multiple file names because of the file name contains space bar.

2-The id counter should counter id=1, id=2 ,id=3 etc...

Otherwise it's perfect. Here is the output from your code.

Code:
<?xml version=1.0 encoding=UTF-8?>
<Karaoke>
<item name= "0001" id="1">/dir1/0001</item>
<item name= "More" id="1">/More/More</item>
<item name= "Than" id="1">/Than/Than</item>
<item name= "I" id="1">/I/I</item>
<item name= "Can" id="1">/Can/Can</item>
<item name= "Say.vob" id="1">/Say.vob/Say.vob</item>
<item name= "0002" id="1">/dir1/0002</item>
<item name= "Morning" id="1">/Morning/Morning</item>
<item name= "Has" id="1">/Has/Has</item>
<item name= "Broken.vob" id="1">/Broken.vob/Broken.vob</item>
<item name= "0003" id="1">/dir1/0003</item>
<item name= "My" id="1">/My/My</item>
<item name= "Way" id="1">/Way/Way</item>
<item name= "Rumba.vob" id="1">/Rumba.vob/Rumba.vob</item>
<item name= "0004" id="1">/dir1/0004</item>
<item name= "Papa.vob" id="1">/Papa.vob/Papa.vob</item>
<item name= "0005" id="1">/dir1/0005</item>
<item name= "Touch" id="1">/Touch/Touch</item>
<item name= "By" id="1">/By/By</item>
<item name= "Touch.vob" id="1">/Touch.vob/Touch.vob</item>
<item name= "0006" id="1">/dir2/0006</item>
<item name= "Unchained" id="1">/Unchained/Unchained</item>
<item name= "Melody.vob" id="1">/Melody.vob/Melody.vob</item>
<item name= "0007" id="1">/dir2/0007</item>
<item name= "Unchained" id="1">/Unchained/Unchained</item>
<item name= "Melody.vob" id="1">/Melody.vob/Melody.vob</item>
<item name= "0008" id="1">/dir2/0008</item>
<item name= "Unchained" id="1">/Unchained/Unchained</item>
<item name= "Melody.vob" id="1">/Melody.vob/Melody.vob</item>
<item name= "0009" id="1">/dir2/0009</item>
<item name= "Unchained" id="1">/Unchained/Unchained</item>
<item name= "Melody.vob" id="1">/Melody.vob/Melody.vob</item>
<item name= "0010" id="1">/dir2/0010</item>
<item name= "Unchained" id="1">/Unchained/Unchained</item>
<item name= "Melody.vob" id="1">/Melody.vob/Melody.vob</item>
</Karaoke>


Last edited by Scott; 10-14-2012 at 10:29 PM.. Reason: Code tags, please...
    #4  
Old 10-14-2012
spacebar's Avatar
spacebar spacebar is offline
Registered User
 
Join Date: Oct 2009
Last Activity: 2 June 2014, 3:23 PM EDT
Location: spaceBAR Central
Posts: 440
Thanks: 0
Thanked 70 Times in 70 Posts
Yep I ran the statements from the command line, Try this since your running it as a shell script:

Code:
dir1="dir1"
dir2="dir2"
echo "<?xml version="1.0" encoding="UTF-8"?>" >xml_file.xml
echo "<Karaoke>" >>xml_file.xml
IFS=$'\n'
for x in `ls -1 ${dir1}/* ${dir2}/*`
do
  file=`echo ${x} | sed 's/.*\/\|.*\///g'`
  dir=`echo ${x} | sed 's/\/.*//g'`
  echo "<item name= \"${file}\" id=\"1\">/$dir/$file</item>" >>xml_file.xml
done
echo "</Karaoke>" >>xml_file.xml


Last edited by fpmurphy; 10-15-2012 at 11:09 AM.. Reason: removed color and font directives
The Following User Says Thank You to spacebar For This Useful Post:
ggcc (10-15-2012)
Sponsored Links
    #5  
Old 10-15-2012
ggcc ggcc is offline
Registered User
 
Join Date: Dec 2011
Last Activity: 3 March 2013, 4:37 PM EST
Posts: 17
Thanks: 10
Thanked 0 Times in 0 Posts
Hello Spacebar,

Your code is working perfectly. Thank you so much for your big help. I was able to load the .xml file with more than 10K songs to the iPhone, now I have a list of Karaoke on my iPhone instead of a binder with more than two hundreds pages.

Thank you so much again.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to create multiple list of files in a directory ? robinbannis Shell Programming and Scripting 4 06-12-2009 12:02 PM
create diffrent files based on other file and parameters list raghav525 Shell Programming and Scripting 9 03-18-2009 04:51 AM
ls > file - Creating file containing the list of all files present in a directory pranavagarwal Shell Programming and Scripting 1 09-26-2008 09:37 PM
ksh: How to get latest file from a list of files in a directory Jayathirtha Shell Programming and Scripting 1 06-24-2003 11:23 AM



All times are GMT -4. The time now is 06:46 PM.