944,076 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 6857
  • C# RSS
You are currently viewing page 1 of this multi-page discussion thread
Sep 11th, 2005
0

Quick directory question

Expand Post »
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
C# Syntax (Toggle Plain Text)
  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.
Team Colleague
Reputation Points: 84
Solved Threads: 99
<Insert title here>
tayspen is offline Offline
1,542 posts
since Jul 2005
Sep 11th, 2005
0

Re: Quick directory question

C# Syntax (Toggle Plain Text)
  1. string[] files = Directory.GetFiles(sourceDirectoryName, "*.mp3");
Reputation Points: 13
Solved Threads: 6
Junior Poster in Training
r0ckbaer is offline Offline
55 posts
since Dec 2003
Sep 11th, 2005
0

Re: Quick directory question

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?
Team Colleague
Reputation Points: 84
Solved Threads: 99
<Insert title here>
tayspen is offline Offline
1,542 posts
since Jul 2005
Sep 11th, 2005
0

Re: Quick directory question

ok, this is a function which scans a sourcedir recursively and copies the found files to the target dir:
C# Syntax (Toggle Plain Text)
  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:
C# Syntax (Toggle Plain Text)
  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!)
Reputation Points: 13
Solved Threads: 6
Junior Poster in Training
r0ckbaer is offline Offline
55 posts
since Dec 2003
Sep 11th, 2005
0

Re: Quick directory question

thank you, that is exactly what i wanted. thanks again.
Team Colleague
Reputation Points: 84
Solved Threads: 99
<Insert title here>
tayspen is offline Offline
1,542 posts
since Jul 2005
Sep 11th, 2005
0

Re: Quick directory question

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.
Team Colleague
Reputation Points: 84
Solved Threads: 99
<Insert title here>
tayspen is offline Offline
1,542 posts
since Jul 2005
Sep 13th, 2005
0

Re: Quick directory question

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
Reputation Points: 13
Solved Threads: 6
Junior Poster in Training
r0ckbaer is offline Offline
55 posts
since Dec 2003
Sep 13th, 2005
0

Re: Quick directory question

Quote 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.
Team Colleague
Reputation Points: 84
Solved Threads: 99
<Insert title here>
tayspen is offline Offline
1,542 posts
since Jul 2005
Sep 13th, 2005
0

Re: Quick directory question

oks o i set this

C# Syntax (Toggle Plain Text)
  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


C# Syntax (Toggle Plain Text)
  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
Team Colleague
Reputation Points: 84
Solved Threads: 99
<Insert title here>
tayspen is offline Offline
1,542 posts
since Jul 2005
Sep 13th, 2005
0

Re: Quick directory question

What's the value of textBox1.Text when you call that function?
Reputation Points: 13
Solved Threads: 6
Junior Poster in Training
r0ckbaer is offline Offline
55 posts
since Dec 2003

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Urgent question about Updates
Next Thread in C# Forum Timeline: New Thread is not allowing proper Paint Events





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC