1.

Solve : IF NOT EXIST?

Answer»

okay so what if I wanted to make a code for a batch FILE that WOULD delete a certain folder or folders if a certain file doesnt exist. I have tried in a DOS command window and found the commands that work for this, but when i compile this into a batchfile code it does not works. This is what I have:
Code: [Select]@echo off
cd..
cd..
cd..
cd..
cd Documents and Settings
cd seth
cd desktop
IF NOT EXIST key.txt
DEL test.txt
exitdo this instead..

@echo off
cd..
cd\docume~1\
cd seth
cd desktop
if not exist key.txt del test.txt
exit@echo off

REM Place file path in VARIABLE to simplify COMAND lines.
SET Location=C:\Documents and Settings\seth\desktop

REM Use fully qualified file names in command.
IF NOT EXIST "%Location%\key.txt" DEL "%Location%\test.txt"

Here is your problem

This will not do what you intend
Quote

IF NOT EXIST key.txt
DEL test.txt
exit

In batch language you cannot WRITE multiline IF statements like that. The whole statement MUST be on one line, either an actual line or an extended line.

On one actual line...

Quote
IF NOT EXIST key.txt DEL test.txt
exit

Or on an extended line. A line is extended with an open-parentheses character "(" and the extension is finished with a close-parentheses character ")".

Quote
IF NOT EXIST key.txt (
another command if wanted
DEL test.txt
another command if wanted
)
exit

This works at the command line too, in win2K and XP

Quote
c:\>(
More? echo 1
More? echo 2
More? echo 3
More? )
1
2
3

c:\>

Note: These are parentheses ( ). These are "brackets" { }.


Discussion

No Comment Found