2

I am trying to run a program which is installed in different location according to OS. I am running the program through a batch file. I saw lot of post which tell to put double quotes but it doesn't work.

if exist "C:\Program Files\Manufacturer\" 
(
    copy F:\Manufacturer\Manufacturer.exe "C:\Program Files\Manufacturer\Manufacturer.exe"
    c:
    cd "C:\Program Files\Manufacturer\"
) 
else 
(
    copy F:\Manufacturer\Manufacturer.exe "C:\Program Files (x86)\Manufacturer\Manufacturer.exe"
    c: 
    cd "C:\Program Files (x86)\Manufacturer\"
)
start Manufacturer.exe

What is the correct syntax?

ehh
  • 3,412
  • 7
  • 43
  • 91

3 Answers3

5

You need to put parentheses on the same lines as the command.

if exist "C:\Program Files\Manufacturer\" (
    copy F:\Manufacturer\Manufacturer.exe "C:\Program Files\Manufacturer\Manufacturer.exe"
    c:
    cd "C:\Program Files\Manufacturer\"
) else (
   copy F:\Manufacturer\Manufacturer.exe "C:\Program Files (x86)\Manufacturer\Manufacturer.exe"
   c: 
   cd "C:\Program Files (x86)\Manufacturer\"
)
SomethingDark
  • 13,229
  • 5
  • 50
  • 55
  • It works, thank you. I have another issue, access denied when copying the file to program files. Can it be fix through the batch file? – ehh Dec 28 '15 at 06:32
  • 1
    @ehh the file likely needs to be run as an administrator. Hold `shift` while you right click and the `Run as Administrator` option should show up. – ug_ Dec 28 '15 at 06:38
  • @ehh - I'd recommend doing what ug_ suggests. If, for whatever reason, you don't want to have to manually choose to run as an administrator every time you run the script, check out [this answer](http://stackoverflow.com/questions/7044985/how-can-i-auto-elevate-my-batch-file-so-that-it-requests-from-uac-administrator/12264592#12264592). – SomethingDark Dec 28 '15 at 06:44
  • I ran as administrator and it doesn't work, access is still denied – ehh Dec 28 '15 at 06:51
0

when I'm faced with that problem I usually take the approach of converting the path to reflect the older 8.3 file types, you can achieve this taking the first 6 letters of the path that contains a space and appending at the end ~1, for example:

If you want to get into the path C:\Program Files\MyApp, you can type C:\Progra~1\MyApp

I have not tested this on Windows 10, but my guess is it should be still working, hope that helps.

AnGut_IT
  • 1
  • 2
  • it doesn't help, same message – ehh Dec 28 '15 at 06:17
  • 2
    That's not always correct, depending on how many files with the same first six characters you have in the directory. It often is, but not frequently enough to be consistent. – SomethingDark Dec 28 '15 at 06:25
  • Yeah, I'm aware of that, but for program files it works, since it is before program files (x86), so I felt no need to go that deep, plus it is easy to figure it out and test it, but in the end you are correct. – AnGut_IT Dec 28 '15 at 06:46
  • You can always use `dir /x` to get the actual 8.3 file name. – SomethingDark Dec 28 '15 at 07:58
0

assuming you have this, which is nasty because it has a space in it.

SET MSBUILDDIR=C:\Program Files (x86)\MSBuild\14.0\bin\

You cannot use use this.

IF NOT EXIST "%MSBUILDDIR%nul" goto MissingMSBuildToolsPath

instead, use && after a command to find out if the ERRORLEVEL was 0

dir "%MSBUILDDIR%msbuild.exe" > nul && echo it exists

or, use || after a command to find out if the ERRORLEVEL was not 0

dir "%MSBUILDDIR%msbuild.exe" > nul || echo does not exist

JJS
  • 6,431
  • 1
  • 54
  • 70