简体   繁体   中英

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.

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.

Is there anyway to ensure that lines 3 and 4 are executed only when the current directory is temp. 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.

I tried the following 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?

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 

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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