Ronnie
Ronnie

Reputation: 11198

if exist not working in batch

I am trying to make a .bat file that creates a simple text file. My problem is Windows XP home folder is C:\Documents and Settings while vista and above its C:\Users\

I am running this and no matter what I put for the path name, I always get it exists

@echo off
if exist C:\Documents and Settings\ (
    echo it exists
    Pause
) else (
    echo file not found
    Pause
)

When I run the above I get it exists when in fact it does not since I am windows 7. What am I doing wrong?

Upvotes: 3

Views: 12103

Answers (1)

linuxuser27
linuxuser27

Reputation: 7353

Firstly, you are missing quotes around the path. Second EXIST only checks if files exist. There are a few secret files Windows uses in its file system. Try the following:

@echo off
if exist "C:\Documents and Settings\NUL" (
    echo it exists
    Pause
) else (
    echo Folder not found
    Pause
)

Upvotes: 8

Related Questions