How to rename file in MFT

In MFT, when file is moved from source to transfer, we can rename the file.

This will be useful in scenarios when the file name is different than what is expected

Sometime during decryption the extension is lost, we can add a different extension

Sometimes we have to append a source to handle files from different sources differently

We can use RenameRegExp to rename the file

This uses same method as java replace. The input is Regular expression

Common use cases and examples

to completely rename the file –

String sourceRegExp = "([A-Za-z0-9_]+)([A-Za-z0-9.]*)";
String targetRegExp = "input_file.csv";
Input Output
ABC_SOME_BIG_FILE_NAME.csvinput_file.csv
ABC_SOME_OTHER_FILE_NAME.csvinput_file.csv
SOME_FILE_NAME_WITHOUT_EXTENSIONinput_file.csv

To completely change the extension

String sourceRegExp = "([A-Za-z0-9_]+)([A-Za-z0-9.]*)";
String targetRegExp = "$1.csv";
InputOutput
some_data_1.txt some_data_1.csv
some_data_2.dat some_data_2.csv
some_data_3. some_data_3.csv
some_data_4.txt.pgp some_data_4.csv
some_data_5.txt.gpg some_data_5.csv
some_data_6.csv some_data_6.csv
some_data_7 some_data_7.csv

To add a prefix to the file name.

String sourceRegExp = "([A-Za-z0-9_.]+)";
String targetRegExp = "source_$1";
InputOutput
some_data_1.txt source_some_data_1.txt
some_data_2.dat source_some_data_2.dat
some_data_3. source_some_data_3.
some_data_4.txt.pgp source_some_data_4.txt.pgp
some_data_5.txt.gpg source_some_data_5.txt.gpg
some_data_6.csv source_some_data_6.csv
some_data_7 source_some_data_7

Sample Java code to test regular expression if the input / output is as expected:

public class RenameString {
    public static void main(String[] args){

        TestCase[] testCases  = new TestCase[7];
      
        testCases[0] = new TestCase("some_data.txt", "some_data.csv");
        testCases[1] = new TestCase("some_data.dat", "some_data.csv");
        testCases[2] = new TestCase("some_data.", "some_data.csv");
        testCases[3] = new TestCase("some_data.txt.pgp", "some_data.csv");
        testCases[4] = new TestCase("some_data.txt.gpg", "some_data.csv");
        testCases[5] = new TestCase("some_data.csv", "some_data.csv");
        testCases[6] = new TestCase("some_data", "some_data.csv");
    
        for ( TestCase t : testCases) {
            String output = renameFileNameExtension(t.inputStr);

            System.out.print(t.inputStr + " --- " + t.expectedOutputStr + " --- " + output + " --- " );

            if ( output.equals(t.expectedOutputStr) ) {
                System.out.println("success");
            }
            else {
                System.out.println("failed");
            }
        }
    }

    public static String renameFileNameExtension(String inputStr) {
        
        String sourceRegExp = "([A-Za-z0-9_]+)([A-Za-z0-9.]*)";
        String targetRegExp = "$1.csv";

        String output = inputStr.replaceAll(sourceRegExp, targetRegExp);

        return output;
    }

}

class TestCase {
    String inputStr;
    String expectedOutputStr;

    TestCase(String inputStr, String expectedOutputStr){
        this.inputStr = inputStr;
        this.expectedOutputStr = expectedOutputStr;
    }
}

You can test Regular expression using this website https://regex101.com/ and selecting Java language.

About the Author

khalil

Hi, I'm Khalil, a passionate front-end developer with over 8 years of experience in web development. I specialize in building interactive, user-friendly websites using technologies like JavaScript, HTML, CSS, and Java.

I've had the opportunity to work on a variety of projects across different industries, helping businesses create modern and responsive web applications.

Outside of coding, I enjoy exploring the latest trends in design and tech, as well as sharing knowledge with others. I'm always looking for new challenges to grow my skills and make a positive impact in the tech community

You may also like these

No Related Post