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



## angelr82 (Jun 15, 2010)

Hello everyone,

Take it easy on me, i am a total noobie 

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:

```
{
                        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++;
                        }
                    }
```


----------



## ckphilli (Apr 29, 2006)

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 (Jun 15, 2010)

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.


----------



## ckphilli (Apr 29, 2006)

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...

```
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 (Jun 15, 2010)

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 (Jun 15, 2010)

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.


```
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++;
```


----------



## ckphilli (Apr 29, 2006)

Awesome, glad you got it worked out.


----------

