Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent overwriting a file using cmd if exist

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!

like image 583
Johnrad Avatar asked Jul 21 '11 18:07

Johnrad


People also ask

Does xcopy Skip existing files?

xcopy cannot be configured to SKIP existing files, so you should copy using "robocopy".

How do I stop a file from closing in CMD?

If you want the command prompt cmd widnow to stay open after executing the last command in batch file –you should write cmd /k command at the end of your batch file. This command will prevent the command prompt window from closing and you'll get the prompt back for giving more commands in the cmd window.

How do I stop a loop in CMD?

To stop this infinite loop, press Ctrl + C and then press y and then Enter.


3 Answers

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\"
like image 125
Jimmy D Avatar answered Sep 27 '22 17:09

Jimmy D


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

like image 32
Noah Puckett Avatar answered Sep 27 '22 17:09

Noah Puckett


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
) 
like image 31
Christoph Bimminger Avatar answered Sep 28 '22 17:09

Christoph Bimminger