1,080,445 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

Posts by jumboora

jumboora
Light Poster
30 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Hi, I have two text files. Textfile1 has data line by line (50000 lines) and in each line, there are 500 numeric values. Textfile2 has only one column (only numeric data) which matches the first column of Textfile1. Textfile2 may have 5000 lines (Each line has one numeric value).
Now my task is to read Textfile2 line by line, read its numeric value and match with the first numeric value of Textfile1, If it matches, then it should copy the whole line and paste it to Textfile3. Please help me to do this. I am new to C#, so just tried in the following program.

static void Main(string[] args)
        {
            StringBuilder strFile = new StringBuilder();

            using (StreamReader reader = new StreamReader(@"F:\Experiment 2\CM\CM Model Files\cm.txt"))
            {
                using (StreamWriter outfile = new StreamWriter(@"F:\Experiment 2\CM\CM Model Files\cm.test"))
                    for (string line = reader.ReadLine(); line != null; line = reader.ReadLine())
                    {

                        char[] charsToTrim = { ' ', '\r', '\n' };
                        string kkj = (line.TrimEnd(charsToTrim));

                        string[] words = kkj.Split(' ');
                        if (words[0] == "3010")
                            line.CopyTo();

                        strFile.AppendLine();
                        outfile.Write(strFile.ToString());
                        strFile.Clear();
                    }
            }
        }
jumboora
Light Poster
30 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
jumboora
Light Poster
30 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Please tell me how to skip (not trim or remove) first word of each line in a text file?
Actually i want to put indexing on each word except first word of each line.(I want to write that first word on each line but without indexing).
Thanks

