Skip to content

File Renaming Documentation

Asterios Raptis edited this page Feb 10, 2025 · 3 revisions

File Renaming and Moving Library Documentation

Introduction

Managing files often involves renaming and moving them efficiently. Whether you're restructuring a project, organizing backups, or appending timestamps to filenames, the file-worker library provides robust utilities for renaming and relocating files and directories.

Imagine needing to rename logs with timestamps or move files between directories while maintaining a structured naming convention. This library automates these tasks seamlessly.

Features Overview

  • Rename files dynamically, including appending timestamps.
  • Change file extensions efficiently.
  • Move files and directories to new locations.
  • Handle large batch renaming with minimal code.

Getting Started

For installation and dependencies, refer to the Getting Started guide.


Renaming Files with RenameFileExtensions

The RenameFileExtensions class provides methods for renaming files dynamically.

Example: Appending a Timestamp to a Filename

File file = new File("report.txt");
String newFilename = RenameFileExtensions.appendSystemtimeToFilename(file);

System.out.println("Renamed file: " + newFilename);

Example: Changing File Extension

File oldFile = new File("document.doc");
RenameFileExtensions.changeFilenameSuffix(oldFile, ".pdf", true);

System.out.println("File extension changed successfully!");

Unit Test Coverage

The RenameFileExtensionsTest class verifies these functionalities:

@Test
public void testAppendSystemtimeToFilename() throws IOException {
    File file = new File("test.txt");
    StoreFileExtensions.toFile(file, "Sample text");

    String newFilename = RenameFileExtensions.appendSystemtimeToFilename(file);
    assertNotNull(newFilename);
}

Moving Files to a New Directory

The library makes it easy to move files between directories.

Example: Moving a File

File source = new File("documents/report.txt");
File destination = new File("archives/report.txt");

RenameFileExtensions.moveFile(source, destination);

System.out.println("File moved successfully!");

Example: Moving and Renaming Simultaneously

File source = new File("logs/error.log");
File destinationDir = new File("logs/archived");

RenameFileExtensions.moveFile(source, destinationDir);

System.out.println("File moved and archived!");

Unit Test Coverage

@Test
public void testMoveFile() throws IOException {
    File file = new File("movetest.txt");
    StoreFileExtensions.toFile(file, "Move test data");

    File destination = new File("new_location/movetest.txt");
    boolean result = RenameFileExtensions.moveFile(file, destination);
    assertTrue(result);
}

Bulk Renaming and Moving

The library supports bulk renaming and moving of files.

Example: Renaming Multiple Files by Changing Extensions

File directory = new File("backup");
RenameFileExtensions.changeAllFilenameSuffix(directory, ".txt", ".bak", true);

System.out.println("All text files converted to backup files!");

Example: Moving a Directory

File oldDir = new File("project");
File newDir = new File("archives/project_backup");

RenameFileExtensions.moveFile(oldDir, newDir);

System.out.println("Project directory moved!");

Unit Test Coverage

@Test
public void testChangeAllFilenameSuffix() throws IOException {
    File directory = new File("test_dir");
    RenameFileExtensions.changeAllFilenameSuffix(directory, ".log", ".txt", true);

    File expectedFile = new File("test_dir/sample.txt");
    assertTrue(expectedFile.exists());
}

Conclusion

The file-worker library simplifies file renaming and moving operations, making it an essential tool for organizing and restructuring files efficiently.

Start using it today to automate your file management workflows!

Clone this wiki locally