1

I am trying to copy my file (fileName) into a folder call backup. When the file have been successfully copied into the backup folder, the modifySQLFile() will than read the file from backup folder and update the file in it and keep the original file (fileName) at it usual place. When the program run for the 2nd time, it will read the original file and create another copy to backup and updated the copied file and overwrite the previous copied file.

However, i am not sure what my codes went wrong and how to do the overwriting of the existing file. Kindly, help me out as i'm new to this.

public string fileName = "DEPOT-Pub_Sub_Combined (wo CardHolder).sql";

private void modifySQLFile()
{
    CopyFile();

    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(fileName, fileTexts);

    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);
}
Vertexwahn
  • 7,709
  • 6
  • 64
  • 90
Stella
  • 45
  • 8
  • The easiest way to overwrite the existing file is to first delete the old file, then replace it with your new file. – sorifiend Jun 02 '16 at 02:36
  • Can you describe in more details the error you are seeing? – Jeff Pang Jun 02 '16 at 02:38
  • with the code above, when i run the program, everything works fine but when i check the files in the folder, its there but not updated in the backup folder. However, it updates the original file instead. When i run the 2nd time, nothing seems to update at all. – Stella Jun 02 '16 at 02:47

1 Answers1

0

First to answer your problem. You are writing to the wrong file in the modifySQLFile() method. Should be destFileName instead of fileName

File.WriteAllLines(fileName, fileTexts);

Secondly, Assuming this file is not huge (<10MB), the better and easier way will be to read the contents of your original file into memory, modify the contents and then write it into backup. There is no need for CopyFile()

Jeff Pang
  • 161
  • 1
  • 6
  • hey thanks jeff, but my file is quite huge. 100 over lines of SQL script to run – Stella Jun 02 '16 at 03:10
  • 100 lines of text is quite small. 10MB will probably be around 100k or more lines. Btw, your code is already reading the file into memory like this `File.ReadAllLines` However, should you ever encounter having to manipulate large files in future, you should use File Streams instead of reading it into memory. – Jeff Pang Jun 02 '16 at 03:11
  • Hey Jeff, can i ask you another question. Now my file can be save properly in the backup found. I would then 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, it will be DEPOT-Pub_Sub_Combined (wo CardHolder) 1.sql. and when i run the program 2nd time, it will create again copy the original to the backup and update that file and save it name as whatever the textbox.text value is. How can this be done? – Stella Jun 02 '16 at 06:23
  • You rename the file by using `File.Move` http://stackoverflow.com/questions/3218910/rename-a-file-in-c-sharp. To create rolling copies, examine the files in backup directory. Easy way is to count the number of files using `Directory.GetFiles(dirPath,"*.sql)` and then renaming it by that next number. However this may not be the most reliable since you can easily make the mistake and the file names might conflict (e.g manually removing a file from backup folder) so the best way is to use regex to find the number from the file name (i.e the "1" in "abc.1.sql") and increment accordingly. – Jeff Pang Jun 02 '16 at 07:46
  • Hey Jeff, i tried using this code! File.Move(destFileName, "DEPOT-Pub_Sub_Combined (wo CardHolder)" + textBox1.Text + ".sql"); it works and save the files, but the problem is, it does not save into the backup folder i created. It actually appear outside of the backup folder. – Stella Jun 02 '16 at 09:21
  • Your new file name should include the backup folder name i.e "backup\\" + "Depot ... remember that all relative path are in relation to your programs location – Jeff Pang Jun 02 '16 at 09:24
  • string destFileName = @"backup\DEPOT-Pub_Sub_Combined (wo CardHolder).sql" ; i already declare my destFilename as this.. but it does not work. – Stella Jun 02 '16 at 09:31
  • Both parameters need to specify "backup" i.e destfilename and the newfilename – Jeff Pang Jun 02 '16 at 09:34
  • Thanks jeff.. my careless mistake on that.. THANK YOU~!! its solve now.. =) – Stella Jun 02 '16 at 09:36