Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

So, I understand that files cannot be created or deleted without administrator privileges in the C:\ drive. My question is this : If a directory in the C:\ drive is entered..say Program Files, then shouldn't file writing be possible there?

I have created a program which deletes empty directories in an entire folder(i.e includes subfolders)

I get that C:\ drive requires permissions, but once my program finds Program Files to be a "not-empty" directory, it enters Program Files but is still denied write permissions. I know this because I made sure the program lists every directory it spots in the given path. So, EVERY folder present in the C:\ drive is printed. No folders are deleted though.

Whereas when I set the path directly to C:\Program Files it performs the task of deleting empty directories as per the code.

Why is this so?

I wrote a batch file which I ran as the administrator. As you might have guessed, no luck. I tried running it through cmd in admin mode, nothing again. Is there a way I can change the .class file of my code to run with admin privileges. Or is there a way to run it in cmd with admin privileges. Say

java emptyFoldersRemover -runAsAdmin

Something like that?

Or do I have to make an executable file?

[EDIT]

I stress on this point again. Isn't there a way to run java.exe with administrator permissions in cmd so that the program is treated with administrator privileges?

[CODE]

String absolutePath=p;
    try{
    path=new File(absolutePath);
    File listOfFolders[] = path.listFiles();
    for(int i=0;i<listOfFolders.length;i++)
        if(listOfFolders[i].isDirectory())
         System.out.println(listOfFolders[i]);
     for (int i = 0; i < listOfFolders.length; i++) 
      {
       if (listOfFolders[i].isDirectory()) 
       {
           if(listOfFolders[i].list().length>0){
              /* if(listOfFolders[i].getName().equalsIgnoreCase("Program Files"))
               {
                   for(int j=0;j<listOfFolders[i].list().length;j++)
                   {
                       System.out.println(listOfFolders[i].list());
                   }
               }*/
               run(listOfFolders[i].getAbsolutePath());
           }
           else
           {
               noOfFolders++;
               System.out.println(noOfFolders);
               listOfFolders[i].delete();
           }
          }
      }
    }
    catch(Exception e)
    {
        //e.printStackTrace();
    }
     return(noOfFolders);
share|improve this question

migrated from stackoverflow.com Dec 19 '12 at 8:03

This question came from our site for professional and enthusiast programmers.

    
The Program Files directory gets special treatment in several ways. This is one of them. –  IronMensan Dec 19 '12 at 3:04
    
But why aren't the empty folders removed from Program Files when I set the path to "C:\\" and not "C:\\Program Files"? –  Torcellite Dec 19 '12 at 3:10
1  
Your string initialization has commas, are they supposed to be "+" for concatenation? –  Torcellite Dec 19 '12 at 3:23
1  
@Torcellite You are right. Let me take it down. –  smit Dec 19 '12 at 3:27
    
You have multiple options. 1. Reset C drive persmissions, 2. Add your user with write privileges on C drive 3. Use "user.dir" to write your information .4. impersonate yourself as admin user, runas command etc. .5. Provide a bat or vb script which you can right click and run as administrator, see mockbox.net/windows-7/… –  Usman Saleem Dec 19 '12 at 3:47

3 Answers 3

What you are asking is if there is a way to run your application "elevated". To do that, you can right click on the cmd that you are using to launch the application and choose Run as Administrator.

If you want to always launch the app elevated, right click on the cmd file, properties, compatibility, then check the "Always run as Administrator" checkbox.

From the comments on your question, it sounds like you've tried this and it didn't work for you - but it most definitely does work, so let's focus on why it may not have worked for you - are you allowed to elevate? Have you, by any chance, disabled UAC Notifications? Users often think that by disabling UAC Notifications that they are disabling UAC itself - that is not the case. Disabling notifications just makes it so the OS can't prompt you to elevate, so it silently fails the elevation.

share|improve this answer
    
I restored my UAC to the default setting and tried what you said. It failed again. I'll post my code now. –  Torcellite Dec 19 '12 at 5:50

for your second point

open dos prompt as administrator and then run your java.exe or any other exe.

it will run as admin

share|improve this answer
    
Tried that, doesn't work. –  Torcellite Dec 19 '12 at 6:09

I've used Elevate to run privileged Java programs, and it works great. You just have to make sure the elevate.exe utility is on the PATH, then prefix your original command with elevate:

C:\Program Files>elevate java emptyFoldersRemover

This will trigger a UAC prompt. If you choose to permit it, your program will then be run with elevated privileges. Be sure the architecture of the "elevate" binary that you use matches the architecture of the JRE that you're using (for example, if you're using a 64-bit java.exe, make sure you elevate it with the 64-bit version of elevate).

Note that some antivirus/anti-malware programs detect elevate.exe's fishy behavior and will quarantine or delete it automatically.

share|improve this answer
    
Sorry, that doesn't seem to work. It actually kills my program instantly. –  Torcellite Dec 22 '12 at 13:11
    
By kills my program instantly, do you mean when you launch your program using the elevate utility, your program just doesn't run? –  rob Dec 23 '12 at 1:13
    
Yeah. I'm not sure if it does pop up and gets terminated instantly or just doesn't start up. –  Torcellite Dec 23 '12 at 3:11
    
Can you vote the question up? I don't have enough reputation points for offering bounty. –  Torcellite Dec 23 '12 at 3:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.