jumboora
Light Poster
30 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Please tell me one more thing, how to process multiple text files in a directory and storing result in only one text file.
I tried to change the above code, but it has some problem.

 static void Main(string[] args)
        {
            string[] files = Directory.GetFiles(@"F:\New folder (4)\", "*.txt", SearchOption.AllDirectories);

            StringBuilder strFile = new StringBuilder();


            foreach (string fri in files)
            {

                //    using (StreamReader reader = new StreamReader(@"F:\Experiment 1\CMTRAINNEW\*.txt"))
                using (StreamReader reader = new StreamReader(fri))
                {
                    using (StreamWriter outfile = new StreamWriter(@"F:\Experiment 1\SVMTrainFormatFilesLF\+1.txt"))

                        for (string line = reader.ReadLine(); line != null; line = reader.ReadLine())
                        {

                            string[] words = kkj.Split(' ');
                            string temp = "";
                            int iCounter = 1;

                            foreach (string word in words)
                            {
                              strFile.Append(iCounter++ + ":" + word + " ");
                                }
                            strFile.AppendLine();                            
                            outfile.Write(strFile.ToString());
                         //   strFile.Clear();
                        }

                }


            }

        }
jumboora
Light Poster
30 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Thank you so much for your kind help.

jumboora
Light Poster
30 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Now i have successfully removed the indexing by using indexof and substring method.but colon is still there with every numeric value. Here is the code. How to remove the colon now?

static void Main(string[] args)
        {
           StringBuilder strFile = new StringBuilder();

           using (StreamReader reader = new StreamReader(@"F:\New folder (4)\xxx.txt"))
           {
               for (string line = reader.ReadLine(); line != null; line = reader.ReadLine())
               {
                   char[] charsToTrim = { ' ', '\r', '\n' };
                   string kkj = (line.TrimEnd(charsToTrim));
                   string[] words = kkj.Split(' ');

                   foreach (string word in words)
                   {
                       int x = word.IndexOf(':');

                       string d = word.Substring(x);

                       strFile.Append(d + " ");
                   }
                   strFile.AppendLine();
               }
           }
            using (StreamWriter outfile = new StreamWriter(@"F:\New folder (4)\xyz1.txt"))
            {
                outfile.Write(strFile.ToString());
            }
        }
jumboora
Light Poster
30 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Thanks for replying and providing me a hint. I have tried according to your suggestion. Here is my try. Please help me to improve it.
I am new in c#, i know there are some mistakes, plz don't mind and help me to solve.

static void Main(string[] args)
        {
            StringBuilder strFile = new StringBuilder();
            using (StreamReader reader = new StreamReader(@"F:\xyz.txt"))
            {

                for (string line = reader.ReadLine(); line != null; line = reader.ReadLine())
                {

                    string[] words = line.Split(' ');
                    for (int i = 0; i < 225; i++)
                    {
                        int x = words[i].IndexOf(':');

                        string d = words[i].Substring(x);

                    }
                    strFile.AppendLine();
                }
                using (StreamWriter outfile = new StreamWriter(@"F:\xyz1.txt"))
                {
                    outfile.Write(strFile.ToString());
                }
            }
        }
jumboora
Light Poster
30 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Hi,
I have a text file in which there are 10000 lines. There are 225 numerical values on each line and each numerical value is followed by a index number and a colon e.g., 1:0.021354 2:0.125432 3:451321 ...... 225:0.001254.
Now I want to remove this indexing. I know how to add indexing but could not perform to remove that. Please help me in solving this problem. Thanks
Here is the code for adding indexing.

static void Main(string[] args)
        {
            string[] files = Directory.GetFiles(@"F:\New folder", "*.txt", SearchOption.AllDirectories);

            StringBuilder strFile = new StringBuilder();

            foreach (string file in files)
            {
                using (StreamReader sr = new StreamReader(file))
                {
                    string s = Path.ChangeExtension(file, null);
                    strFile.AppendFormat(s.Substring(s.LastIndexOf(@"\") + 1) + " ");
              //      string s = Path.GetFileName("-1");
              //      strFile.AppendFormat(s + " ");
                    char[] charsToTrim = { ' ', '\r', '\n' };
                    string kkj = (sr.ReadToEnd().TrimEnd(charsToTrim));
                    string[] words = kkj.Split(' ');
                    string temp = "";

                    int iCounter = 1;

                    foreach (string word in words)
                    {
                        if (word.Contains("-0.000000"))
                        {
                            temp = word.Replace("-0.000000", "");

                            strFile.Append(temp);

                            iCounter++;
                            continue;
                        }

                        else if (word.Contains("0.000000"))
                        {
                            temp = word.Replace("0.000000", "");


                            strFile.Append(temp);

                            iCounter++;
                            continue;
                        }

                        strFile.Append(iCounter++ + ":" + word + " ");

                    }
                    strFile.AppendLine();
                }
            }
            using (StreamWriter outfile = new StreamWriter(@"F:\New folder (3)\-1.train"))
            {
                outfile.Write(strFile.ToString());
            }
        }
jumboora
Light Poster
30 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Thanks MIKE....Its working

jumboora
Light Poster
30 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

It is working fine but still copying each time 2 consecutive files, but i want to copy 1 each time and then skip 2.

jumboora
Light Poster
30 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

If we change the value of skipNum to 2 then it first copies 2 files into the directory and skip 1.
But I want to read (copy) first and then skip 2 and then read (copy) 4th and then skip 2....

jumboora
Light Poster
30 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
jumboora
Light Poster
30 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Hi, I want to read source directory and copy each file to the destination by skipping after each file based on interval value i.e., if i set the value of interval 2, then it should skip two files everytime.

For Example: If there are 5 files in a directory, 1.txt, 2.txt, 3.txt, 4.txt and 5.txt and i have set the interval = 1, then my program should skip one text file after each file and copy 1, 3 and 5.txt to the destination.
I have a code which simply copies from source to destination.

static void Main(string[] args)
        {
            string fileName;
            string destFile;
            string sourcePath = @"E:\Source";
            string targetPath = @"E:\Destination";
            // To copy all the files in one directory to another directory.
            string[] files = System.IO.Directory.GetFiles(sourcePath);
            // Copy the files and overwrite destination files if they already exist.
            foreach (string s in files)
            {
                // Use static Path methods to extract only the file name from the path.
                fileName = System.IO.Path.GetFileName(s);
                destFile = System.IO.Path.Combine(targetPath, fileName);
                System.IO.File.Copy(s, destFile, true);
            }
        }
jumboora
Light Poster
30 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Its working. Thank you so much Mike.

jumboora
Light Poster
30 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
jumboora
Light Poster
30 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

No, Please leave this option on run time. I will set the filename each time.
Thanks

jumboora
Light Poster
30 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Sorry Mike, I was doing some mistake. Your solution is absolutely right.
Now, it is my last question related to this topic. If we want to make reverse this process.
For example, Reading a directory in which jpg files are present like this (345001.jpg, 345002.jpg,.....345200.jpg and 346001.jpg, 346002.jpg,,,,,346200.jpg). This time we have to copy only the base file names to the text file i.e., only 345 and 346 should be written to the text file.
Thanks

jumboora
Light Poster
30 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Hi Mike, Sorry your solution is not working. I think you didn't get my point.

jumboora
Light Poster
30 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Please tell me one more thing.
Video ID's are like 111001, 111002, 111003 in the source directory. Here you can see the first three numbers are same but the last three numbers varying starting from 001 to 003. It can also go to 200. But in videoID.txt, the first three numbers are given, For Example, If 111 is given. Then It should search in the source directory and copy 111001, 111002,,,,,,111200.
Here I mean to say, In the source directory video's will always be given in this format where the last three numbers will be added in ascending order with the actual videoID.

Thanks!

jumboora
Light Poster
30 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
 
© 2013 DaniWeb® LLC
Page generated in 0.1768 seconds using 2.6MB