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
  • 5,096

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
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,388
-1

for your second point

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

it will run as admin