Quick directory question

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2005
Posts: 1,542
Reputation: tayspen is on a distinguished road 
Solved Threads: 98
Team Colleague
tayspen's Avatar
tayspen tayspen is offline Offline
<Insert title here>

Quick directory question

 
0
  #1
Sep 11th, 2005
how would i coppy only specific things out of a directory. say *.mp3. I am able to copy the whold directory but i only want specific endings.

this is part of the copy code
  1.  
  2. try
  3. {
  4. Directory.CreateDirectory(targerDirectoryName);
  5.  
  6. string[] files = Directory.GetFiles(sourceDirectoryName);
  7.  
  8. foreach (string fileName in files)
  9. File.Copy(fileName, targerDirectoryName + Path.DirectorySeparatorChar + Path.GetFileName(fileName));
  10.  
  11. string[] directories = Directory.GetDirectories(sourceDirectoryName);
  12.  
  13. foreach (string directory in directories)
  14. CopyDirectory(directory, targerDirectoryName + Path.DirectorySeparatorChar + Path.GetFileName(directory));
  15. }
  16.  
  17. catch
  18. {
  19. //
  20. }

if you no how i can specify wha i want to copy that be great.
Firefox
Ewido
Tune up windows
Get detailed system information
My Fixes

Member - Alliance of Security Analysis Professionals - Since 2006
Reply With Quote Quick reply to this message  
Join Date: Dec 2003
Posts: 55
Reputation: r0ckbaer is an unknown quantity at this point 
Solved Threads: 6
r0ckbaer r0ckbaer is offline Offline
Junior Poster in Training

Re: Quick directory question

 
0
  #2
Sep 11th, 2005
  1. string[] files = Directory.GetFiles(sourceDirectoryName, "*.mp3");
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,542
Reputation: tayspen is on a distinguished road 
Solved Threads: 98
Team Colleague
tayspen's Avatar
tayspen tayspen is offline Offline
<Insert title here>

Re: Quick directory question

 
0
  #3
Sep 11th, 2005
not eacxtly what i want it to do. You see i am attempting to make a program that rips the songs off the ipod. In case you didnt know they are stores in a hidden folder on the ipod called Ipod_control. In that there is a folder called music.. In music there are alot of individual forders called f01 f02 ect. when i tell the program to copy all mp3 from music it copies the f01 ect. folders to. i just want the mp3's. is thre a way i can do this?
Firefox
Ewido
Tune up windows
Get detailed system information
My Fixes

Member - Alliance of Security Analysis Professionals - Since 2006
Reply With Quote Quick reply to this message  
Join Date: Dec 2003
Posts: 55
Reputation: r0ckbaer is an unknown quantity at this point 
Solved Threads: 6
r0ckbaer r0ckbaer is offline Offline
Junior Poster in Training

Re: Quick directory question

 
0
  #4
Sep 11th, 2005
ok, this is a function which scans a sourcedir recursively and copies the found files to the target dir:
  1. /// <summary>
  2. ///
  3. ///
  4. /// </summary>
  5. /// <param name="sourceDir">source directory</param>
  6. /// <param name="targetDir">target directory</param>
  7. /// <param name="targetDir">file type (mp3)</param>
  8. static void Mp3FileCopy(string sourceDir, string targetDir, string fileType)
  9. {
  10. try
  11. {
  12. foreach (string d in Directory.GetDirectories(sourceDir))
  13. {
  14. foreach (string f in Directory.GetFiles(d, fileType))
  15. {
  16. File.Copy(f,
  17. targetDir + "\\" + new FileInfo(f).Name,
  18. false);
  19. }
  20.  
  21. Mp3FileCopy(d, targetDir, fileType);
  22. }
  23. }
  24. catch (System.Exception excpt)
  25. {
  26. Console.WriteLine(excpt.Message);
  27. }
  28. }

you call it like this:
  1. Mp3FileCopy("d:\\download", "d:\\mp3test", "*.mp3");

make sure you created the target dir before attempting to use that function! (replace the source and target dir to your needs!)
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,542
Reputation: tayspen is on a distinguished road 
Solved Threads: 98
Team Colleague
tayspen's Avatar
tayspen tayspen is offline Offline
<Insert title here>

Re: Quick directory question

 
0
  #5
Sep 11th, 2005
thank you, that is exactly what i wanted. thanks again.
Firefox
Ewido
Tune up windows
Get detailed system information
My Fixes

