Click here to Skip to main content
15,867,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi i want to rename the file name and copy that in same folder. I have scheduled my console application in Task Scheduler.
If i am opened that CSV file means it got the following error:


C#
System.IO.IOException was caught   Message="The process cannot access the file because it is being used by another process."   Source="mscorlib"   StackTrace:        at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)        at System.IO.__Error.WinIOError()        at System.IO.File.Move(String sourceFileName, String destFileName) 


How can i solve this??

My code:

C#
foreach (var s in yetACCfiles)
                {
                    string source = To_be_processed + "\\" + s;
                    string filename = s;
                    string[] spilt = filename.Split('_');
                    string nam = spilt[0].ToString();
                    int index = yetACCfiles.IndexOf(s) + 1;
                    string rename;
                    if (index >= 10)
                    {
                        rename = nam + "_" + index + ".csv";
                    }
                    else
                    {
                        rename = nam + "_" + 0 + index + ".csv";
                    }
                    string destFile = System.IO.Path.Combine(To_be_processed, rename);
                    System.IO.File.Move(source, destFile);

                }
Posted
Updated 12-Jun-14 22:08pm
v3
Comments
Sprint89 13-Jun-14 3:23am    
Is it really being used by another process? If you run Unlocker on it, does it show something hanging on it - a copy process or something?
Pikoh 13-Jun-14 3:23am    
I'm not sure to understand your problem. Do you mean that if your file is opened while the scheduled task is executed it gives you that error? If is that what you mean,that is normal,you can't rename an opened file. If you want in that case to skip that file and go on with the process, wrap the move line in a try/catch.
vinodhini sekar 13-Jun-14 5:22am    
I put try/catch for rename the files . My scenario is i have the files name as
test_01,test_03,test_04,test_06...etc
Now i want to rename the all the file name as in order(i.e)
test_01,test_02,test_03,test_04

When i put the File.Move code in try.. it displays the same catch error.
try
{
using (File.Open(source, FileMode.Open))
{




System.IO.File.Move(source, destFile);
}
}

catch (IOException e)
{
}
Pikoh 13-Jun-14 7:33am    
But why are you opening the file before moving it? That way of course you'll have problems

1 solution

Do not use File.Open. Or close it before renaming and then reopen new file.

I've adjusted your function slightly. Your own above which in general looks correct, I just removed unnecessary ifs and concatenations.

C#
string rename;
foreach (var s in yetACCfiles)
{
    string source = System.IO.Path.Combine(To_be_processed, s);
    string[] spilt = s.Split('_');
    string nam = spilt[0].ToString();
    int index = yetACCfiles.IndexOf(s) + 1;

    rename = String.Format("{0}_{1:00}.csv", nam, index);
// alternative is ToString:     rename = String.Format("{0}_{1}.csv", nam, index.ToString("00"));
    string destFile = System.IO.Path.Combine(To_be_processed, rename);
    System.IO.File.Move(source, destFile);
 
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900