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

try
            {
                Directory.CreateDirectory(targerDirectoryName);

                string[] files = Directory.GetFiles(sourceDirectoryName);

                foreach (string fileName in files)
                    File.Copy(fileName, targerDirectoryName + Path.DirectorySeparatorChar + Path.GetFileName(fileName));

                string[] directories = Directory.GetDirectories(sourceDirectoryName);

                foreach (string directory in directories)
                    CopyDirectory(directory, targerDirectoryName + Path.DirectorySeparatorChar + Path.GetFileName(directory));
            }

            catch
            {
                //
            }

if you no how i can specify wha i want to copy that be great.

Recommended Answers

All 20 Replies

string[] files = Directory.GetFiles(sourceDirectoryName, "*.mp3");

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?

ok, this is a function which scans a sourcedir recursively and copies the found files to the target dir:

/// <summary>
		/// 
		/// 
		/// </summary>
		/// <param name="sourceDir">source directory</param>
		/// <param name="targetDir">target directory</param>
		/// <param name="targetDir">file type (mp3)</param>
		static void Mp3FileCopy(string sourceDir, string targetDir, string fileType)
		{
			try
			{
				foreach (string d in Directory.GetDirectories(sourceDir))
				{
					foreach (string f in Directory.GetFiles(d, fileType))
					{
						File.Copy(f, 
							targetDir + "\\" + new FileInfo(f).Name, 
							false);
					}

					Mp3FileCopy(d, targetDir, fileType);
				}
			}
			catch (System.Exception excpt)
			{
				Console.WriteLine(excpt.Message);
			}
		}

you call it like this:

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!)

thank you, that is exactly what i wanted. thanks again.

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.

oks o i set this

public void Read()
        {
            File.SetAttributes(textBox1.Text + "\\iPod_Control\\Music", FileAttributes.Normal);
            // Read the 128 byte ID3 tag into a byte array
            FileStream oFileStream;
            oFileStream = new FileStream(this.filename, FileMode.Open);
            byte[] bBuffer = new byte[128];
            oFileStream.Seek(-128, SeekOrigin.End);
            oFileStream.Read(bBuffer, 0, 128);
            oFileStream.Close();

            // Convert the Byte Array to a String
            Encoding instEncoding = new ASCIIEncoding();   // NB: Encoding is an Abstract class
            string id3Tag = instEncoding.GetString(bBuffer);

            // If there is an attched ID3 v1.x TAG then read it 
            if (id3Tag.Substring(0, 3) == "TAG")
            {
                this.Title = id3Tag.Substring(3, 30).Trim();
                this.Artist = id3Tag.Substring(33, 30).Trim();
                this.Album = id3Tag.Substring(63, 30).Trim();
                this.Year = id3Tag.Substring(93, 4).Trim();
                this.Comment = id3Tag.Substring(97, 28).Trim();

                // Get the track number if TAG conforms to ID3 v1.1
                if (id3Tag[125] == 0)
                    this.Track = bBuffer[126];
                else
                    this.Track = 0;
                this.GenreID = bBuffer[127];

                this.hasTag = true;
                // ********* IF USED IN ANGER: ENSURE to test for non-numeric year
            }
            else
            {
                this.hasTag = false;
            }
        }

        public void updateMP3Tag()
        {
            // Trim any whitespace
            this.Title = this.Title.Trim();
            this.Artist = this.Artist.Trim();
            this.Album = this.Album.Trim();
            this.Year = this.Year.Trim();
            this.Comment = this.Comment.Trim();

            // Ensure all properties are correct size
            if (this.Title.Length > 30) this.Title = this.Title.Substring(0, 30);
            if (this.Artist.Length > 30) this.Artist = this.Artist.Substring(0, 30);
            if (this.Album.Length > 30) this.Album = this.Album.Substring(0, 30);
            if (this.Year.Length > 4) this.Year = this.Year.Substring(0, 4);
            if (this.Comment.Length > 28) this.Comment = this.Comment.Substring(0, 28);

            // Build a new ID3 Tag (128 Bytes)
            byte[] tagByteArray = new byte[128];
            for (int i = 0; i < tagByteArray.Length; i++) tagByteArray[i] = 0; // Initialise array to nulls

            // Convert the Byte Array to a String
            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
            // Copy "TAG" to Array
            byte[] workingByteArray = instEncoding.GetBytes("TAG");
            Array.Copy(workingByteArray, 0, tagByteArray, 0, workingByteArray.Length);
            // Copy Title to Array
            workingByteArray = instEncoding.GetBytes(this.Title);
            Array.Copy(workingByteArray, 0, tagByteArray, 3, workingByteArray.Length);
            // Copy Artist to Array
            workingByteArray = instEncoding.GetBytes(this.Artist);
            Array.Copy(workingByteArray, 0, tagByteArray, 33, workingByteArray.Length);
            // Copy Album to Array
            workingByteArray = instEncoding.GetBytes(this.Album);
            Array.Copy(workingByteArray, 0, tagByteArray, 63, workingByteArray.Length);
            // Copy Year to Array
            workingByteArray = instEncoding.GetBytes(this.Year);
            Array.Copy(workingByteArray, 0, tagByteArray, 93, workingByteArray.Length);
            // Copy Comment to Array
            workingByteArray = instEncoding.GetBytes(this.Comment);
            Array.Copy(workingByteArray, 0, tagByteArray, 97, workingByteArray.Length);
            // Copy Track and Genre to Array
            tagByteArray[126] = System.Convert.ToByte(this.Track);
            tagByteArray[127] = System.Convert.ToByte(this.GenreID);

            // SAVE TO DISK: Replace the final 128 Bytes with our new ID3 tag
            FileStream oFileStream = new FileStream(this.filename, FileMode.Open);
            if (this.hasTag)
                oFileStream.Seek(-128, SeekOrigin.End);
            else
                oFileStream.Seek(0, SeekOrigin.End);
            oFileStream.Write(tagByteArray, 0, 128);
            oFileStream.Close();
            this.hasTag = true;
        }

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

