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.
Main Topics
Browse All TopicsHi, 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_fil
if NOT Exist C:\WINNT\system32\some_fil
del C:\WINNT\system32\some_fil
if Exist C:\WINNT\system32\some_fil
if NOT Exist C:\WINNT\system32\some_fil
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.
Thanks.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Not reliably; Windows doesn't offer a command line tool for that, regedit won't do as the PendingFileRenameOperation
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_fil
:Otherwise it'll delete the file.
del C:\WINNT\System32\some_fil
if Exist C:\WINNT\System32\some_fil
if NOT Exist C:\WINNT\System32\some_fil
GOTO DONE
:NOFILE
echo FILE DOESN'T EXIST - C:\WINNT\system32\some_fil
:DONE
Business Accounts
Answer for Membership
by: oBdAPosted on 2005-04-30 at 12:43:33ID: 13901644
To delete a file that's in use, download Sysinternals PendMoves archive (http://www.sysinternals.c om/ntw2k/s ource/ misc .shtml#pen dmoves) and put movefile.exe some place into the path. b/tip0500/ rh0578.htm )
.txt stem32\som e_file.exe
e.exe >>"LogFile" 2>&1 e.exe (
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 (http://www.jsifaq.com/sub
@echo off
setlocal
set LogFile=C:\Temp\testOutput
set DeleteFile=%Systemroot%\sy
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_fil
if not exist C:\WINNT\system32\some_fil
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
:leave