0

I am installing 3 msi files from 2 batch files. The first batch file finds the msi location and writes the log to my desired location

pushd %~dp0..\%VERSION%\MSIFOLDER\
msiexec /i "InstallMe.msi" /qn TARGETDIR="%x32%" TARGET64DIR="%x64%" /L*v "%~dp0Scripts\Logs\InstallMe.log"
msiexec /i "InstallMe2.msi" /qn TARGETDIR="%x32%" TARGET64DIR="%x64%" /L*v "%~dp0Scripts\Logs\InstallMe2.log"
popd

The second batch file finds the msi location (same as batch1) and writes the log to my desired location(same loc as batch1)

pushd %~dp0..\%VERSION%\MSIFOLDER\
msiexec /i "InstallMe3.msi" /qn TARGETDIR="%Target_PATH%\InstallMe3" /L*v "%~dp0..\..\BatchFileFolder\Scripts\Logs\InstallMe3.log"
popd

however, notice the different pathing to the log files of batch1 and batch2

It seems that pushd in batch1 does not set the path im pushd into as current working dir as the log %~dp0 still points to where the batch file lives

In batch2 it works as I expect it... I am really baffled how, basically identical commands have different behavior and cannot find out what causes it.

on top of both files i have SETLOCAL EnableDelayedExpansion and
SETLOCAL EnableExtensions

Another thing worth mentioning is that if the 3 MSIs are called from 1 batch the pushd behaves correctly i.e. as batch2 when writing the log file to the /L*v "%~dp0..\..\BatchFileFolder\Scripts\Logs\InstallMe3.log" path. But when I separate those 3 in 2 batch files .. they behave differently?!

Important: The reason I am using pushd is, I cannot install msi files with relative path.

Any ideas and help would be highly appreciated.

|
+   ContainerDir
|   +---MSIFilesRootDir [%VERSION%]
|   |   +---MSIFilesDir [%~dp0..\%VERSION%\MSIFilesDir]
|   |       InstallMe1.msi
|   |       InstallMe2.msi
|   |       InstallMe3.msi
|   |
|   +---BatchFileFolder [%~dp0]
|   |     BatchFile.bat
|   |   \---Scripts [%~dp0Scripts]
|   |       \---Logs [%~dp0Scripts\Logs]
|   |           InstallMe1.log
|   |           InstallMe2.log
|   |           InstallMe3.log
|   |

This is the folder and file structure

