Solved

C# keep value of variable

Posted on 2010-11-27
1
399 Views
Last Modified: 2012-05-10
Are there any method to keep value of  "i" in the code below so that the name of file that change will be continue

public void CopyFiles(string sourceDir, string targetDir)
        {
            string[] sourceFiles = Directory.GetFiles(sourceDir);
            for (int i = 0; i < sourceFiles.Length; i++)
            {
                string sourceFile = sourceFiles[i];
                string targetFile = targetDir + "\\" + "1" + (i + 10).ToString().PadLeft(3, '0') + "1" + ".mp3";
                File.Copy(sourceFile, targetFile, true);
            }
        }

Open in new window

0
Comment
Question by:teera
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 4

Accepted Solution

by:
Maverick543 earned 500 total points
ID: 34224906
You can make use of a global variable.

Assign to 0 before starting the whole copying. Then add it to the line
string targetFile = targetDir + "\\" + "1" + (i + 10 + globalVarI).ToString().PadLeft(3, '0') + "1" + ".mp3";
               
And add the value of i to it at the end of the loop.

See the code below for a full solution

Regards
Marco
int globalI = 0;

public void CopyFiles(string sourceDir, string targetDir)
        {
            string[] sourceFiles = Directory.GetFiles(sourceDir);
            for (int i = 0; i < sourceFiles.Length; i++)
            {
                string sourceFile = sourceFiles[i];
                string targetFile = targetDir + "\\" + "1" + (i + 10 + globalI).ToString().PadLeft(3, '0') + "1" + ".mp3";
                File.Copy(sourceFile, targetFile, true);
            }
            globalI += sourceFiles.Length;
        }

Open in new window

0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question