Raja
Raja

Reputation: 71

check for file existence using nmake and delete it

I am using Windows version of NMAKE. I would like to check for file existence in a make file. If it exists I need to delete it. Here is my code:

!IF EXIST ("C:\ABC.XML")
    @del ABC.XML
!ELSE
    @echo "FILE DOESN'T EXIST
!ENDIF

The above code is not working. I could not figure it out the problem. Please help.

Upvotes: 1

Views: 1155

Answers (1)

Ross Ridge
Ross Ridge

Reputation: 39601

Your code doesn't work because !IF, !ELSE and !ENDIF are a preprocessing directives and the result of preprocessing must produce a valid makefile. Commands are only allowed as part of what Microsoft calls a description block which are required to start with a dependency line with one or more targets and zero or more dependents.

You can get around this by executing your commands during the preprocessing stage by including them in a preprocessing directive surrounded by brackets ([]). Something like this:

!IF EXIST(C:\ABC.XML)
!  IF [del C:\ABC.XML]
!  ENDIF
!ELSEIF [echo FILE DOESN'T EXIST]
!ENDIF

The second !IF and the !ELSEIF directives are used to provide a context for the commands so they're executed during the preprocessing phase.

However I think you'd probably be better moving the del command to a description block where it's actually needed. For example if file ABC.XML needs to be deleted before it can be rebuilt, use something like this:

ABC.XML: ABC.CSV
    -rem The csv2xml translator requires that the XML file not already exist
    -@del ABC.XML 2> NUL
    csv2xml ABC.CSV ABC.XML

Upvotes: 2

Related Questions