• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 81649
  • Last Modified:

Batch file - delete file if exists and output results

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.

Thanks.
1
greyknight17
Asked:
greyknight17
  • 2
  • 2
2 Solutions
 
oBdACommented:
To delete a file that's in use, download Sysinternals PendMoves archive (http://www.sysinternals.com/ntw2k/source/misc.shtml#pendmoves) 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 (http://www.jsifaq.com/subb/tip0500/rh0578.htm)

@echo off
setlocal
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

:leave
0
 
greyknight17Author Commented:
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.
0
 
oBdACommented:
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.
0
 
spamsterCommented:
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
GOTO DONE

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

:DONE
0
 
spamsterCommented:
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
Spamster
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now