I'm trying to write a batch script that will check to see if a file exists and then delete it if it does, otherwise display a message.
Code:
@echo off
echo This script will del 'results.txt' if it exists
echo.
if exist C:\Users\Sam\Desktop\results.txt (
del C:\Users\Sam\Desktop\results.txt
pause
) else (
echo file not found
pause
)
I've seen this kind of syntax on a few tutorial pages, but no matter how I type it, and regardless of whether the file exists or not, the script returns 'file not found'.
I've tried running the script from the same directory as the file I want it to delete and just typing the file name without location, and it still doesn't work. I've even tried wrapping the location is quotes which doesn't work either.
Is this still supported in Vista or am I typing it wrong?
Thanks.
@echo off
echo This script will del 'results.txt' if it exists
echo.
if exist C:\Users\Sam\Desktop\results.txt (del C:\Users\Sam\Desktop\results.txt) else (echo file not found)
Pause
Oh I just realized what was wrong. When I saved the 'results.txt' file through Wing, I had already selected 'Text File' for the file extension and had typed the file name as 'results.txt' therefore the file became: 'results.txt.txt'.
Both our scripts work now since the file extension has been fixed. Thanks for your effort though; I had thought that my syntax was incorrect.