Ragnarsson
Ragnarsson

Reputation: 1825

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 [email protected]/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

Upvotes: 3

Views: 2144

Answers (4)

Isma
Isma

Reputation: 15180

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");

Upvotes: 1

Sheece Gardazi
Sheece Gardazi

Reputation: 567

rm is a command for Linux command line equivalent command on windows is del command:

    C:\>del /?
    Deletes one or more files.

    DEL [/P] [/F] [/S] [/Q] [/A[[:]attributes]] names
    ERASE [/P] [/F] [/S] [/Q] [/A[[:]attributes]] names

      names         Specifies a list of one or more files or directories.
                    Wildcards may be used to delete multiple files. If a
                    directory is specified, all files within the directory
                    will be deleted.

      /P            Prompts for confirmation before deleting each file.
      /F            Force deleting of read-only files.
      /S            Delete specified files from all subdirectories.
      /Q            Quiet mode, do not ask if ok to delete on global wildcard
      /A            Selects files to delete based on attributes
      attributes    R  Read-only files            S  System files
            H  Hidden files               A  Files ready for archiving
            I  Not content indexed Files  L  Reparse Points
            -  Prefix meaning not

To delete a folder use:

      DEL /F /S /Q /A "Full Path of Folder\*"

Reference: sevenforums

Upvotes: 1

paulselles
paulselles

Reputation: 698

Originally added this as a comment but upgrading to an Answer for more visibility.

Execute the rm command on the Windows server over ssh. This will require you setting up an ssh server on Windows, cygwin looks to be one of the best options. Once you has the ssh server setup executing the remote rm command will use the command ssh [email protected] "rm -rf /cygdrive/c/Temp".

Upvotes: 1

Roman Gherta
Roman Gherta

Reputation: 840

in Windows try

rmdir directoryname /s /q

as per documentation https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/rd

/s Deletes a directory tree (the specified directory and all its subdirectories, including all files).

/q Specifies quiet mode. Does not prompt for confirmation when deleting a directory tree. (Note that /q works only if /s is specified.)

How do you run this Windows command from a Linux station... good question

Upvotes: 1

Related Questions