【问题标题】:How to delete a folder in remote Windows from Linux如何从 Linux 删除远程 Windows 中的文件夹
【发布时间】:2018-10-15 02:44:28
【问题描述】:

我正在本地 (Linux) 和远程 Selenium 节点 (Windows) 上运行自动化测试。我想删除一个在测试期间创建的文件夹,使用 Java Runtime.getRuntime().exec。它在本地(Linux)上运行良好,但我很难弄清楚如何在 Windows 节点上做到这一点。以下是我的尝试:

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();
}

我尝试删除远程 Windows 上的文件夹 C:/Temp,但没有成功。我没有得到任何异常,它通过了那个块。显然命令行是错误的,但我不知道。

非常感谢任何帮助。谢谢

【问题讨论】:

  • 你只需要找到一个可以在你的shell中运行的命令行,然后在你的Java代码中找到exec()。这根本不是 Java 问题,而是 Linux 和 Windows 问题,而且可能更适合 superuser.com 的主题。
  • @EJP 感谢您的提示
  • 基于 EJP 的评论,如果您真的希望能够从 Linux 机器上执行此操作,您可能希望在该机器上设置 Samba。
  • 您可以尝试通过 ssh 执行命令,这将使命令成为 ssh IEUser@10.2.2.240 "rm -rf /C/Temp"。如果您的 Windows 机器上没有 ssh 服务器设置,则 cygwin 是一种可能的解决方案,其中包含许多在线设置指南。
  • 当你说你在使用 Selenium 时,我假设你是在两个不同的服务器上测试一个网站,对吧?

标签: java linux windows shell delete-directory


【解决方案1】:

实现此目的的另一种方法是直接从 Web 服务器执行此操作,方法是向您的网站添加一个方法来清理资源。

例如:http://your_server/clean_resources

然后,只需使用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;
        }
    });

最后,使用 Selenium,只需在完成测试后导航到此 URL。

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

【讨论】:

  • 谢谢,我认为这是一个接近的解决方案,我可以摆脱命令行
【解决方案2】:

在 Windows 中尝试

rmdir directoryname /s /q

根据文档 https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/rd

/s 删除目录树(指定目录及其所有子目录,包括所有文件)。

/q 指定安静模式。删除目录树时不提示确认。 (请注意,/q 仅在指定 /s 时才有效。)

如何从 Linux 站运行这个 Windows 命令...好问题

【讨论】:

    【解决方案3】:

    最初将此作为评论添加,但升级为答案以获得更多可见性。

    通过 ssh 在 Windows 服务器上执行 rm 命令。这将要求您在 Windows 上设置 ssh 服务器,cygwin 看起来是最好的选择之一。设置好 ssh 服务器后,执行远程 rm 命令将使用命令ssh IEUser@10.2.2.240 "rm -rf /cygdrive/c/Temp"

    【讨论】:

      【解决方案4】:

      rm是Linux命令行的命令,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
      

      要删除文件夹,请使用:

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

      Reference: sevenforums

      【讨论】:

        猜你喜欢
        • 2020-10-11
        • 1970-01-01
        • 2021-12-22
        • 1970-01-01
        • 1970-01-01
        • 2020-02-04
        • 2012-08-29
        • 1970-01-01
        • 2010-10-01
        相关资源
        最近更新 更多