javalinuxwindowsshelldelete-directory

How to delete a folder in remote Windows from Linux


I am running automation tests on both local (Linux) and remote Selenium node (Windows). And I want to delete a folder created during test, using Java Runtime.getRuntime().exec. It works fine on local (Linux), but I have a hard time to figure out how to do it on Windows node. The following is my attempts:

try {
    if (rBundle.getString("RUN_ON").equalsIgnoreCase("local")) // delete folder temp on local (Linux) - it works
        Runtime.getRuntime().exec("rm -rf " + System.getProperty("user.home") + "/Temp");
    else // delete folder C:/Temp on remote Windows
        Runtime.getRuntime().exec("rm -rf IEUser@10.2.2.240/C/Temp");
        // Runtime.getRuntime().exec("rm -rf //10.2.2.240/C/Temp");
} catch (IOException e) {
    e.printStackTrace();
}

I try to delete folder C:/Temp on the remote Windows, but I don't have any success. I don't get any Exceptions, it went though that block. Obviously the command line is wrong, but I have no idea.

Any help much appreciated. Thanks


Solution

  • Another way to achieve this could be to do it directly from the Web server by adding a method to your Website to clean resources.

    For example: http://your_server/clean_resources

    Then, just use Java to delete the folder:

    // You could pass a parameter to the URL to know if it's windows  
    // or linux and set the path accordingly
    String path = "c:/temp";
    
    Path directory = Paths.get(path);
        Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {
    
            @Override
            public FileVisitResult visitFile(Path file,
                    BasicFileAttributes attrs) throws IOException {
                Files.delete(file);
                return FileVisitResult.CONTINUE;
            }
    
            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc)
                    throws IOException {
                Files.delete(dir);
                return FileVisitResult.CONTINUE;
            }
        });
    

    Finally, using Selenium, just navigate to this URL when you finish your test.

    driver.get("http://your_server/clean_resources");