How to delete a world folder (PROPERLY) ?

Discussion in 'Plugin Development' started by ThunderWaffeMC, Jun 29, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    ThunderWaffeMC

    I have literally looked everywhere on Google to find how to delete a folder, files and directories but I can't seem to find a perfect working one for my needs. I want to delete a world folder on command (I have tried the bukkit.deleteWorld code which doesn't seem to delete anything).

    I want it like:

    Code:java
    1.  
    2. if (args.length == 1) {
    3. if (args[0].equalsIgnoreCase("delete")) {
    4. World playerworld = Bukkit.getWorld(player.getName() + "-world");
    5. File playerworldfolder = new File(player.getName() + "-world");
    6. Bukkit.getServer().unloadWorld((playerworld), isInitialized()); //unload world
    7. //delete world folder and all contents under the directory: "C://User/Toshiba/My Documents/zServers/Collide Server/" and delete the "playerworldfolder" OR "playerworld"
    8. if (!(playerworldfolder==null)){ // if the world folder is still there (meaning it didn't delete) then send a message saying "failed"
    9. sender.sendMessage("failed");
    10. }
    11.  
    12.  


    All I need at the moment is the code for deleting the folder or all the files in the folder.

    Thanks!

    I've tried using Common IO which should be a 1 liner:

    File worldDir = new File(worldName + File.separator);
    FileUtils.deleteDirectory(worldDir);

    But I don't know how to install it properly (since I get errors in the console on command saying FileUtils isn't installed.
     
  2. Code:java
    1. File folder = new File(server.getWorld(args[0]).getWorldFolder().getPath());
    2. server.unloadWorld(args[0], true);
    3. config.getConfigurationSection("worlds").set(args[0], null);
    4. deleteDirectory(folder);
    5.  
    6. /***********************************/
    7.  
    8. public static boolean deleteDirectory(File path) {
    9. if( path.exists() ) {
    10. File[] files = path.listFiles();
    11. for(int i=0; i<files.length; i++) {
    12. if(files[I].isDirectory()) {[/I]
    13. [I] deleteDirectory(files[I]);[/I][/I]
    14. [I] }[/I]
    15. [I] else {[/I]
    16. [I] files[I].delete();[/I][/I]
    17. [I] }[/I]
    18. [I] }[/I]
    19. [I] }[/I]
    20. [I] return( path.delete() );[/I]
    21. [I] }[/I]


    And of course you need to run some checks to make sure the folder is a legit world folder,

    Code:java
    1.  
    Code:java
    1.  
    2. [I]if(new File(worldpath + "/level.dat").exists()){[/I]
    3. [I][/I]

    is one i use. Also remember to nicely teleport all players to another world and tell them what's going on :)
     
    ThunderWaffeMC likes this.
  3. Offline

    ThunderWaffeMC

    Raz I'm getting many errors with the code. I'm not sure what your "server" variable is but I replaced it with getServer(). I'm not sure what to add as the world path at:

    Code:java
    1.  
    2. if(new File(worldpath + "/level.dat").exists()){
    3.  


    and I don't know what your variable is for "config" as in:

    Code:java
    1.  
    2. config.getConfigurationSection("worlds").set(args[0], null);
    3.  


    Then for:

    Code:java
    1.  
    2. public static boolean deleteDirectory(File path) {
    3. if( path.exists() ) {
    4. File[] files = path.listFiles();
    5. for(int i=0; i<files.length; i++) {
    6. if(files.isDirectory()) {
    7. deleteDirectory(files);
    8. }
    9. else {
    10. files.delete();
    11. } //end else
    12. }
    13. }
    14. return( path.delete() );
    15. }
    16.  


    I get errors on "files.isDirectory()" saying: cannot invoke isDirectory() on the array type File[]
    I get another error on "deleteDirectory" saying: The method deleteDirectory(File) in the type [Plugin Name] is not applicable for the arguments (File[])
    Also another error on "files.delete()" saying: cannot invoke files.delete() on the array type File[]

    My code:

    Code:java
    1.  
    2. Bukkit.getServer().unloadWorld((playerworld), isInitialized()); //unload world
    3.  
    4. sender.sendMessage(ChatColor.BLUE + "[Collide] " + ChatColor.RED + "Deleting files... Please wait!"); //delete files
    5. File folder = new File(getServer().getWorld(player.getName() + "-world").getWorldFolder().getPath());
    6. if(new File(worldpath + "/level.dat").exists()){
    7. config.getConfigurationSection("worlds").set(args[0], null);
    8. deleteDirectory(folder);
    9.  
    10. public static boolean deleteDirectory(File path) {
    11. if( path.exists() ) {
    12. File[] files = path.listFiles();
    13. for(int i=0; i<files.length; i++) {
    14. if(files.isDirectory()) {
    15. deleteDirectory(files);
    16. }
    17. else {
    18. files.delete();
    19. } //end else
    20. }
    21. }
    22. return( path.delete() );
    23. }
    24.  


    Thanks though for the code. Hopefully you can help with these errors?
     
  4. Hi!
    Worldpath is basically the string that represents the name of the world, in your case
    Code:java
    1. getServer().getWorld(player.getName() + "-world").getWorldFolder().getPath();

    config is a FileConfiguration fetched from getServer().getConfig() Which you do not need to worry about, that code won't work of the shelf for your plugin as it's written for mine, The only thing that should work for you directly is the deleteDirectory, but you are passing the File as a array (File[] Remove the [] so it's just 1 file object and it should work just fine to run that function. server is indeed getServer():

    I'd suppose your code would look something like
    Code:java
    1. if((args.length >= 1) && args[0].equalsIgnoreCase("delete"){
    2. World playerworld = bukkit.getWorld(player.getName() + "-world");
    3. File folder = playerWorld.getWorldFolder();
    4. if(new File(playerWorld.getWorldFolder().getPath() + "/level.dat").exists()){
    5. getServer().unloadWorld(playerworld);
    6. deleteDirectory(folder);
    7. }
    8. }
    9.  
    10. public static boolean deleteDirectory(File path) {
    11. if( path.exists() ) {
    12. File[] files = path.listFiles();
    13. for(int i=0; i<files.length; i++) {
    14. if(files.isDirectory()) {
    15. deleteDirectory(files);
    16. }
    17. else {
    18. files.delete();
    19. } //end else
    20. }
    21. }
    22. return( path.delete() );
    23. }
    24.  
     
    ThunderWaffeMC likes this.
  5. Offline

    ThunderWaffeMC

    Raz sorry for the late reply. My internet was down for a while.

    So I have changed it to:

    File files = path.listFiles();

    but I get an error on "path.listFiles()" (most likely because it's listing the files but it's just the one). The error is: Type mismatch: cannot convert from File[] to File

    I also now get an error on "length" from "for(int i=0; i<files.length; i++) {" saying it cannot be resolved or is not in a field.

    Any ideas? Thanks!
     
  6. Offline

    xTrollxDudex

    ThunderWaffeMC
    Can't you just set the world file to null?

    Edit:
    Did you try to cast (File) to the line that said File[]
    What's file.length!? Is it supposed to be file.size()?
     
  7. Offline

    ThunderWaffeMC

    xTrollxDudex How do I cast (File) to the other line?

    Also, I think setting the world folder to null would be to sketchy. It's more organized and safe to use the other methods. How can I use files.size? It would just be the same error (wouldn't it?)
     
  8. Offline

    xTrollxDudex

    ThunderWaffeMC
    1.
    Code:java
    1. File[] files = (File) path.listFiles();
    try that

    2.it's size(), your hurting .size()'s feelings :) and did you try it yet? I'm not experienced with java I/O too much

    3.why would setting the file to null be sketchy? It would pretty much be like setting a minecraft block to air to represent nothing (unless I'm missing something a noob I/O coder doesn't know)
     
  9. Offline

    ThunderWaffeMC

    xTrollxDudex Hm. Both of the methods you gave me (the cast and the size()) doesn't work. I'll just wait for Raz to reply since he's the one that knows about the code since he wrote it.
     
  10. Offline

    xTrollxDudex

  11. Offline

    Firefly

    Why in the world (no pun intended) would you cast a File array to a file? Do this to retrieve the individual file from the array.

    Code:
     File[] files = path.listFiles();
        for(int i=0; i<files.length; i++) {
        if(files[i].isDirectory()) {
        deleteDirectory(files[i]);
        }
    }
     
    ThunderWaffeMC likes this.
  12. ThunderWaffeMC
    I use deleteDirectory(File path) in my plugin which is live and used by hundreds, the error is not in the function code but rather in the argument you pass to it. Do you mind putting all of the code in your file on pastebin.com?
     
    ThunderWaffeMC likes this.
  13. Offline

    Firefly

    It's because he's passing in files which is a File[] rather than doing
    Code:
    files[i]
    to get a single File object.
     
    ThunderWaffeMC likes this.
  14. Offline

    ThunderWaffeMC

    Raz likes this.
Thread Status:
Not open for further replies.

Share This Page