Skip to content

Commit 7c3e509

Browse files
author
Jan Guegel
committed
change SaveAsDialog now appends "_{index}" to filename if file already exists
1 parent ab31c82 commit 7c3e509

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
### Fixed
99
- Fixed files from hidden folders showing up in storage tab browser ([#217])
1010

11+
### Changed
12+
- SaveAsDialog now appends "_{index}" to filename if file already exists ([#131])
13+
1114
## [1.3.0] - 2025-09-30
1215
### Added
1316
- Added a separate "Save as" option in the text editor ([#224])
@@ -96,6 +99,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
9699
[#250]: https://github.com/FossifyOrg/File-Manager/issues/250
97100
[#251]: https://github.com/FossifyOrg/File-Manager/issues/251
98101
[#267]: https://github.com/FossifyOrg/File-Manager/issues/267
102+
[#267]: https://github.com/FossifyOrg/File-Manager/issues/131
99103

100104
[Unreleased]: https://github.com/FossifyOrg/File-Manager/compare/1.3.0...HEAD
101105
[1.3.0]: https://github.com/FossifyOrg/File-Manager/compare/1.2.3...1.3.0

app/src/main/kotlin/org/fossify/filemanager/activities/SaveAsActivity.kt

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class SaveAsActivity : SimpleActivity() {
5858
?: filename.getMimeType()
5959
val inputStream = contentResolver.openInputStream(source)
6060

61-
val destinationPath = "$destination/$filename"
61+
val destinationPath = setAvailablePath("$destination/$filename")
6262
val outputStream = getFileOutputStreamSync(destinationPath, mimeType, null)!!
6363
inputStream!!.copyTo(outputStream)
6464
rescanPaths(arrayListOf(destinationPath))
@@ -86,4 +86,32 @@ class SaveAsActivity : SimpleActivity() {
8686
return filename.replace("[/\\\\<>:\"|?*\u0000-\u001F]".toRegex(), "_")
8787
.takeIf { it.isNotBlank() } ?: "unnamed_file"
8888
}
89+
90+
private fun setAvailablePath(path: String): String {
91+
val file = File(path)
92+
93+
return if (!file.exists()) {
94+
path
95+
} else {
96+
findAvailableNameForExistingFile(file)
97+
}
98+
}
99+
100+
private fun findAvailableNameForExistingFile(file: File): String {
101+
val parent = file.parent ?: return file.absolutePath
102+
val name = file.nameWithoutExtension
103+
val ext = if (file.extension.isNotEmpty()) ".${file.extension}" else ""
104+
105+
var index = 1
106+
var newFile: File
107+
108+
do {
109+
val newName = "${name}_${index}$ext"
110+
newFile = File(parent, newName)
111+
index++
112+
} while (newFile.exists())
113+
114+
return newFile.absolutePath
115+
}
116+
89117
}

0 commit comments

Comments
 (0)