Skip to content

Instantly share code, notes, and snippets.

@DHosseiny
Last active June 8, 2020 11:25
Show Gist options
  • Save DHosseiny/36d92edb284260a8c4071b4b2ce63969 to your computer and use it in GitHub Desktop.
Save DHosseiny/36d92edb284260a8c4071b4b2ce63969 to your computer and use it in GitHub Desktop.
Runnable Kotlin Script example with windows batch(double click .bat file to run) -- This example Searches all neighbor folders for android projects and deletes project and it's modules "build" folders(For reducing disk size). *need one time set "kotlinc" enviroment variable(See comments for guidance).
%kotlinc% -script DeleteBuildFolders.kts -- -dir .
:: "." is default path you can change it to any folder you want to work on that folder ^
:: (script workes on current folder without "-dir" argument)"
:: "^" can be used for multiline commands
import java.io.File
// Get the passed in path, i.e. "-d some/path" or use the current path.
val path = if (args.contains("-d")) args[1 + args.indexOf("-dir")]
else "."
println("Processing in folder: $path")
println()
fun deleteBuildFolders(path: String) {
val folders = File(path)
.walk()
.maxDepth(2)
.onEnter { it.isDirectory }
.map { File(it.path + "/build") }
.filter(File::canWrite)
.toList()
if (folders.isEmpty()) {
println("No Android Project folders found")
}
folders.forEach { folder ->
val deleted = folder.deleteRecursively()
if (deleted) println(folder.path + " Deleted")
}
}
deleteBuildFolders(path)
println()
println("Press Enter to close")
System.`in`.read()
@DHosseiny
Copy link
Author

DHosseiny commented Jun 8, 2020

First way: Open a cmd and type this:
setx kotlinc "%USERPROFILE%\.AndroidStudio4.0\config\plugins\Kotlin\kotlinc\bin\kotlinc" Second way: create a new variable in "Environment Variables" of windows with value of:
%USERPROFILE%\.AndroidStudio4.0\config\plugins\Kotlin\kotlinc\bin\kotlinc

"AndroidStudio4.0" used here, change it to your installed version of AndroidStudio or IntellijIdea(Make sure Kotlin plugin is installed)

@DHosseiny
Copy link
Author

For me it deleted 5G.B

@DHosseiny
Copy link
Author

DHosseiny commented Jun 8, 2020

--dir command argument used here. You can have any number of arguments with any name you want and receive it in code with args

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment