0

My file can be save properly in the backup found. I would like to rename the backup file name,something like "DEPOT-Pub_Sub_Combined (wo CardHolder)" + textbox1.text + ".sql". For example the textbox1 value is 1,So it will be DEPOT-Pub_Sub_Combined (wo CardHolder) 1.sql and when I run the program 2nd time, It will create copy of the original backup and update that file and save It's name as whatever the textbox.text value is. How can this be done? what should i do?

private void modifySQLFile()
{
    CopyFile();
   string destFileName = @"backup\DEPOT-Pub_Sub_Combined (wo CardHolder).sql" ;
   string[] fileTexts = File.ReadAllLines(@"backup\DEPOT-Pub_Sub_Combined (wo CardHolder).sql");
   int counter = 0;

    //File processing 
    foreach (string line in fileTexts)
    {
        //only process non-comments line
        if (line.StartsWith("--") == false)
        {
            //replace instances of server name
            if (line.Contains(SERVERNAME) == true)
            {
                fileTexts[counter] = fileTexts[counter].Replace(SERVERNAME, textBox1.Text);            
            }

            if (line.Contains(ACCESSID) == true)
            {
                fileTexts[counter] = fileTexts[counter].Replace(ACCESSID, textBox2.Text);
            }

            if(line.Contains(NETWORKID) == true)
            {
                fileTexts[counter] = fileTexts[counter].Replace(NETWORKID, textBox2.Text);
            }


        }
        counter++;
    }
    //update file
    File.WriteAllLines(destFileName, fileTexts);
File.Move(destFileName, "DEPOT-Pub_Sub_Combined (wo CardHolder)" + textBox1.Text + ".sql");
    MessageBox.Show("Completed!");
}

private void CopyFile()
{

    string targetPath = @"backup";
    string destFile = Path.Combine(targetPath, fileName);

    if(!Directory.Exists(targetPath))
    {
        Directory.CreateDirectory(targetPath);
    }

   File.Copy(fileName, destFile, true);
}
Stella
  • 45
  • 8
  • your code looks like it is doing what you describe in your question. The only thing that I miss is the creation of the backup filename like in your description. Where do you initialize `fileName` from your `CopyFile` method? What exactly are you still having problems with? – Mong Zhu Jun 02 '16 at 07:34
  • Hi Mong Zhu, my problem now is, the files are created. But when it creates the copies it does not goes into the backup folder. – Stella Jun 02 '16 at 08:35
  • it still remains to clarify how `fileName` is created. Might it be that you overwrite the file in the backup folder instead of creating new ones? or did not a single file made it into the folder? – Mong Zhu Jun 02 '16 at 08:45
  • 'fileName' i created as a public string. string fileName = "DEPOT-Pub_Sub_Combined (wo CardHolder).sql"; – Stella Jun 02 '16 at 08:52
  • ok when I try your `CopyFile` method it works as intended. It copies the file to the folder. Since the overwrite property in the call of `File.Copy(fileName, destFile, true);` is set to true I get the feeling that your filename is not changing and you overwrite the old one – Mong Zhu Jun 02 '16 at 08:52
  • doesn't it has to change according to the entry of the textbox as you wrote in your question? – Mong Zhu Jun 02 '16 at 08:53
  • I need those copies file to be the back up .. for example the file name DEPOT-Pub_Sub_Combined (wo CardHolder) 1 .sql" and DEPOT-Pub_Sub_Combined (wo CardHolder) 2.sql"; and DEPOT-Pub_Sub_Combined (wo CardHolder) 3.sql"; all this 3 files will be created and should be inside the backup folder i created. Now this 3 files is not in the backup folder, its outside of the backup folder. Im not sure where have i do wrong. I updated my codes already – Stella Jun 02 '16 at 09:05
  • You load the original file from the backup folder change it and save it with a the same name inside the backup folder. with `File.Move(destFileName, "DEPOT-Pub_Sub_Combined (wo CardHolder)" + textBox1.Text + ".sql");` you copy the new file outside the backup folder. The second argument is the destination the first one is the source! – Mong Zhu Jun 02 '16 at 09:15
  • have you tried the solution that I suggested? – Mong Zhu Jun 02 '16 at 09:19
  • Yes i tried. Does not work. Does not have multiple files and the filename does not include the textbox1.text value.. – Stella Jun 02 '16 at 09:22
  • it just mainly overwrite the file – Stella Jun 02 '16 at 09:30
  • actually it does include the textbox: `// make a new filename each time this method is executed string newFileName = @"DEPOT-Pub_Sub_Combined (wo CardHolder)" + textbox1.text + ".sql" // give it as parameter so that the copied file gets a new name CopyFile(newFileName);` – Mong Zhu Jun 02 '16 at 09:35
  • Okay, thank you. My problem is solve already. I got what i want. Thank you so much – Stella Jun 02 '16 at 09:38
  • how did you solve it? it would be good to know for the people who encounter in future the same problem, so that they can find also a solution – Mong Zhu Jun 02 '16 at 09:45

2 Answers2

0

I think you should use the "File.Move" method.

See more at MSDN

In your example it would be:

File.Move(fileName, "DEPOT-Pub_Sub_Combined (wo CardHolder)" + textbox1.text + ".sql");

Greetz,
Jordan Kniest

Jordan Kniest
  • 200
  • 1
  • 6
0

try this please :

private void modifySQLFile()
{
    // make a new filename each time this method is executed
    string newFileName = @"DEPOT-Pub_Sub_Combined (wo CardHolder)" + textbox1.text + ".sql"
    // give it as parameter so that the copied file gets a new name
    CopyFile(newFileName);

    string destFileName = @"backup\DEPOT-Pub_Sub_Combined (wo CardHolder).sql" ;
    string[] fileTexts = File.ReadAllLines(@"backup\DEPOT-Pub_Sub_Combined (wo CardHolder).sql");
   int counter = 0;

    //File processing 
    foreach (string line in fileTexts)
    {
        //only process non-comments line
        if (line.StartsWith("--") == false)
        {
            //replace instances of server name
            if (line.Contains(SERVERNAME) == true)
            {
                fileTexts[counter] = fileTexts[counter].Replace(SERVERNAME, textBox1.Text);            
            }

            if (line.Contains(ACCESSID) == true)
            {
                fileTexts[counter] = fileTexts[counter].Replace(ACCESSID, textBox2.Text);
            }

            if(line.Contains(NETWORKID) == true)
            {
                fileTexts[counter] = fileTexts[counter].Replace(NETWORKID, textBox2.Text);
            }


        }
        counter++;
    }
    //update file
    File.WriteAllLines(destFileName, fileTexts);
    MessageBox.Show("Completed!");
}

// the new filename should go for the copy
private void CopyFile(string newFileName)
{

    string targetPath = @"backup";
    string destFile = Path.Combine(targetPath, newFileName);

    if(!Directory.Exists(targetPath))
    {
        Directory.CreateDirectory(targetPath);
    }

   File.Copy(fileName, destFile, true);
}
Mong Zhu
  • 23,309
  • 10
  • 44
  • 76