# exporting results from batch file



## tommo020788 (Oct 20, 2008)

Hi there, I am trying to achieve a pretty simple task here, i'm just cant seem to think how to do what i want properly though...

I currently have a batch file that looks like the following:
@echo off
color 91
DEL /F file.exe
DEL /F file.exe
DEL /F file.exe >log.txt

basicly it attempts to delete the same file a few times in a row.
I've tried to make it so it creates a log with the results, but it didnt seem to work... 

I would like to have it check for when it has success, then creates a log with the "file has been successfully deleted" message that comes up after it, then have it exit the batch file.

Then If it does not have success by the end of the batch, I want it to create a log with the error.



Is there anyone who has experience in batch file creation that could help me out here?


----------



## tommo020788 (Oct 20, 2008)

here is what i've put together so far:



> if exist test (del test & echo. test deleted > log.txt) & exit
> else
> if exist test (del test & echo. test deleted > log.txt) & exit
> else
> (echo. Failure! "test" does not seem to exist, or cannot be deleted. >> log.txt)


I'm still having a problem with this though...
It is creating two seperate log files. one from the first line, and one from the second.

anyone know why its doing this?


----------



## TheOutcaste (Aug 8, 2007)

First suggestion is don't use *exit* while testing batch files. More often than not, it not only exits the batch file, but closes the Command Prompt so you won't see any errors. Use *Goto :EOF* instead. Once it's working, you can do a search and Replace to replace *Goto :EOF* with *Exit*.
Second, run the batch file from a Command Prompt rather than double clicking on it, so the window will stay open if there is an error.

I suspect that's why you didn't see the error caused by the else statement:

```
C:\Test Dir>tout

C:\Test Dir>if exist test (del test   & echo. test deleted  1>log.txt )  & exit

C:\Test Dir>else
[COLOR=Red]'else' is not recognized as an internal or external command,
operable program or batch file.[/COLOR]

C:\Test Dir>if exist test (del test   & echo. test deleted  1>log.txt )  & exit

C:\Test Dir>else
[COLOR=Red]'else' is not recognized as an internal or external command,
operable program or batch file.
[/COLOR]
C:\Test Dir>(echo. Failure! "test" does not seem to exist, or cannot be deleted.  1>>log.txt )

C:\Test Dir>
```
The Else statement has to be on the same line as the If statement, either physically, or by using Line continuation (which is more readable as well):

```
if exist test (del test & echo. test deleted > log.txt & exit) else if exist test (del test & echo. test deleted > log.txt & exit) else echo. Failure! "test" does not seem to exist, or cannot be deleted. >> log.txt
```
or this

```
if exist test (
  del test
  >log.txt echo.test deleted
  Goto:EOF
  ) else (
    if exist test (
    del test
    >log.txt echo.test deleted
    Goto:EOF
    ) else (
    >>log.txt echo.Failure! "test" does not seem to exist, or cannot be deleted.
  )
)
```
If you run this and test exists, it's deleted and the log file gets one line:
" test deleted "
If it doesn't exist, you'll get this line:
" Failure! "test" does not seem to exist, or cannot be deleted. "
If you run it multiple times, the Failure message gets added to the file, as you are using >> instead of > for Redirection on that one line. Not sure if that was what you intended.
Also note that there is a leading and trailing space. The trailing space is because of the space between the text and the redirection symbol. The leading space is because you used a period after the Echo statement.
So use one of these if those spaces are not wanted:
*Echo test deleted>log.txt
Echo.test deleted>log.txt*
You can also avoid the trailing space when using Echo by placing the redirection at the start of the line as I've done above:
*>log.txt echo.test deleted*
Easiest to use this method when the statements are on their own lines.

This will only give the failure message if the file does not exist, it won't show you if the file was not deleted for some reason. So you first need to check if the file exists, if it does, then try to delete, then check to see if it still exists.

This should do what you want. You might want to add a small delay between the delete attempts just in case a program is a bit slow in releasing a file lock. I've used Ping to create a 1 second delay:

```
Set _File2Del=test
Set _LogFile=Log.txt
If Not Exist "%_File2Del%" Echo.Failure! "%_File2Del%" does not seem to exist>"%_LogFile%"&Goto:EOF
>"%_LogFile%" Echo.Attempting to delete "%_File2Del%"
If Exist "%_File2Del%" Del /F "%_File2Del%" >>"%_LogFile%" 2>&1
If Exist "%_File2Del%" (
  >>"%_LogFile%" Echo.First attempt to delete "%_File2Del%" Failed. Trying again
  Ping 1.0.0.0 -n 1 -w 1000 > nul
  Del /F "%_File2Del%" >>"%_LogFile%" 2>&1
  ) Else (
  >>"%_LogFile%" Echo."%_File2Del%" was succesfully deleted
)  
If Exist "%_File2Del%" Echo.Failure! "%_File2Del%" cannot be deleted.>>"%_LogFile%"
```
HTH

Jerry


----------

