简体   繁体   English

使用Batch脚本清除selenium测试中的临时文件夹

[英]Using Batch script to clear temp folder in selenium tests

Running a lot of selenium tests causes the temp folder to be filled up with lot 'anonymous-web-driver' profiles in the case of firefox and 'scoped-dirs' in the case of chrome. 运行大量的selenium测试会导致temp文件夹在firefox的情况下填充很多'anonymous-web-driver'配置文件,在chrome的情况下会填充'scoped-dirs'。

To deal with this I came up with the following batch script code 为了解决这个问题,我提出了以下批处理脚本代码

@echo off
cd %temp%
for /d %%D in (*) do rd /s /q "%%D"
del /f /q *

I have the following issues with it 我有以下问题

1) It does the job successfully but when the batch script is run on a network drive, it deletes all the files in the current folder since cd %temp% does not navigate to the temp as there is no temp folder. 1)它成功完成了工作,但是当批处理脚本在网络驱动器上运行时,它会删除当前文件夹中的所有文件,因为cd %temp%没有导航到临时文件,因为没有临时文件夹。

Is there anyway to ensure that lines 3 and 4 are executed only when the current directory is temp. 无论如何都要确保仅当当前目录为temp时才执行第3行和第4行。 Since the script is stored on a network drive I want to ensure that even if it is run by accident it does cause any unintentional deletion. 由于脚本存储在网络驱动器上,我想确保即使它意外运行也会导致任何意外删除。

2) Since some of the folders cannot be deleted in the temp , the cmd window hangs there saying that these folders could not be deleted.I am fine with the files that cannot be deleted but I want to close the cmd window since I have hundreds of tests to run and each test opening up a cmd window is pretty ugly. 2)由于某些文件夹无法在临时文件中删除,因此cmd窗口挂起来说这些文件夹无法删除。我很好用无法删除的文件但我想关闭cmd窗口,因为我有数百个测试运行和每个测试打开一个cmd窗口是非常难看的。

I tried the following Runtime.getRuntime().exec("taskkill /f /im cmd.exe"); 我尝试了以下Runtime.getRuntime().exec("taskkill /f /im cmd.exe"); and it works fine except for the following fact that it kills all cmd processes there are other cmd processes that does some work.Are there ways by which I can close only the cmd window opened by the runtime exec call? 并且它工作正常,除了以下事实,它杀死所有cmd进程,还有其他cmd进程做了一些工作。有哪些方法可以关闭运行时exec调用打开的cmd窗口?

setlocal enableextensions

    pushd "%temp%"
    if not errorlevel 1 (
        rmdir . /s /q >nul 2>nul
        popd
    )

And since current directory can't be deleted, it will delete all files and directories not locked. 由于无法删除当前目录,因此将删除未锁定的所有文件和目录。

 if not "%temp%"=="%CD%" goto :eof 

每当批处理文件到达此行时,它将测试当前目录是否等于临时目录,如果不退出批处理文件。

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

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