Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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.txtbasicly 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?

You can catch DEL command failure by coding
del File_Name 2>> log.txt
but you can't directly get a log for its success. A workaround is
if exist File_Name (del File_Name & echo. File_Name deleted >> log.txt)
By the way this is NT batch scripting that has nothing to do with DOS (that doesn't exist in XP systems where the kernel is NT based and the prompt operated by cmd.exe not command.com).

hmmmm couldnt you through an "else" variable or something in there after that then?
so it looks something like this:if exist File_Name (del File_Name & echo. File_Name deleted >> log.txt)
else
echo. File_Name does not exist, or cannot be deleted >> log.txt)

also, could I add onto that code something like this?
if exist test (del test & echo. test deleted & exit>> log.txt)
else (echo. Failure! "test" does not seem to exist, or cannot be deleted. >> log.txt)is that valid coding?
The reason for putting exit there, is so i can repeat the line of code a second time if it fails.
what do you think?

here is what i've put together so far:
[QUOTE]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)[/QUOTE]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?

I don't kmow why you want to repeat twice the same command, anyway the general format of the if statement is
if exist File_Name ( first command second command ... ) else ( first command second command ... )
i.e. in your example
if exist test ( del test 2> log.txt for %%j in (log.txt) do if %%~zj equ 0 echo. test deleted > log.txt ) else ( echo. test can't be found > log.txt )
Better you use >> as redirector if you want to add lines to an existing log and for your info 2> means to redirect the standard error messages to a file.

![]() |
![]() |
![]() |
| Login or Register to Reply | |
| Login | Register |
| Ads by Google |