0

I've been stuck on this thing for at least a full month now.

What I have, is a code that specifies a directory, and then checks if the directory is empty. If it's not, it deletes all the files inside it and leaves the directory folder. I'm using this to clean stuff like temporary files, and the recycle bin.

The way I did it, is I declared a variable called "SRC_FOLDER" inside a private method, which has the value of the directory I want cleaned. And then in a public method, I wrote the rest of the code.

Here is the full code:

import java.io.File;
import java.io.IOException;

public class test {
private static final
    String SRC_FOLDER = "C:\Users\Denisowator\AppData\Local\Temp";

public static void main(String[] args) {
      File directory = new File(SRC_FOLDER);

        //Check if directory exists
        if(!directory.exists()) {
           System.out.println("Directory does not exist.");
           System.out.println("Skipping directory.");
           System.exit(0);
        }
        else {

           try {
           delete(directory);
           }

           catch(IOException e){
               e.printStackTrace();
               System.exit(0);
           }
        }

        System.out.println("Cleaned directory" + SRC_FOLDER + ".");
    }

    public static void delete(File file)
        throws IOException{

        if(file.isDirectory()){

            //If directory is empty
            if(file.list().length==0){
                System.out.println("Directory is empty")
            }
        }
        else {
            //If file exists, then delete it
            file.delete();
            System.out.println("File is deleted : " + file.getAbsolutePath());
        }
    }
  }

As you can see, the variable is used as a value for a file, which is essentially the folder I want cleaned, it then checks the file's length (amount of files inside it), and if the length is above zero, the files are deleted.

It all works fine, but what I have a problem with, is specifying the same directory, based on what user executes the code.

Let's say I give this program to someone, and their name is "Steve" or something like that. The code won't work, because it's looking for a user "Denisowator" in the Users folder. So how can I make the value change, based on what the user's username is?

I've looked into things like "user.home" and "user.dir", but I have no idea how I would implement that into a variable's value.

  • 1
    Use the [`user.name`](https://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html) environment variable. – Andy Turner Oct 25 '16 at 11:23
  • Would I simply implement this in the place of "Denisowator"? So it would be "C:\Users\user.name\AppData\Local\Temp". – Denisowator Oct 25 '16 at 11:25
  • You should take a look at this: http://stackoverflow.com/questions/585534/what-is-the-best-way-to-find-the-users-home-directory-in-java – Titus Oct 25 '16 at 11:26
  • @Denisowator yep. – Andy Turner Oct 25 '16 at 11:26
  • @Titus The answer that was accepted in the link you gave, uses "user.home" which takes you to the "AppData\Roaming" folder, instead of the "AppData\Local" one. – Denisowator Oct 25 '16 at 11:29
  • 1
    I see, in that case, if you need this to work only on Windows systems, you can do something like this: `System.getenv("AppData") + "\\Local\\Temp"` or `System.getProperty("user.home").replace("Roaming", "Local\\Temp")` – Titus Oct 25 '16 at 11:36

1 Answers1

1

Create your variable by concatenating the value of "user.home" with the static sub path:

String SRC_FOLDER = System.getProperty("user.home") + "\AppData\Local\Temp";

You can also create the variable via environment variable:

String SRC_FOLDER = System.getenv("LOCALAPPDATA") + "\Temp";

However, the latter will get a valid folder only on windows.

Stefan
  • 444
  • 5
  • 16
  • Thank you, I can't test this right now, as I'm at college. But when I get home, I'll try both, as well as Andy's suggestion in the question's comments. – Denisowator Oct 25 '16 at 11:37
  • Just tested it. It worked, but I have a question. Will it only clear the current user's directory? Or will it clear that same directory for every user? I'm the only user on my PC so I can't test it right now unless I make a new account. – Denisowator Oct 25 '16 at 18:44
  • btw @Andy Turner It just said the directory didn't exist, and didn't clear it. Thanks for the help anyway. – Denisowator Oct 25 '16 at 18:45
  • 1
    @Denisowator The variable is set only for the current users directory. So the code will only affect the user directory of the logged in user. – Stefan Oct 25 '16 at 19:15