代码之家  ›  专栏  ›  技术社区  ›  Ragnarsson

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

  •  3
  • Ragnarsson  · 技术社区  · 7 年前

    我正在本地(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,但没有成功。我没有任何例外,它穿过了那个街区。显然命令行是错误的,但我不知道。

    非常感谢您的帮助。谢谢

    4 回复  |  直到 7 年前
        1
  •  1
  •   Isma    7 年前

    实现这一点的另一种方法是直接从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
  •  1
  •   Roman Gherta    7 年前

    在Windows中尝试

    rmdir directoryname /s /q
    

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

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

    /q指定安静模式。删除目录树时不提示确认。(请注意,只有在指定了/时,/q才起作用。)

    如何从Linux工作站运行此Windows命令。。。问得好

        3
  •  1
  •   paulselles    7 年前

    最初将此作为评论添加,但为了更具可见性,升级为答案。

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

        4
  •  1
  •   Sheece Gardazi    7 年前

    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

    推荐文章