Kris
  • 322
  • 8
  • 19
  • The most noticeable difference between the two examples is that the former uses effectively two target directories. _It was my understanding that there may only be a single root destination directory_. I've never seen the component location property `TARGET64DIR` used in a Windows Installer command, are you sure that this isn't causing an issue? – Compo Jul 26 '18 at 10:19
  • 1
    Have you tried `%~dp0..\..\BatchFileFolder\Scripts\Logs\InstallMe1.log (and 2)` for the batch1? You seem to expect 3 logs to be placed on the same directory. But at the same time you are using different directory paths for batch1 and batch2 – Julio Jul 26 '18 at 10:23
  • The third msi creates a service and puts some files in its `targetdir=c:\program\installme3\`. The path comes from a variable with path value stored already in it so just appending the `\installme3` to it, I does not uses the %~dp0 to find that location. – Kris Jul 26 '18 at 10:25
  • @Julio this is the actual problem, that to me comes from the pushd inconsistent behavior - I am using different path to the log location because, this is the only way it works. The correct location for all three should/must be `/L*v "%~dp0..\..\BatchFileFolder\Scripts\Logs\InstallMe*.log"` but the batch1 does it differently..... WHY WHY WHY WHY WHY :/ – Kris Jul 26 '18 at 10:27
  • 1
    So, are you sure that `%~dp0Scripts\Logs` and `%~dp0..\..\BatchFileFolder\Scripts\Logs` are equal? May be you could `pushd "%~dp0Scripts\Logs\" ///// echo %CD%` and `pushd "%~dp0..\..\BatchFileFolder\Scripts\Logs\" ///// echo %CD%` (note //// => new line) to see if the absolute path is equal. – Julio Jul 26 '18 at 10:34
  • 1
    Why do you think it is `PUSHD`'s fault? The behavior you see is totally expected. `%~dp0Scripts\Logs` points to the subdirectory `Scripts\Logs` in where your batch file lives. I think you confused `%~dp0` with the current directory, It is the path to the current executing batch file, it doesn't change by changing the directory – sst Jul 26 '18 at 10:57
  • 3
    See also [What is the reason for batch file path referenced with %~dp0 sometimes changes on changing directory?](https://stackoverflow.com/questions/12141482/) and the difference between application/script directory and the current directory as explained for example on answer on [How to run tree command from current directory?](https://stackoverflow.com/a/50941545/3074564) BTW: You can get absolute path by using __FOR__, for example `for %%I in ("%~dp0..\%VERSION%\MSIFOLDER") do set "FolderMSI=%%~fI"` to get assigned to environment variable `FolderMSI` the absolute MSI folder path. – Mofi Jul 26 '18 at 11:02
  • @Mofi, the msi file contains `+`, this gives me a lead to follow, but the msi name itself is in `" "`.. So I wonder, but what you say gets me closer I think.. @sst - pushd changes the current working directory so when i do `pushd "%~dp0..\%VERSION%\MSIFOLDER"` and then `%~dp0` I expect `%~dp0` to be the path i pushd to, which is the case with the 2nd batch and the correct behaviour to me. – Kris Jul 26 '18 at 11:03
  • 1
    You said: `I expect %~dp0 to be the path i pushd to` . That's not the case. You just confirmed the source of confusion, in fact `%~dp0` must always point to the directory of batch file, So if you observe a behavior other than that then it is the `batch2` that is not working as expected not `batch1`. In that case you might have affected by the bug that Mofi posted. But I'm not sure if that bug applies to your case because we don't know the directory structure of your batch file. – sst Jul 26 '18 at 11:26
  • How do you invoke the batch files? By double clicking on them in Explorer? or call them from another batch file? – sst Jul 26 '18 at 11:36
  • @sst from cmd run as administrator and by BatchFileName.bat – Kris Jul 26 '18 at 12:19
  • 1
    Assuming that the current directory of the `cmd` session is set to the directory where the batch file resides, if you are invoking the batch file this way(without double quotes): `> BatchFileName.bat` or if you are providing the full path to batch file(with or without quotes) then you are not affected by the `%~dp0` bug but invoking this way `> "BatchFileName.bat"` then you will be affected by the bug, and remember, the bug affects the expected behavior of `%~dp0` **which must always point the directory of the batch file**, `PUSHD` is working just fine. – sst Jul 26 '18 at 12:52
  • 1
    You will observe consistent behavior if you invoke the batch file directly from Explorer or if you provide the full path to the batch file in `cmd` session. Anyway if you want the path to the log files to be relative to the current directory, then you should use `%cd%\ ` or the safer alternative `%__cd__%` but not `%~dp0`. – sst Jul 26 '18 at 13:02
  • @sst that was it! because the batch file has a nasty symbol `+` I call it from cmd as "batchfile.bat" instead the regular batchfile.bat with no quotation marks! Thank you @Compo, @Mofi and @sst, I learned a lot! – Kris Jul 26 '18 at 13:19
  • @sst if you like to take your comment out so I mark it as answer? – Kris Jul 26 '18 at 14:15

1 Answers1

1

Does this enable you to visualise things? I have added some information inside square brackets to help.

|
\---SomeDir [%~dp0..\..]
    +---ContainerDir [%~dp0..]
    |   +---MSIFilesRootDir  [%~dp0..\%VERSION%]
    |   |   \---MSIFilesDir [%~dp0..\%VERSION%\MSIFilesDir]
    |   |           InstallMe.msi [%~dp0..\%VERSION%\MSIFilesDir\InstallMe.msi]
    |   |           InstallMe2.msi [%~dp0..\%VERSION%\MSIFilesDir\InstallMe2.msi]
    |   |           InstallMe3.msi [%~dp0..\%VERSION%\MSIFilesDir\InstallMe3.msi]
    |   |           
    |   \---BatchDir [%~dp0]
    |       |   BatchFile.bat [%0]
    |       |       
    |       \---Scripts [%~dp0Scripts]
    |           \---Logs [%~dp0Scripts\Logs]
    |                   InstallMe.log [%~dp0Scripts\Logs\InstallMe.log]
    |                   InstallMe2.log [%~dp0Scripts\Logs\InstallMe2.log]
    |                   
    \---BatchFileFolder [%~dp0..\..\BatchFileFolder]
        \---Scripts [%~dp0..\..\BatchFileFolder\Scripts]
            \---Logs [%~dp0..\..\BatchFileFolder\Scripts\Logs]
                    InstallMe3.log [%~dp0..\..\BatchFileFolder\Scripts\Logs\InstallMe3.log]

You can create something like this using Tree with options /A and /F, (enter Tree /? at the Command Prompt for usage information)

Compo
  • 36,585
  • 5
  • 27
  • 39