3

So, I understand that files cannot be created or deleted without administrator privileges in the C:\ drive. My question is like 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 that 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, but 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?

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);
Rohit Gupta
  • 3,499
  • 1
    The Program Files directory gets special treatment in several ways. This is one of them. –  Dec 19 '12 at 03:04
  • But why aren't the empty folders removed from Program Files when I set the path to "C:\" and not "C:\Program Files"? –  Dec 19 '12 at 03:10
  • 1
    Your string initialization has commas, are they supposed to be "+" for concatenation? –  Dec 19 '12 at 03:23
  • 1
    @Torcellite You are right. Let me take it down. – smit Dec 19 '12 at 03: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 http://www.mockbox.net/windows-7/354-windows-7-run-vbs-script-as-elevated-user-uac – Usman Saleem Dec 19 '12 at 03:47
  • I tried the fifth option, doesn't work. I don't want to do anything as drastic as resetting drive permissions. So options 1 and 5 are out.

    The third option seems viable - is this how I go about it? http://gcc.gnu.org/ml/java/2000-q1/msg00406.html

    The fourth command showed up on a lot of searches, I have had no luck using this method. How do I go about it?

    –  Dec 19 '12 at 03:51
  • What operating system are you on that blocks write permissions to C: but not C:\Program Files\ ? Something sounds odd here... The behavior you describe is also strange (traversing into C:\Program Files\ fails, while starting with C:\Program Files\ succeeds). I think you are going to have to post your code - something sounds fishy... –  Dec 19 '12 at 04:43
  • @smit That's resetting user permissions for a file on your Desktop. I want to give my program administrator privileges so that it can edit files or folders in the C:\ drive itself. –  Dec 19 '12 at 05:51
  • @Torcellite I tried to reset permission for write on c:\ but I could not change for system user. And I am not getting proper command to get admin priviledges.let me delete my previous comment. – smit Dec 19 '12 at 05:59
  • @smit You should vote up the question so that more users can see it. This one really has me stumped. –  Dec 19 '12 at 06:06
  • @Torcellite I will do that. but you need to add bounty to this question. – smit Dec 19 '12 at 06:07
  • @smit I will be allowed to add bounty after 2 days. –  Dec 19 '12 at 06:09
  • @Torcellite You have no option to wait until 2 days. However till that time try to find way around. My way is working but not in C:\ to change permission to file and directory. Better you do some research on it. If you want I can keep my answer open but its not useful for c:\ so I deleted it. – smit Dec 19 '12 at 06:13
  • @smit That's alright. I'll try finding a way. –  Dec 19 '12 at 06:27

4 Answers4

0

I can only give a direction (I think) as I once wrote a defragger, decades ago and I ran into problem not being able to even access some folders even when running 'elevated'. I can not remember specifics but my tool needed to acquire "SE_RESTORE_NAME" privilege, see https://learn.microsoft.com/en-us/windows/win32/secauthz/privilege-constants#example.

Once I had that figured out I could basically access anything and do with files whatever I wanted.

SE_RESTORE_NAME TEXT("SeRestorePrivilege")

Required to perform restore operations. This privilege causes the system to grant all write access control to any file, regardless of the ACL specified for the file. Any access request other than write is still evaluated with the ACL. Additionally, this privilege enables you to set any valid user or group SID as the owner of a file. This privilege is required by the RegLoadKey function.

The following access rights are granted if this privilege is held:

WRITE_DAC

WRITE_OWNER

ACCESS_SYSTEM_SECURITY

FILE_GENERIC_WRITE

FILE_ADD_FILE

FILE_ADD_SUBDIRECTORY

DELETE

Sorry, as I said I can not remember specifics but hope it helps you in the right direction.

0

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.

Kevin Day
  • 101
  • I restored my UAC to the default setting and tried what you said. It failed again. I'll post my code now. –  Dec 19 '12 at 05:50
0

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.

rob
  • 14,228
-2

for your second point

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

it will run as admin