-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRename.kt
More file actions
104 lines (89 loc) · 3.35 KB
/
Rename.kt
File metadata and controls
104 lines (89 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import java.io.File
import java.security.MessageDigest
fun main() {
renameFiles()
// deleteDuplicateFiles()
}
fun deleteDuplicateFiles() {
val parentFolder = File("./Frames_1") // Change this to your folder path
if (!parentFolder.exists() || !parentFolder.isDirectory) {
println("Invalid folder path")
return
}
val imageHashes = mutableMapOf<String, File>() // Map to store image hashes
findAndRemoveDuplicates(parentFolder, imageHashes)
}
fun findAndRemoveDuplicates(folder: File, imageHashes: MutableMap<String, File>) {
folder.listFiles()?.forEach { file ->
if (file.isDirectory) {
// Recursively check subfolders
findAndRemoveDuplicates(file, imageHashes)
} else if (isImageFile(file)) {
val hash = getFileHash(file)
if (imageHashes.containsKey(hash)) {
println(">>>>>>> Duplicate found: ${file.absolutePath} (same as ${imageHashes[hash]?.absolutePath})")
file.delete() // Remove the duplicate
} else {
imageHashes[hash] = file
}
}
}
}
// Function to check if a file is an image
fun isImageFile(file: File): Boolean {
val extensions = listOf("jpg", "jpeg", "png", "gif", "bmp", "webp")
return file.extension.lowercase() in extensions
}
// Function to compute a file's hash (SHA-256)
fun getFileHash(file: File): String {
val digest = MessageDigest.getInstance("SHA-256")
file.inputStream().use { input ->
val buffer = ByteArray(1024)
var bytesRead: Int
while (input.read(buffer).also { bytesRead = it } != -1) {
digest.update(buffer, 0, bytesRead)
}
}
return digest.digest().joinToString("") { "%02x".format(it) }
}
fun renameFiles() {
// Specify the folder path
val folderPath = "./occasions/stickers"
// Specify the new name prefix
val newNamePrefix = "easter_sticker_"
// Access the folder
val folder = File(folderPath)
// Check if the folder exists and is a directory
if (folder.exists() && folder.isDirectory) {
// Get all files in the folder
val files = folder.listFiles()
if (files != null) {
var counter = 0
for (file in files) {
if (file.isFile) {
// Get the file extension
var extension = file.extension
// extension = if (file.extension == "PNG") "webp" else "webp"
if (file.name.startsWith("b")) {
// Construct the new file name
val newFileName = "$newNamePrefix${counter}.$extension"
// Rename the file
val renamedFile = File(folder, newFileName)
val success = file.renameTo(renamedFile)
// Print the result
if (success) {
println("Renamed: ${file.name} -> ${renamedFile.name}")
counter++
} else {
println("Failed to rename: ${file.name}")
}
}
}
}
} else {
println("No files found in the folder.")
}
} else {
println("The specified path is not a valid folder.")
}
}