HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Moving files while preserving the folder structure

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
thewhilepreservingfilesmovingfolderstructure

Problem

I'm writing a console application that will look through a directory and move any log files that have a date modified older than X days (configurable in the app.config file).

The problem I'm having is with the Move method I wrote. It works, but I think there might be a better way to write this method and eliminate the need for the sourceRoothPath parameter. Here's what I have (it's part of a LogManager class I wrote to do the checking and moving of log files):

```
public void Move(string sourceRootPath, string sourcePath, string targetPath)
{
if (String.IsNullOrWhiteSpace(sourcePath))
throw new ArgumentNullException("sourcePath");

if (String.IsNullOrWhiteSpace(targetPath))
throw new ArgumentNullException("destinationPath");

if (!File.Exists(sourcePath))
throw new FileNotFoundException(sourcePath);

try
{
// trimmedPath becomes the file path with all the subfolders, but without the
// sourceRootPath that comes in front of it. i.e. it strips the value passed
// in sourceRootPath from the value passed in sourcePath. The "+ 1" is to include the
// trailing "\" in the path.
string trimmedPath = sourcePath.Substring(sourceRootPath.Length + 1);
string newPath = Path.Combine(targetPath, trimmedPath);
string fileName = Path.GetFileName(sourcePath);

// folderStructure is used for creating the subfolder structure I want to preserve.
// (It is just removing the file name and extension from the newPath.)
string folderStructure = newPath.Substring(0, (newPath.Length - fileName.Length));

// Directory.CreateDirectory will create the entire folder structure for me; no need
// for looping or recursive calls.
Directory.CreateDirectory(folderStructure);

// File.Move has no ability to overwrite, so I have to delete the file if it exists in the
// destination directory so that File.Move doesn't throw an except

Solution

No, you do definitely need 3 parameters to do what you are trying to do

Although I would suggest a different 3.

public void Move(string sourceRoot, string targetRoot, string[] relative) {
    /* Precondition checks and try-catch removed for clarity */

    string source = Path.Combine(sourceRoot, relative);
    string target = Path.Combine(targetRoot, relative);
    string folderStructure = Path.GetDirectoryName(target);

    /* etc */
}


This makes clear that there is shared structure under source and target, and there isn't any direct string manipulation on file and folder names. I would even be tempted to factor this out into a seperate class, e.g.

class DirectoryMover {
    private string source;
    private string target;

    private void Move(string[] relative) { ... }
    public void MoveAll() { /* iterate over folders calling Move */ }
}

Code Snippets

public void Move(string sourceRoot, string targetRoot, string[] relative) {
    /* Precondition checks and try-catch removed for clarity */

    string source = Path.Combine(sourceRoot, relative);
    string target = Path.Combine(targetRoot, relative);
    string folderStructure = Path.GetDirectoryName(target);

    /* etc */
}
class DirectoryMover {
    private string source;
    private string target;

    private void Move(string[] relative) { ... }
    public void MoveAll() { /* iterate over folders calling Move */ }
}

Context

StackExchange Code Review Q#129189, answer score: 4

Revisions (0)

No revisions yet.