private void button2_Click(object sender, EventArgs e)
        {

            label2.Visible = true;
            string sourceDir = textBox1.Text + "\\iPod_Control\\Music";  \\This is whhere they would type the drive
            string targetDir = folderBrowserDialog1.SelectedPath;

            foreach (string d in Directory.GetDirectories(sourceDir))
            {

                foreach (string f in Directory.GetFiles(d, fileType))
                {
                    listBox1.Items.Add(Convert.ToString(f));//show whats going to be copied in the listbox before they rip it
               



                }

            }

Any Ideas

What's the value of textBox1.Text when you call that function?

What's the value of textBox1.Text when you call that function?

at that time it contains the path of the file to open. H:\ipod_contol\music

at that time it contains the path of the file to open. H:\ipod_contol\music

eh im not sure if thats what u mean...

well if textbox1.text is H:\ipod_contol\music and then you cocatenate again \ipod_contol\music to it gives : H:\ipod_contol\music\ipod_contol\music, so it's normal that te folder access is denied, because it doesn't exist

well if textbox1.text is H:\ipod_contol\music and then you cocatenate again \ipod_contol\music to it gives : H:\ipod_contol\music\ipod_contol\music, so it's normal that te folder access is denied, because it doesn't exist

eh how right your are lemme see hold ojn

ok so this is now what i have

public void Read()
        {
            this.filename = @"H:\ipod_control\music";
            // Read the 128 byte ID3 tag into a byte array
            FileStream oFileStream;
            oFileStream = new FileStream(this.filename, FileMode.Open);
            byte[] bBuffer = new byte[128];
            oFileStream.Seek(-128, SeekOrigin.End);
            oFileStream.Read(bBuffer, 0, 128);
            oFileStream.Close();

            // Convert the Byte Array to a String
            Encoding instEncoding = new ASCIIEncoding();   // NB: Encoding is an Abstract class
            string id3Tag = instEncoding.GetString(bBuffer);

            // If there is an attched ID3 v1.x TAG then read it 
            if (id3Tag.Substring(0, 3) == "TAG")
            {
                this.Title = id3Tag.Substring(3, 30).Trim();
                this.Artist = id3Tag.Substring(33, 30).Trim();
                this.Album = id3Tag.Substring(63, 30).Trim();
                this.Year = id3Tag.Substring(93, 4).Trim();
                this.Comment = id3Tag.Substring(97, 28).Trim();

                // Get the track number if TAG conforms to ID3 v1.1
                if (id3Tag[125] == 0)
                    this.Track = bBuffer[126];
                else
                    this.Track = 0;
                this.GenreID = bBuffer[127];

                this.hasTag = true;
                // ********* IF USED IN ANGER: ENSURE to test for non-numeric year
            }
            else
            {
                this.hasTag = false;
            }
        }

that was for the read part. i set the filename ahs H:\....

now this is for the button to how mp3's

try
            {
                label2.Visible = true;
                string sourceDir = textBox1.Text + "\\iPod_Control\\Music";  //redeclare
                string targetDir = folderBrowserDialog1.SelectedPath;

                foreach (string d in Directory.GetDirectories(sourceDir))
                {


                    foreach (string f in Directory.GetFiles(d, fileType))
                    {
                        listBox1.Items.Add(Convert.ToString(f));//show whats going to be copied in the listbox
                        Read();

                       

                    }

                }

            }

            catch
            {

                frmerror frm = new frmerror();
                frm.ShowDialog(this);  // show notes (EDIT: notes canceled now show about)
                frm.Dispose();
            }

do i not have REad(); in the right spot. cuz now its given me my the first song (its still not reading the tags of em) then giving me my catch.

I must be doing soomthing wrong


EDIT: see i dont think its scanning the whol music directory then reading the tags then showing em' cuz its only finding one....

lol, of course it gives you an exception:

this.filename = @"H:\ipod_control\music";
            // Read the 128 byte ID3 tag into a byte array
            FileStream oFileStream;
            oFileStream = new FileStream(this.filename, FileMode.Open);

the FileStream class awaits a FILE and not a DIRECTORY!

lol, of course it gives you an exception:

this.filename = @"H:\ipod_control\music";
            // Read the 128 byte ID3 tag into a byte array
            FileStream oFileStream;
            oFileStream = new FileStream(this.filename, FileMode.Open);

the FileStream class awaits a FILE and not a DIRECTORY!

and that makes sense how do i make it await, a directory

uh?

do this.

listBox1.Items.Add(Convert.ToString(f));
this.filename = f;
Read();

and in the Read() function remove the line : this.filename = H\ipoed\etc... ans also the concat thinggy

uh?

do this.

listBox1.Items.Add(Convert.ToString(f));
this.filename = f;
Read();

and in the Read() function remove the line : this.filename = H\ipoed\etc... ans also the concat thinggy

concat...lol?

remove line : this.filename = blabla... from the Read() func

remove line : this.filename = blabla... from the Read() func

i did it still doesnt read the corect tags and then display them in the listbox

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.