Member - Alliance of Security Analysis Professionals - Since 2006
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,542
Reputation: tayspen is on a distinguished road 
Solved Threads: 98
Team Colleague
tayspen's Avatar
tayspen tayspen is offline Offline
<Insert title here>

Re: Quick directory question

 
0
  #6
Sep 11th, 2005
Ok all is fine cept when i copies the mp3's scince some of them are called wierd names in the ipod control folder. like qwxs thats what it copies them as. is there a way i can tell it to copy but like also copy its id3 tags so the names are correct.

Note: only some songs have weird names in the folder so only so some come out weiird.
Firefox
Ewido
Tune up windows
Get detailed system information
My Fixes

Member - Alliance of Security Analysis Professionals - Since 2006
Reply With Quote Quick reply to this message  
Join Date: Dec 2003
Posts: 55
Reputation: r0ckbaer is an unknown quantity at this point 
Solved Threads: 6
r0ckbaer r0ckbaer is offline Offline
Junior Poster in Training

Re: Quick directory question

 
0
  #7
Sep 13th, 2005
You should remove the sentence "google is there for you" in your sig, because you do anything but googlin' :rolleyes:

http://csharp-home.com/index/tiki-re...?articleId=149
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,542
Reputation: tayspen is on a distinguished road 
Solved Threads: 98
Team Colleague
tayspen's Avatar
tayspen tayspen is offline Offline
<Insert title here>

Re: Quick directory question

 
0
  #8
Sep 13th, 2005
Originally Posted by r0ckbaer
You should remove the sentence "google is there for you" in your sig, because you do anything but googlin' :rolleyes:

http://csharp-home.com/index/tiki-re...?articleId=149

Heh, welll lemme try that.
Firefox
Ewido
Tune up windows
Get detailed system information
My Fixes

Member - Alliance of Security Analysis Professionals - Since 2006
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,542
Reputation: tayspen is on a distinguished road 
Solved Threads: 98
Team Colleague
tayspen's Avatar
tayspen tayspen is offline Offline
<Insert title here>

Re: Quick directory question

 
0
  #9
