-
-
Notifications
You must be signed in to change notification settings - Fork 1
File Renaming Documentation
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.
- Rename files dynamically, including appending timestamps.
- Change file extensions efficiently.
- Move files and directories to new locations.
- Handle large batch renaming with minimal code.
To include this library in your Java project, add the following dependency:
Replace the variable ${latestVersion} with the current latest version:
Define version in the gradle.properties file:
fileWorkerVersion=${latestVersion}
or in the build.gradle ext area:
fileWorkerVersion = "${latestVersion}"
Then, add the dependency:
implementation("io.github.astrapi69:file-worker:$fileWorkerVersion")
For automatic version updates:
[versions]
file-worker-version=${latestVersion}
[libraries]
file-worker = { module = "io.github.astrapi69:file-worker", version.ref = "file-worker-version" }
Then, add:
implementation libs.file.worker
Maven dependency is now available on Sonatype. Check out the
Sonatype repository
for the latest snapshots and releases.
<properties>
...
<file-worker.version>${latestVersion}</file-worker.version>
...
</properties>
<dependencies>
...
<dependency>
<groupId>io.github.astrapi69</groupId>
<artifactId>file-worker</artifactId>
<version>${file-worker.version}</version>
</dependency>
...
</dependencies>The RenameFileExtensions class provides methods for renaming files dynamically.
File file = new File("report.txt");
String newFilename = RenameFileExtensions.appendSystemtimeToFilename(file);
System.out.println("Renamed file: " + newFilename);File oldFile = new File("document.doc");
RenameFileExtensions.changeFilenameSuffix(oldFile, ".pdf", true);
System.out.println("File extension changed successfully!");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);
}The library makes it easy to move files between directories.
File source = new File("documents/report.txt");
File destination = new File("archives/report.txt");
RenameFileExtensions.moveFile(source, destination);
System.out.println("File moved successfully!");File source = new File("logs/error.log");
File destinationDir = new File("logs/archived");
RenameFileExtensions.moveFile(source, destinationDir);
System.out.println("File moved and archived!");@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);
}The library supports bulk renaming and moving of files.
File directory = new File("backup");
RenameFileExtensions.changeAllFilenameSuffix(directory, ".txt", ".bak", true);
System.out.println("All text files converted to backup files!");File oldDir = new File("project");
File newDir = new File("archives/project_backup");
RenameFileExtensions.moveFile(oldDir, newDir);
System.out.println("Project directory moved!");@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());
}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!
- Create New Files And Directories
- Delete Files Or Directories
- File Comparison Documentation
- File Modification And Merging
- File Reading Documentation
- File Renaming Documentation
- File Search Documentation
- File Sorting Documentation
- File Writing Documentation
- Java File Copy Utilities
- System Utilities Documentation