Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8387641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:16:08+00:00 2026-06-09T18:16:08+00:00

Hi I am trying to writing a code which copy files from source folder

  • 0

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;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T18:16:10+00:00Added an answer on June 9, 2026 at 6:16 pm

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

    You need to make the other method static as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a code in which I am trying to open up the
I am trying to copy a file from a child directory of my root
I'm currently trying to create a C source code which properly handles I/O whatever
I'm writing xslt code which concatenates some string: <xsl:attribute name='src'> <xsl:value-of select=concat('url(&apos;', $imgSrc, '&apos;)')
I'm trying to create a directory and copy files to it. The code I've
Im trying to access the org.testng.reporters.XMLReporter from my Java code. I am writing code
I'm writing a small piece of code which takes input from a user via
I'm trying to practice writing better code, so I wanted to validate my input
I am trying to fully understand the process pro writing code in some language
I am writing a code where the android phone is the client trying to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.