0

I trying to delete the folder and the files inside the folder, if i execute the command (rmdir /s /q folderpath) it works fine, but when u run the below code, it's unable to delete the file, can you please help me where I am doing wrong?

String folderPath = "C:\\Users\\"+userName+"\\Documents\\test\\";

void deleteDir()
{
    int flag;
    
    File f = new File(folderPath);
    
    try
    {
        Runtime.getRuntime().exec("adb kill-server");
        Runtime.getRuntime().exec("cmd /c rmdir /s /q "+f);
    }
    catch(Exception e) {e.printStackTrace();
}
Azzi
  • 1
  • 3
  • There a reason you're not just calling file.delete() ? – Roddy of the Frozen Peas Jul 11 '20 at 01:05
  • I tried the file.delete(), even with that i see the same issue. – Azzi Jul 11 '20 at 01:42
  • You haven't explained the issue. Does it give you an error? Does this other Q&A not work? https://stackoverflow.com/questions/20281835/how-to-delete-a-folder-with-files-using-java – Roddy of the Frozen Peas Jul 11 '20 at 01:43
  • Actual path of the folder is like this - C:\Users\username\OneDrive - companyname\Documents\test\. I turned off the OneDrive sync as well. If I run the same Java script against a folder which is not under OneDrive, it's working. Only I have problem with the folders under OneDrive. But I can delete the folder under one drive manually or by running the same command in cmd. I wonder why it is not working when i run Java program – Azzi Jul 11 '20 at 01:45
  • Read the process' errorstream, and maybe its 'input' stream (Java `Process.getInputStream()` is the output from the perspective of the child), to see what it says is happening and if there is a problem what that problem is. – dave_thompson_085 Jul 11 '20 at 03:12
  • The problem in your comment example is space in folder name, include " before and after folder in exec arg. – Dyorgio Nov 03 '20 at 21:54

0 Answers0