Solved: C# WPF - Export Text files and increment every nTH file to new directory

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

angelr82

Angel
Thread Starter
Joined
Jun 15, 2010
Messages
88
Hello everyone,

Take it easy on me, i am a total noobie :confused:

I have a simple WPF App in C# that exports data from SQL to individual .TXT files. It works great except it dumps all the data in one single directory. I am trying to figure out how to have it create the TXT files in a different directory every Nth file.

For example - if i have 100,000 text files it dumps them in one DIR and ideally i would like it to create 100 files and then increment the DIR .

So it would do something like
C:\txtdumps\text001 (100 txt files)
C:\txtdumps\text002 (100 txt files)
C:\txtdumps\text003 (100 txt files)
C:\txtdumps\text004 (100 txt files)
C:\txtdumps\text005 (100 txt files)
C:\txtdumps\text006 (100 txt files)
C:\txtdumps\text007 (100 txt files)
and so on.

any suggestions on how i can accomplish this?

This is what it basically looks like:
Code:
                    {
                        FileName = rdr.GetString(0);
                        text = rdr.GetString(1);
                        if (string.IsNullOrEmpty(text)) continue;
                        filePath = filePathOut + FileName.ToString() + ".txt";
                        if (!System.IO.File.Exists(filePath))
                        {
                            using (sw = new StreamWriter(filePath))
                            {
                                sw.Write(text);
                                sw.Flush();
                                sw.Close();
                            }
                            count++;
                        }
                    }
 
Joined
Apr 29, 2006
Messages
4,393
You have all the pieces, just need to append filepathout with the appropriate increment when the condition is met.

Edit: If this is going to be a continuous thing you'll need to get a count of files in the latest directory.
 

angelr82

Angel
Thread Starter
Joined
Jun 15, 2010
Messages
88
so that is where im lost. Not sure how to make it increment or set the condition in C#. I get a total count at the end but having trouble seeing how to create the condition and then increment. I guess i need to search for some code examples.
 
Joined
Apr 29, 2006
Messages
4,393
Several different ways to go about it. What I would do is read the directory and get a count. LIke this:

http://stackoverflow.com/questions/16193126/counting-the-number-of-files-in-a-folder-in-c-sharp

Then, as you work through the files, if the count exceeds your limit, bump your directory by 1. So you would want to split your path up...
Code:
string topdir = @"C:\txtdumps\"
string subdir = "text" (I split this out in case you ever need a new sub-directory)
int dirinc = 1

string filepathout = topdir+subdir+dirinc.ToString();
Looks like you will also want to research leading zeroes...
https://msdn.microsoft.com/en-us/library/dd260048(v=vs.110).aspx

So:
1) Count the files you have, set the first filepathout
2) Iterate, move files, etc, keep a count on the directory, write a condition to where if directorycount equals 100, add one dirinc and make a new filepathout
3)I would make sure and put topdir and subdir in a config file so you can change them without recompiling(app config).
 

angelr82

Angel
Thread Starter
Joined
Jun 15, 2010
Messages
88
sadly this is over my head (obviously) the code i have now runs a SQL command that pulls all data records then just writes to one single folder.

I added:
int fCount = Directory.GetFiles(filePathOut, "*.txt", SearchOption.AllDirectories).Length;

and
filePath = filePathOut + @"\" + subDIR + @"\" + dirinc + @"\" + FileName.ToString() + ".txt";

but still wrote to single DIR
then i played with my SQL to get a user defined int via a text box to select just the top nTH records but that made things worse. lol
 

angelr82

Angel
Thread Starter
Joined
Jun 15, 2010
Messages
88
Ok so got it figured out, here is the code that runs good just in case someone else in the future needs help with something similar.

Code:
if (count >= increment)
                {
                    subDIR++;
                    di = Directory.CreateDirectory(System.IO.Path.Combine(parentDirectory, subDIR.ToString()));
                    count = 0;
                }

                string fileName = FileName.ToString() + ".txt";
                using (StreamWriter sw = new StreamWriter(System.IO.Path.Combine(di.FullName, fileName)))
                {
                    sw.Write(text);
                    sw.Flush();
                    sw.Close();
                }

                count++;
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Members online

Top