简体   繁体   English

如何从Linux中删除远程Windows中的文件夹

[英]How to delete a folder in remote Windows from Linux

I am running automation tests on both local (Linux) and remote Selenium node (Windows). 我正在本地(Linux)和远程Selenium节点(Windows)上运行自动化测试。 And I want to delete a folder created during test, using Java Runtime.getRuntime().exec . 我想使用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. 它在本地(Linux)上工作正常,但我很难弄清楚如何在Windows节点上执行此操作。 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. 我尝试删除远程Windows上的文件夹C:/ Temp,但我没有任何成功。 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 谢谢

in Windows try 在Windows中尝试

rmdir directoryname /s /q

as per documentation https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/rd 根据文档https://docs.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). / s删除目录树(指定的目录及其所有子目录,包括所有文件)。

/q Specifies quiet mode. / q指定安静模式。 Does not prompt for confirmation when deleting a directory tree. 删除目录树时不提示确认。 (Note that /q works only if /s is specified.) (注意/ q仅在指定了/ s时有效。)

How do you run this Windows command from a Linux station... good question 你如何从Linux工作站运行这个Windows命令......好问题

Originally added this as a comment but upgrading to an Answer for more visibility. 最初将此添加为评论,但升级到答案以获得更多可见性。

Execute the rm command on the Windows server over ssh. 通过ssh在Windows服务器上执行rm命令。 This will require you setting up an ssh server on Windows, cygwin looks to be one of the best options. 这将要求您在Windows上设置ssh服务器,cygwin看起来是最好的选择之一。 Once you has the ssh server setup executing the remote rm command will use the command ssh IEUser@10.2.2.240 "rm -rf /cygdrive/c/Temp" . 一旦你有ssh服务器设置执行remote rm命令将使用命令ssh IEUser@10.2.2.240 "rm -rf /cygdrive/c/Temp"

rm is a command for Linux command line equivalent command on windows is del command: rm是Windows上命令行等效命令的命令是del命令:

    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 参考:七个论坛

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. 实现此目的的另一种方法是直接从Web服务器执行此操作,方法是向您的网站添加方法以清理资源。

For example: http://your_server/clean_resources 例如: http:// your_server / clean_resources

Then, just use Java to delete the folder: 然后,只需使用Java删除该文件夹:

// 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. 最后,使用Selenium,只需在完成测试后导航到此URL。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM