Prevent overwriting a file using cmd if exist

Johnrad picture Johnrad · Jul 21, 2011 · Viewed 243.5k times · Source

I am currently writing a .bat batch file that executes an installation file. Before it runs the installation file I check to see if the directory exists to avoid re-installing the application.

I do this by using a If Not Exist filename statement. If the installed file doesn't exist, I then execute the installation file.

For some reason, when I test it with the application where it has been installed already, it is still trying to reinstall the application over it.

Here is a snippet of my code:

cd "C:\Documents and Settings\John\Start Menu\Programs\"
pause
If NOT exist "Software Folder"\ (
 start \\filer\repo\lab\"software"\"myapp"\setup.exe
 pause
) 

Where SoftwareFolder is a subdirectory of "C:\Documents and Settings\John\Start Menu\Programs\". I am checking to see if it exists in my Programs folder.

I know nothing is wrong with my start command. I have a feeling something is wrong with my beginning CD command or one of its parameters.

Thanks a lot, guys!

Answer

Jimmy D picture Jimmy D · Jul 21, 2011

Use the FULL path to the folder in your If Not Exist code. Then you won't even have to CD anymore:

If Not Exist "C:\Documents and Settings\John\Start Menu\Programs\SoftWareFolder\"