Commit 887e84ff authored by pengjunjing's avatar pengjunjing

feat:增加移动脚本

parent dbf52e7b
package com.laihua.projecthelper.submerge
import java.io.File
main()
fun main() {
println("开始运行")
//要检查合并的目录
val targetPath = "C:\\Android\\workspace\\laihua-hw"
val targetFile = File(targetPath)
//旧模块名字,未移动之前
val mKtModelOldDirs = targetFile.listFiles()?.filter {
it.isDirectory && it.name.startsWith("m_kt", true)
}
//新模块的父目录位置
val mKtModelNewParentDirs = targetFile.listFiles()?.filter {
it.isDirectory && it.name.startsWith("mod_", true)
}
//所有的不在子模块里面的文件
val outModuleFiles = mutableListOf<File>()
mKtModelOldDirs?.forEach {
println("旧模块文件夹列表:${it.name}")
val allFilesIncludeSub = it.getAllFilesIncludeSub()
outModuleFiles.addAll(allFilesIncludeSub)
for (file in allFilesIncludeSub) {
println("--> ${file.path}")
}
}
val sumOf = mKtModelOldDirs?.sumOf {
it.getAllFilesIncludeSub().size
}
println("文件总数:$sumOf")
println("----------------------------------------")
val moveInfoList = mutableListOf<MoveInfo>()
outModuleFiles.forEach { oldFile ->
var tmpPath = oldFile.path.replace(targetPath, "")
if (tmpPath.startsWith(File.separator)) {
tmpPath = tmpPath.replaceFirst(File.separator, "")
}
//所属模块名字
val moduleName = tmpPath.substring(0, tmpPath.indexOf(File.separator))
// println("新位置1 $tmpPath $moduleName")
mKtModelNewParentDirs?.forEach { newParentFile: File ->
newParentFile.listFiles()?.find {
it.name == moduleName
}?.let { newParentFileTarget ->
var replace = oldFile.path.replace(targetPath, "")
replace = replace.replaceFirst("${File.separator}$moduleName", "")
replace = newParentFileTarget.path + replace
if (File(replace).exists()) {
// println("新位置,存在相同文件 ${oldFile.path} -> $replace")
moveInfoList.add(MoveInfo(moduleName, oldFile, File(replace)))
} else {
println("新位置,需要新增的文件 ${oldFile.path} -> $replace")
}
}
}
}
println("需要移动的总数:${moveInfoList.size}")
moveInfoList.forEach {
}
}
class MoveInfo(val moduleName: String, old: File, new: File) {
}
/**
* 获取所有文件,包括子文件的子文件,(包含最深度的文件)
*/
fun File.getAllFilesIncludeSub(): List<File> {
if (this.isFile) {
throw IllegalArgumentException()
}
val directory = this
val files = mutableListOf<File>()
if (directory.exists() && directory.isDirectory) {
directory.listFiles()?.forEach { file ->
if (file.isFile) {
files.add(file)
} else {
files.addAll(file.getAllFilesIncludeSub())
}
}
}
return files
}
package com.laihua.projecthelper.util
import java.io.File
/**
* Author: pengjunjing
* Date: 2023/3/2
* Description:
*/
class FileExt {
}
/**
* 获取所有文件,包括子文件的子文件,(包含最深度的文件)
*/
fun getAllFilesIncludeSub(path: String): List<File> {
val directory = File(path)
val files = mutableListOf<File>()
if (directory.exists() && directory.isDirectory) {
directory.listFiles()?.forEach { file ->
if (file.isFile) {
files.add(file)
} else {
files.addAll(getAllFilesIncludeSub(file.absolutePath))
}
}
}
return files
}
///**
// * 获取所有文件,包括子文件的子文件,(包含最深度的文件)
// */
//fun File.getAllFilesIncludeSub(): List<File> {
// if (this.isFile) {
// throw IllegalArgumentException()
// }
// val directory = this
// val files = mutableListOf<File>()
// if (directory.exists() && directory.isDirectory) {
// directory.listFiles()?.forEach { file ->
// if (file.isFile) {
// files.add(file)
// } else {
// files.addAll(file.getAllFilesIncludeSub())
// }
// }
// }
// return files
//}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment