0
votes

Hi I am trying to writing a code which copy files from source folder to destination folder. If destination folder contains same file name then my program should store file with different name. e.g. Source Folder contains: C:\test\ test1.txt test2.txt

Destination folder contains D:\test\ test1.txt test2.txt test3.txt

then copy action should copy test1.txt and test2.txt from source to destination folder with name changed to test4.txt and test5.txt

This is not complete code. But I am getting error An Object reference is required for nonstatic field,method, or property. at getFileName( ref destfileName, ref targetPath). Any help on this?

class Program
{
    static void Main(string[] args)
    {
        string sourcefileName = null;
        string destfileName = null;
        string sourcePath = @"C:\test";
        string targetPath = @"D:\test";
        List<int> seqNum = new List<int>();

        // To copy a folder's contents to a new location:
        // Create a new target folder, if necessary.
        if (!System.IO.Directory.Exists(targetPath))
        {
            System.IO.Directory.CreateDirectory(targetPath);
        }

        // To copy all the files in one directory to another directory.
        // Get the files in the source folder. (To recursively iterate through
        // all subfolders under the current directory, see
        // "How to: Iterate Through a Directory Tree.")
        // Note: Check for target path was performed previously
        //       in this code example.
        if (System.IO.Directory.Exists(sourcePath))
        {
            string[] files = System.IO.Directory.GetFiles(sourcePath);

            // Copy the files and overwrite destination files if they already exist.
            foreach (string s in files)
            {
                // Use static Path methods to extract only the file name from the path.
                //File name is like text1.txt
                sourcefileName = System.IO.Path.GetFileName(s);       
                if (System.IO.Directory.GetFiles(targetPath).Count() > 0)
                {
                    foreach (string file in System.IO.Directory.GetFiles(targetPath))
                    {
                        if (file.Contains(sourcefileName))
                        {
                            int num;
                            string existingLatestFile = string.Empty;
                            destfileName = sourcefileName.Replace(".txt", string.Empty);
                            for (int i = 0; i < sourcefileName.Length; i++)
                            {
                                if (Char.IsDigit(sourcefileName[i]))
                                {
                                    existingLatestFile += sourcefileName[i];
                                }
                            }
                            if (int.TryParse(existingLatestFile, out num))
                            {
                                seqNum.Add(num);
                            }
                            destfileName = destfileName.Replace(existingLatestFile, string.Empty);//Remove existing number
                            num = num + 1;
                            destfileName = destfileName + num.ToString() + ".txt"; // Make a new file name
                            while (!getFileName( ref destfileName, ref targetPath))
                            {

                            }

                        }
                        else
                        {
                            destfileName = sourcefileName;
                        }
                        string destFile = System.IO.Path.Combine(targetPath, destfileName);
                        System.IO.File.Copy(s, destFile, false);
                    }
                }

            }
        }
        else
        {
            Console.WriteLine("Source path does not exist!");
        }

        if (System.IO.Directory.GetFiles(targetPath).Count() > 0)
        {
            foreach (string file in System.IO.Directory.GetFiles(targetPath))
            {
        /*        if (file.Contains(dir + "\\" + filename))
                {
                    int num;
                    existingLatestFile = file.Replace(dir + "\\" + filename, string.Empty);
                    existingLatestFile = existingLatestFile.Replace(".txt", string.Empty);

                    if (int.TryParse(existingLatestFile, out num))
                    {
                        seqNum.Add(num);
                    }
              }*/
          Console.WriteLine(file);
            }
        }

        // Keep console window open in debug mode.
        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }

    bool getFileName(ref string filename, ref string destFolder)
    {
        bool retValue =false;
        foreach (string file in System.IO.Directory.GetFiles(destFolder))
        {
            if (file.Contains(filename))
            {
                retValue = false;
            }
            else
            {
                retValue = true;
            }                
        }           
        return retValue;
    }
}
2

2 Answers

6
votes

Main() is a static method.
It is not associated with any instance.

You need to make the other method static as well.

0
votes

main is a static method, to call the non-static method getFileName you need to create a instance first

change

while (!getFileName...

to

Program p = new Program();
while (!p.getFileName...