Batch Moving files matching filenames to folders in C# or using a script

cadash

I was wondering if someone could assist or point me in the right direction to move files where part of the filename needs to be matched to part of the foldername for example:

Moving filename Cust-10598.txt to a folder named John-Doe-10598 Is this possible?

I was able to create all the folders inside the root directory where all the files are contained, now I would like to sort them and put each of them inside the matching folder.

Any help or ideas are highly appreciated

Anu Viswan

Assuming you already have a list of probably folders using Directory.GetDirectores(),

var listOfFolders = Directory.GetDirectories(basePath);

You can find the associated Folder for given filename using following method.

string GetAssociatedDirectory(string fileName,IEnumerable<string> folderNames)
{
    Regex regEx = new Regex(@"Cust-(?<Id>[\d]*)",RegexOptions.Compiled);
    Match match = regEx.Match(fileName);
    if (match.Success)
    {
        var customerId = match.Groups["Id"].Value;

        if(folderNames.Any(folder=>folder.EndsWith($"-{customerId}")))
        {
            return folderNames.First(folder=>folder.EndsWith(customerId));
        }
        else
        {
            throw new Exception("Folder not found");
        }
    }
    throw new Exception("Invalid File Name");
}

You can then use File.Move to copy the file to destination directory

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Batch file for moving files to folders based on filenames

Moving files to the right folders and renaming them using a batch script

Moving files into folders using bash script

Comparing files in two folders using batch script

Batch script - files to folders - with addition

Batch script to create folders and move files using part of their name

moving files to their respective folders using bash scripting

Moving files by their filenames

Batch Programming: Moving Files to Folders Based on Date Modified

Change filenames while moving to another folder using unix shell script

Using mkdir and ffmpeg in a batch script on recursive folders

How to zip multiple folders using Batch script

How to rename multiple folders that have a set number of files within that folder using batch script?

Batch script that will insert filenames into files within multiple subfolders

moving files and folders to a subfolder

Moving files into separate folders

Moving Files into Folders with Exceptions

Moving folders with the files with exceptions

finding files and moving their folders

Creating Folders and Moving Files

Reading files in multiple directories, matching filenames with their data using Node and Promises

Batch script to move each set of n files to different new folders

batch script to move x number of files to empty folders

Batch script to move EAN 13 barcode files to designated YMD folders

Batch script to create a ZIP file out of a list of stated files and folders

Batch script for move files into corresponding folders with similar name

Moving Files With Batch

Changing Multiple Filenames in multiple Folders with Batch

Batch script fails on filenames with parenthesis

TOP Ranking

HotTag

Archive