Sep 13th, 2005
oks o i set this

  1.  
  2. public void Read()
  3. {
  4. File.SetAttributes(textBox1.Text + "\\iPod_Control\\Music", FileAttributes.Normal);
  5. // Read the 128 byte ID3 tag into a byte array
  6. FileStream oFileStream;
  7. oFileStream = new FileStream(this.filename, FileMode.Open);
  8. byte[] bBuffer = new byte[128];
  9. oFileStream.Seek(-128, SeekOrigin.End);
  10. oFileStream.Read(bBuffer, 0, 128);
  11. oFileStream.Close();
  12.  
  13. // Convert the Byte Array to a String
  14. Encoding instEncoding = new ASCIIEncoding(); // NB: Encoding is an Abstract class
  15. string id3Tag = instEncoding.GetString(bBuffer);
  16.  
  17. // If there is an attched ID3 v1.x TAG then read it
  18. if (id3Tag.Substring(0, 3) == "TAG")
  19. {
  20. this.Title = id3Tag.Substring(3, 30).Trim();
  21. this.Artist = id3Tag.Substring(33, 30).Trim();
  22. this.Album = id3Tag.Substring(63, 30).Trim();
  23. this.Year = id3Tag.Substring(93, 4).Trim();
  24. this.Comment = id3Tag.Substring(97, 28).Trim();
  25.  
  26. // Get the track number if TAG conforms to ID3 v1.1
  27. if (id3Tag[125] == 0)
  28. this.Track = bBuffer[126];
  29. else
  30. this.Track = 0;
  31. this.GenreID = bBuffer[127];
  32.  
  33. this.hasTag = true;
  34. // ********* IF USED IN ANGER: ENSURE to test for non-numeric year
  35. }
  36. else
  37. {
  38. this.hasTag = false;
  39. }
  40. }
  41.  
  42. public void updateMP3Tag()
  43. {
  44. // Trim any whitespace
  45. this.Title = this.Title.Trim();
  46. this.Artist = this.Artist.Trim();
  47. this.Album = this.Album.Trim();
  48. this.Year = this.Year.Trim();
  49. this.Comment = this.Comment.Trim();
  50.  
  51. // Ensure all properties are correct size
  52. if (this.Title.Length > 30) this.Title = this.Title.Substring(0, 30);
  53. if (this.Artist.Length > 30) this.Artist = this.Artist.Substring(0, 30);
  54. if (this.Album.Length > 30) this.Album = this.Album.Substring(0, 30);
  55. if (this.Year.Length > 4) this.Year = this.Year.Substring(0, 4);
  56. if (this.Comment.Length > 28) this.Comment = this.Comment.Substring(0, 28);
  57.  
  58. // Build a new ID3 Tag (128 Bytes)
  59. byte[] tagByteArray = new byte[128];
  60. for (int i = 0; i < tagByteArray.Length; i++) tagByteArray[i] = 0; // Initialise array to nulls
  61.  
  62. // Convert the Byte Array to a String
  63. Encoding instEncoding = new ASCIIEncoding(); // NB: Encoding is an Abstract class // ************ To DO: Make a shared instance of ASCIIEncoding so we don't keep creating/destroying it
  64. // Copy "TAG" to Array
  65. byte[] workingByteArray = instEncoding.GetBytes("TAG");
  66. Array.Copy(workingByteArray, 0, tagByteArray, 0, workingByteArray.Length);
  67. // Copy Title to Array
  68. workingByteArray = instEncoding.GetBytes(this.Title);
  69. Array.Copy(workingByteArray, 0, tagByteArray, 3, workingByteArray.Length);
  70. // Copy Artist to Array
  71. workingByteArray = instEncoding.GetBytes(this.Artist);
  72. Array.Copy(workingByteArray, 0, tagByteArray, 33, workingByteArray.Length);
  73. // Copy Album to Array
  74. workingByteArray = instEncoding.GetBytes(this.Album);
  75. Array.Copy(workingByteArray, 0, tagByteArray, 63, workingByteArray.Length);
  76. // Copy Year to Array
  77. workingByteArray = instEncoding.GetBytes(this.Year);
  78. Array.Copy(workingByteArray, 0, tagByteArray, 93, workingByteArray.Length);
  79. // Copy Comment to Array
  80. workingByteArray = instEncoding.GetBytes(this.Comment);
  81. Array.Copy(workingByteArray, 0, tagByteArray, 97, workingByteArray.Length);
  82. // Copy Track and Genre to Array
  83. tagByteArray[126] = System.Convert.ToByte(this.Track);
  84. tagByteArray[127] = System.Convert.ToByte(this.GenreID);
  85.  
  86. // SAVE TO DISK: Replace the final 128 Bytes with our new ID3 tag
  87. FileStream oFileStream = new FileStream(this.filename, FileMode.Open);
  88. if (this.hasTag)
  89. oFileStream.Seek(-128, SeekOrigin.End);
  90. else
  91. oFileStream.Seek(0, SeekOrigin.End);
  92. oFileStream.Write(tagByteArray, 0, 128);
  93. oFileStream.Close();
  94. this.hasTag = true;
  95. }

Now when i presss the show mp3 buttons(this populates the list box with the mp3's in the directory) it gives me an error saying the folded acsess is denied. Am i seeting it up wrong. i want the program to reaad the mp3 tags as it populates the listbox


here is the code that populates the list box. this is where i gert the error


  1.  
  2. private void button2_Click(object sender, EventArgs e)
  3. {
  4.  
  5. label2.Visible = true;
  6. string sourceDir = textBox1.Text + "\\iPod_Control\\Music"; \\This is whhere they would type the drive
  7. string targetDir = folderBrowserDialog1.SelectedPath;
  8.  
  9. foreach (string d in Directory.GetDirectories(sourceDir))
  10. {
  11.  
  12. foreach (string f in Directory.GetFiles(d, fileType))
  13. {
  14. listBox1.Items.Add(Convert.ToString(f));//show whats going to be copied in the listbox before they rip it
  15.  
  16.  
  17.  
  18.  
  19. }
  20.  
  21. }

Any Ideas
Firefox
Ewido
Tune up windows
Get detailed system information
My Fixes

Member - Alliance of Security Analysis Professionals - Since 2006
Reply With Quote Quick reply to this message  
Join Date: Dec 2003
Posts: 55
Reputation: r0ckbaer is an unknown quantity at this point 
Solved Threads: 6
r0ckbaer r0ckbaer is offline Offline
Junior Poster in Training

Re: Quick directory question

 
0
  #10
Sep 13th, 2005
What's the value of textBox1.Text when you call that function?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C# Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC