Prevent overwriting a file using cmd if exist

243,756

Solution 1

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\"

Solution 2

I noticed some issues with this that might be useful for someone just starting, or a somewhat inexperienced user, to know. First...

CD /D "C:\Documents and Settings\%username%\Start Menu\Programs\"

two things one is that a /D after the CD may prove to be useful in making sure the directory is changed but it's not really necessary, second, if you are going to pass this from user to user you have to add, instead of your name, the code %username%, this makes the code usable on any computer, as long as they have your setup.exe file in the same location as you do on your computer. of course making sure of that is more difficult. also...

start \\filer\repo\lab\"software"\"myapp"\setup.exe

the start code here, can be set up like that, but the correct syntax is

start "\\filter\repo\lab\software\myapp\" setup.exe

This will run: setup.exe, located in: \filter\repo\lab...etc.\

Solution 3

As in the answer of Escobar Ceaser, I suggest to use quotes arround the whole path. It's the common way to wrap the whole path in "", not only separate directory names within the path.

I had a similar issue that it didn't work for me. But it was no option to use "" within the path for separate directory names because the path contained environment variables, which theirself cover more than one directory hierarchies. The conclusion was that I missed the space between the closing " and the (

The correct version, with the space before the bracket, would be

If NOT exist "C:\Documents and Settings\John\Start Menu\Programs\Software Folder" (
 start "\\filer\repo\lab\software\myapp\setup.exe"
 pause
) 
Share:
243,756
Johnrad
Author by

Johnrad

Fresh out of college. CS Degree.

Updated on October 27, 2020

Comments

  • Johnrad
    Johnrad over 3 years

    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!

  • Johnrad
    Johnrad almost 13 years
    I have tried that aswell and it wasnt working. But when i did If Not Exist C:\"Documents and Settings"\John\"Start Menu"\Programs\"SoftWare Folder"\
  • Mechanic
    Mechanic over 3 years
    Also ensure that the first parameter (window title) of start command works as intended. It either works or not every time you put it to your script. Sometimes adding a dummy right before the command makes it work.