Batch file - delete file if exists and output results

Posted on 2005-04-30
1 Endorsement
Last Modified: 2012-09-18
Hi, I want to have a batch file that will:

1. Check to see if the file exists
2. Try to delete it if it does exist
3. Output what happened

I got something here but it's not doing what I want.  For example, if the file doesn't exist, it will print FILE DOESN'T EXIST to the testOutput.txt file.  But it will also print out CAN'T DELETE.  How can I make it escape (or Go To) after the last statement?  I tried using the go to redirections, but they don't seem to work for me.

if Exist testOutput.txt del testOutput.txt
attrib -r -s -h C:\WINNT\system32\some_file.exe
if NOT Exist C:\WINNT\system32\some_file.exe  echo FILE DOESN'T EXIST - C:\WINNT\system32\some_file.exe  >> testOutput.txt
del C:\WINNT\system32\some_file.exe
if Exist C:\WINNT\system32\some_file.exe  echo CAN'T DELETE - C:\WINNT\system32\some_file.exe  >> testOutput.txt
if NOT Exist C:\WINNT\system32\some_file.exe  echo DELETED - C:\WINNT\system32\some_file.exe  >> testOutput.txt

Also, is there a way to ensure that the file is deleted?  If a reboot is required, that's ok also as long as it can be deleted using a batch file.

If possible, how can I change it so that it will check to see if it CAN be deleted, but if it can't, then output CAN'T DELETE?  Right now, it's outputting CAN'T DELETE even if the file doesn't exist.

Question by:greyknight17
    LVL 82

    Accepted Solution

    To delete a file that's in use, download Sysinternals PendMoves archive ( and put movefile.exe some place into the path.
    The following script should then work for you (assuming NT4/W2k/XP); note that it's currently in test mode, it will only add the delete commands to the log file instead of running them. Handle with care, the deletion during a reboot can not easily be canceled; you'd have to delete the entry from the registry (

    @echo off
    set LogFile=C:\Temp\testOutput.txt
    set DeleteFile=%Systemroot%\system32\some_file.exe

    if exist "%LogFile%" del "%LogFile%"
    if not exist "%DeleteFile%" (
      echo FILE DOESN'T EXIST - %DeleteFile%  >>"%LogFile%"
      goto leave
    echo Trying to delete %DeleteFile% ... >>"%LogFile%"
    echo Setting attributes ...
    attrib -r -s -h "%Deletefile%"  >>"%LogFile%"
    echo Deleting file using del ...
    :: *** Test mode: remove the ECHO in front of the following line to run the script for real:
    ECHO del C:\WINNT\system32\some_file.exe >>"LogFile" 2>&1
    if not exist C:\WINNT\system32\some_file.exe (
      echo DELETED - %DeleteFile%  >>"%LogFile%"
      goto leave
    echo CAN'T DELETE using del - %DeleteFile% >>"%LogFile%"
    echo Scheduling deletion after reboot using movefile.exe ... >>"%LogFile%"
    :: *** Test mode: remove the ECHO in front of the following line to run the script for real:
    ECHO movefile "%DeleteFile%" "" >>"%LogFile%" 2>&1

    LVL 15

    Author Comment

    Sorry, been a while since I visited back here.

    Is there any way to do this without using any third party tools?  I want to do this all in dos if possible.
    LVL 82

    Expert Comment

    Not reliably; Windows doesn't offer a command line tool for that, regedit won't do as the PendingFileRenameOperations value is a REG_MULTI_SZ; reg.exe might be able to do it somehow (and isn't in a default W2k installation anyway), but that value is a sensitive one, so I'd stick to the movefile tool.
    LVL 7

    Assisted Solution

    Here's what I did with a little modification of yours and no third party tools.

    :If the log file is already there it'll delete it.
    if Exist testOutput.txt del testOutput.txt
    ECHO Checking for existing log file...

    :Change the attributes on the file (if it's there)
    attrib -r -s -h C:\TEST\some_file.exe
    ECHO Changing attributes on some_file...

    :If it doesn't exist, it'll just skip the area you don't need it to run.
    if NOT Exist C:\WINNT\System32\some_file.exe GOTO NOFILE

    :Otherwise it'll delete the file.
    del C:\WINNT\System32\some_file.exe
    if Exist C:\WINNT\System32\some_file.exe  echo CAN'T DELETE - C:\WINNT\system32\some_file.exe  >>testOutput.txt
    if NOT Exist C:\WINNT\System32\some_file.exe  echo DELETED - C:\WINNT\system32\some_file.exe  >>testOutput.txt

    echo FILE DOESN'T EXIST - C:\WINNT\system32\some_file.exe  >>testOutput.txt

    LVL 7

    Expert Comment

    That 5 line down in the new batch file, of course, should read:

    attrib -r -s -h C:\WINNT\System32\some_file.exe

    -Hope that helps

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
    Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now