0

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?

Greedy Coder
  • 1,256
  • 1
  • 15
  • 36

2 Answers2

2
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.

MC ND
  • 69,615
  • 8
  • 84
  • 126
  • Works well..what is the concept behind this? Just want to know – Greedy Coder Oct 30 '13 at 12:31
  • For the second part I could not find a solution..overcame it by using thread signalling and closed all cmd processes after work is completed. – Greedy Coder Oct 30 '13 at 12:35
  • 1
    Pushd "stores" the current directory in memory and changes to the drive/directory that is indicated, generating an errorlevel if this can't be done. If no errorlevel, try to remove current directory recursively. It starts deleting anything (files, folders) below. Once if finishes, tries to delete current dir, but since we are using it it can´t be deleted. Then `popd` retrieves the stored directory from memory to return to original directory. – MC ND Oct 30 '13 at 12:38
  • For the second part: rmdir does not wait for file/folder deletion nor blocks (only while working) cmd window. If the window keeps open the problem is in other place. How do you start the batch file ? – MC ND Oct 30 '13 at 12:40
  • It gets called automatically by a method.Since I had access to the other cmd processes,I leveraged threading and synchronized accordingly. – Greedy Coder Oct 30 '13 at 12:45
  • 1
    I've rereading the original question, and my question is the same, how is the batch file called? by a method you says, but, What code executes de batch? the exact function call, parameters passed, ... Anyway, try to add `exit` at the end of your batch file to "ensure?" window gets closed. – MC ND Oct 30 '13 at 12:54
  • My original batch script was the one...I was calling it from java using Runtime.exec as cmd /c start "batch file name".When the files could not be deleted the cmd window was open saying the message.But now after adding exit it works fine.Thanks so much and sorry for not telling it in the first place, I was not paying attention. – Greedy Coder Oct 30 '13 at 13:07
0
 if not "%temp%"=="%CD%" goto :eof 

Whenever the batch file hits this line it tests if the current directory is equal to the temp directory and if not exits the batch file.

David Candy
  • 735
  • 5
  • 8