Reputation: 3944
In my below code, I am trying to move all my .doc
files located in one folder to another. In production, this will be moving files generated into created folder C:\Temp\
to a network folder that has a database job which every 5 minutes moves the files from the network folder into our document imagining archive system.
When trying my below test code, I receive "The process cannot access the file because it is being used by another process."
CODE:
public void moveLocalToCommitFYI()
{
// MOVE DOCS FORM TEMP FOLDER TO COMMITFYI FOLDER
string dirSource = @"C:\Users\NAME\Desktop\CommitTest\MoveTest\";
string dirDest = @"C:\Users\NAME\Desktop\CommitTest\MoveTest\DestTest\";
try
{
Directory.Move(dirSource, dirDest);
}
catch (Exception ex)
{
MessageBox.Show("Source:\t" + ex.Source + "\nMessage: \t" + ex.Message + "\nData:\t" + ex.Data);
}
}
What appears strange to me is that when I specify a single source file outright and it's destination, code functions fine:
public void moveLocalToCommitFYI()
{
// MOVE DOCS FORM TEMP FOLDER TO COMMITFYI FOLDER
string dirSource = @"C:\Users\NAME\Desktop\CommitTest\MoveTestFile.doc";
string dirDest = @"C:\Users\NAME\Desktop\CommitTest\MoveTest\MoveTestFile.doc";
try
{
Directory.Move(dirSource, dirDest);
}
catch (Exception ex)
{
MessageBox.Show("Source:\t" + ex.Source + "\nMessage: \t" + ex.Message + "\nData:\t" + ex.Data);
}
}
Surely there is a way to move all my .doc
files from the one directory to the other without needing to loop through and specify each individual file name?
EDIT:
Made the modification of having my destination folder be a different folder on my desktop instead of a sub-folder. dirSource contained 5 word documents and the destination directory (\MoveTest\ on my Desktop) contained no files:
string dirSource = @"C:\Users\NAME\Desktop\CommitTest\MoveTest\";
string dirDest = @"C:\Users\NAME\Desktop\MoveTest\";
This generated "Cannot create a file when that file already exists"
.
Based on this, I assumed that the code is actually MOVING the set directory folder and all it's contents from one location to another, so I modified my code to the following:
string dirSource = @"C:\Users\NAME\Desktop\CommitTest\MoveTest\";
string dirDest = @"C:\Users\NAME\Desktop\MoveTest2\";
This got rid of my sub-folder \MoveTest\ within \Desktop\CommitTest\ and created folder \Desktop\MoveTest2.
Is there a way for me to move the contents of the folder without getting rid of the source folder and place them into an already created destination?
Upvotes: 0
Views: 5517
Reputation: 56536
Your test code is trying to move the entire directory into a directory inside itself. This is not a valid operation. Your actual code is probably trying to move the whole Temp directory to another location. This is not the same as moving all files inside it to another directory.
You'll have to loop through all file names and move each file individually.
var files = Directory.EnumerateFiles(dirSource, "*.doc")
.Select(path => new FileInfo(path));
foreach (var file in files)
file.MoveTo(Path.Combine(dirDest, file.Name));
If you find that the individual moves add too much overhead to the network traffic (especially important if this is a slow Internet connection and not a fast local connection), you might want to zip up all of your doc files (e.g. with ZipFile
) before transmitting them to the server, and then have the server unzip them. This compression will probably lower the overall size, and transmitting one file instead of many will reduce the network overhead.
Upvotes: 3
Reputation: 11903
You cannot move a folder below itself. You need to find all its contents and move them. Or do this: rename MoveTest to MoveTest2, create a new MoveTest directory, and now you can move MoveTest under it.
Upvotes: 1