Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

C# keep value of variable

Posted on 2010-11-27
1
Medium Priority
?
403 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 2000 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

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…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

636 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