Unlike most programming languages, the IF statement in MSI script is broken down in 2 parts.
In the System Search it does all the tests but does not act on them, it just stores the results inside a Property that you specify. It is preferred to use all UPPERCASE for the name of the property since that will make it a Public property and guarantee that you can see it everywhere.
In the MSI Script view is where you have your traditional IF statement. There you just refer to the value of the Property that was set and act accordingly.
The catch with using DOS command in MSI Scripts is that it checks the ERRORLEVEL. Therefore if the DOS command fails, MSI will deem that the entire package failed and rollback. To prevent this, use the property tab and change Synchronous to Synchronous, Ignore Exit code, in the custom action that launches the DOS command. In this example we want to delete the C:\OldStuff folder before install our application. To do this we are looking for a file named unsetup.exe inside a folder called C:\OldStuff\SETUP. If we find it, we want to remove the entire C:\OldStuff folder, subfolders and their contents. If it doesn't find it we want it to just install normally.
This
is an over-elaborate example since it would be much simpler to use a
Conditional feature to delete the folder instead of a batch file. But
I had this in my notes and it does show an idea.
While in the Windows Installer Editor
In the Installation Expert view:
In Target System section select System Search
-Click Add
-Select File or Directory
-Property: OLDSTUFFEXIST
-Operation: Search for file: return full file name
-Search Directory: C:\OldStuff\SETUP
-File name: UNSETUP.EXE
-Click OK

Go to the MSI Script view:
-Select the Execute Deferred page (At the bottom)
-Just before the CreateFolders action add the following:
-Remark: Check for and remove C:\OldStuff

-If statement: OLDSTUFFEXIST and click Ok
-Execute Program from Path:
-Custom Action Name: RipOutOldStuff
-Property: %COMSPEC
-Exe and Command line: /c rmdir /s /q C:\OldStuff

-Click on Properties tab
-For Processing, change Synchronous to Synchronous, Ignore exit code

-Click Ok
-Pause installation:
-Seconds to pause: 2
-End statement:
The result will look like this:

Note:
This is an over-elaborate example since it would be much simpler to
use a Conditional Feature to delete the folder instead of a batch
file. But I had this in my notes and it does show an idea.