0

UPDATE: I just got another solution here: https://stackoverflow.com/a/1078898/14311474

I'm having a little problem with incrementing the current existing file.

instead of incrementing the current count, the filename becomes like this.

outputFileName(1).mp4
outputFileName(1)(1).mp4
outputFileName(1)(1)(1).mp4

I want the filename become like this:

outputFileName(1).mp4
if exists:
outputFileName(2).mp4
if exists:
outputFileName(3).mp4

Here's my code:

private void OverWriteFile(string OutputFileName)
{
    bool r = false;
    if (!string.IsNullOrEmpty(OutputFileName)) {
        if (File.Exists(OutputFileName)) {
            var dr = MessageBox.Show($"The file '{finalOutputVideoFilePath}' already exists. Overwrite? else \nfile name will be incremented", "Overwriting",
                                    MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.No) { 
            
            OutputFileName.GetNextFileName();
            
            }
            else {
                File.Delete(OutputFileName);
            }
        }
    }
    return r;
}

public static string GetNextFileName(this string fileName)
{
    string extension = Path.GetExtension(fileName);
    string pathName = Path.GetDirectoryName(fileName);
    string fileNameOnly = Path.Combine(pathName, Path.GetFileNameWithoutExtension(fileName));
    int i = 1;
    while (File.Exists(fileName))
    {
        Interlocked.Increment(ref i);
        fileName = string.Format("{0}({1}){2}", fileNameOnly, i, extension);
    }
    return fileName;
}
Maherz123
  • 67
  • 5
  • You probably want to parse the file name, extract the number and then work with that instead of just slapping on another set of parenthesis. – Joelius Oct 17 '20 at 10:45
  • this code seems to work for me, is there more info you can post to help recreate what you have? – audzzy Oct 17 '20 at 11:04
  • *"UPDATE: I just got another solution here: https://stackoverflow.com/a/1078898/14311474"* Is that solution working for you? – Cid Oct 17 '20 at 11:08
  • 1
    You should pass always the root name to the method. If you pass a postfixed name, it will be considered as a root. Cid's solution seems to work the same way. As well as [mine](https://github.com/koszeggy/KGySoft.CoreLibraries/blob/84f813815375892a6ef86e1fbe527f55454a6786/KGySoft.CoreLibraries/CoreLibraries/Files.cs#L122) – György Kőszeg Oct 17 '20 at 11:11
  • @GyörgyKőszeg I'm gonna try yours as well. and if its work, can you post it as solution so that I can mark your solution – Maherz123 Oct 17 '20 at 11:13
  • 1
    Off-topic: `Interlocked.Increment` is nonsense here; `i` is a local variable, not shared between threads. – Ruud Helderman Oct 17 '20 at 11:15
  • It worked for me as well the given url by György Thank you so much – Maherz123 Oct 17 '20 at 11:17
  • @GyörgyKőszeg that's not my solution, OP found a duplicate – Cid Oct 17 '20 at 11:20
  • Check my answer here: https://stackoverflow.com/a/64402330/1109215 – apocalypse Oct 17 '20 at 12:15

0 Answers0