Created
January 25, 2021 10:19
-
-
Save vonox7/b9f29bc0436ce3b17e43305e0723f827 to your computer and use it in GitHub Desktop.
Moves a folder from a g suite folder to another. Useful when file is not owned and therefore can't be moved in the google drive web ui.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.example.gsuite | |
| import java.io.File | |
| import java.io.FileNotFoundException | |
| fun copyGsuiteDirectoryRecursively() { | |
| val originPrefix = "/Volumes/GoogleDrive/My Drive/origin" | |
| val targetPrefix = "/Volumes/GoogleDrive/Shared drives/destination" | |
| fun String.isIgnored() = this.endsWith("/Icon\r") || this.endsWith("/.DS_Store") | |
| // Files | |
| val count = File(originPrefix).walk().count() | |
| File(originPrefix).walk().forEachIndexed { index, file -> | |
| if (!file.isDirectory) { | |
| if (!file.path.isIgnored()) { | |
| val targetFile = File("$targetPrefix${file.path.toString().substringAfter(originPrefix)}") | |
| File(targetFile.parent).mkdirs() | |
| print("Copy $targetFile...") | |
| try { | |
| file.copyTo(targetFile, overwrite = true) | |
| print(" finished") | |
| if (!file.delete()) { | |
| println("ERROR: Could not delete ${file.path.replace("\r", "")}") | |
| } | |
| } catch (e: FileNotFoundException) { | |
| if (e.message?.contains("Operation not supported") == true && | |
| (file.path.endsWith(".gdoc") || file.path.endsWith(".gform") || file.path.endsWith(".gslides") || file.path.endsWith(".gsheet")) | |
| ) { | |
| println(" skipped") // Don't delete skipped files | |
| } else { | |
| throw e | |
| } | |
| } | |
| } else { | |
| if (!file.delete()) { | |
| throw Exception("Could not delete ${file.path.replace("\r", "")}") | |
| } | |
| } | |
| println(" - ${(index * 1000 / count) / 10.0}%") | |
| } | |
| } | |
| // Delete empty folders | |
| println("--------") | |
| println("Delete empty folders...") | |
| File(originPrefix).walkBottomUp().forEach { file -> | |
| if (file.isDirectory && file.walk().all { it.path.isIgnored() || it.path == file.path }) { | |
| println("Delete folder ${file.path}") | |
| if (!file.delete()) { | |
| println("ERROR: Could not delete ${file.path.replace("\r", "")}") | |
| } | |
| } | |
| } | |
| println("--------") | |
| println("Remaining files: ${File(originPrefix).walk().count()}") | |
| File(originPrefix).walk().forEach { file -> | |
| if (!file.isDirectory) { | |
| println(file.path) | |
| } | |
| } | |
| println("--------") | |
| println("FIN") | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment