Link to home
Start Free TrialLog in
Avatar of Robb Hill
Robb HillFlag for United States of America

asked on

.net + Moving Files in a list

I am trying to do the following to handle moving .net files.


I will have a method that would pass in the following params

string originalPath, string originalDocument

Then I would convert the path strings into a directoryinfo.
SourceDirectoryInfo = new DirectoryInfo(originalPath);


I think from there I want to create a list of file info objects...
Something like this ...if there is better syntax please advise.

FileList would be  public List<FileInfo> FileList { get; set; }

FileList = SourceDirectoryInfo.GetFiles("$" + originalDocumentId + "_*.*", SearchOption.AllDirectories).Where(fi => fi.FullName.ToUpper().Contains(@"\VERSIONS\")).ToList();

So essentially this should create a list of files that given this source directory and the files are formatted as shown above...
and the directory its searching would have some subfolder called VERSIONS.  These files with the leading $ would be in a Versions volder.

So once I get this list I need to attempt to move those files in the list to a new destination.

I would need to iterate through this file list...and move the files...

I would need the ability to revert back the entire move if anything didnt work.

Moving one item in the list would not work.

It would need to be all or nothing.

I would imagine I would need some type of backup file list to restore to if the files being moved were not succesfull..



Please help.

Other than my starting point in my logic of passing in a string path and document(file)

that is really the only basis that this needs to start at.

And the structure the code needs to look to find the files in their directory structure.
ASKER CERTIFIED SOLUTION
Avatar of Dmitry G
Dmitry G
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Robb Hill

ASKER

I was hoping more of an example with the added complexity of the list of fileinfo objects.    Seems like with move you could crelate a backup to restore to on the exception of moving files ...keeping with less code
Before writing any code it's a good idea to understand requirements and have some plan :).
What do you mean saying "create a backup"? Where? On a disk? In a memory?
Creating it on a disk - involves copying and finally deleting anyway. In a memory? How big files are? How do you read them into memory?

Do I miss something?
something like this..but this would not work exactly as the directory part of code is different and the fact of iterating the list.

 try
            {
                // setup a file information objects
                sourceFile = new FileInfo(Path.Combine(sourcepath, sourcefilename));
                targetFile = new FileInfo(Path.Combine(targetpath, targetfilename));
                backupFile = new FileInfo(Path.Combine(Path.GetTempPath(), string.Format("{0}{1}", Guid.NewGuid(), sourceFile.Extension)));


                // if the source file exists
                if (sourceFile.Exists)
                {
                    // create a backup of the source file
                    sourceFile.CopyTo(backupFile.FullName);
                    // Reassign the backup file to the file info object
                    backupFile = new FileInfo(backupFile.FullName);

                    // Now move the source file to the target file.
                    sourceFile.MoveTo(targetFile.FullName);
                    // No exceptions result in true being returned
                    results = true;

     }
            }
            catch (Exception)
            {
                try
                {
                    // An exception is thrown, lets try to recover the source file if we successfully created a backup of it.
                    if (!File.Exists(sourceFile.FullName) && File.Exists(backupFile.FullName))
                        backupFile.MoveTo(sourceFile.FullName);
                }
                catch { /* Eat any exceptions.  Most likely we are unauthorized */ ; }
                results = false;
            }
            finally
            {
                try
                {
                    if (File.Exists(backupFile.FullName))
                        File.Delete(backupFile.FullName);
                }
                catch { /* Eat any exceptions.  Most likely we are unauthorized */ ; }
            }
            return results;

Open in new window

Fundamentally, there is no need to keep a backup copy of the moved file from a group as the original can suffice.  The reason I say this is because of the cost associated with copies and deletes of multiple files and their directories.  Instead what needs to take place is, identify the source list, create the target list, have a comparator that can tell you when the source list and target list match and then delete the source list.  If you need to roll back, you use the target list to reconstruct the missing parts of the source list.

-saige-
Was hoping for a move example ..but thanks