2

I want to use script like below

function InitializeSetup(): Boolean;
begin
  Result := not IsAdminLoggedOn;
  if Result then
  begin

to check if the user is administrator or not.

But how can I do if user is administrator, then install A.txt in C:\program files\ABC, otherwise in D:\TEST?

Can I write something to connect to [Files]?

Because I also want to use check path when installing files, if I can combine [Code] and [Files] it might be easier for me.

Excuse for my lack of knowledge, and thanks in advance.

I've tried to use this,

[Files]
Source: "..\ABC\CDE.txt"; DestDir: "{code:GetDirName}\IJK"; \
    Check: DirExists(ExpandConstant('C:\Program Files\FGH'))

But I don't know how to write the code, if I want to install more files in more path.

20170109UPDATE "this one doesn't work"

function GetDirName(Param: string): string;
begin
  if IsAdminLoggedOn And DirExists(ExpandConstant('C:\ABC')) then
      begin
      Result := ExpandConstant('C:\ABC\My Program')
      end
        else
        begin
        if DirExists(ExpandConstant('C:\DEF')) then
          begin
          Result := ExpandConstant('C:\DEF\My Other Program');
          end
            else
            begin
            MsgBox('No destination found, aborting installation', mbError, MB_OK);
            end;
        end;
end;
Martin Prikryl
  • 167,268
  • 50
  • 405
  • 846
  • So is it just about a single file (`A.txt`) or do you want to change the overall installation path, like in http://stackoverflow.com/q/40977536/850848? If the latter, the solution is the same. – Martin Prikryl Dec 21 '16 at 16:56
  • actually it's not only a single file, I try to install more file in more than one path, and I have to check if the path is exist or not before installation. – Sabrina Liu Dec 22 '16 at 00:50
  • I add the [Files] word in the end of my question – Sabrina Liu Dec 22 '16 at 01:17
  • let me revise, I add check is because I want to make sure the path exist, then install files, but I'm not sure if I use "{code:GetDirName}\IJK" with check is correct or not. I don't want to create any folder if the path doesn't exist. – Sabrina Liu Dec 22 '16 at 01:35
  • The part with checking folder existence is a different question. And It's not clear to me, why you install to `{code:GetDirName}\IJK`, but check for an existence of `C:\Program Files\FGH`. – Martin Prikryl Dec 22 '16 at 09:21

1 Answers1

4

Just implement the GetDirName scripted constant to return a different path for privileged and un-privileged installation.

[Files]
Source: "..\ABC\CDE.txt"; DestDir: "{code:GetDirName}"

[Code]

function GetDirName(Param: string): string;
begin
  if IsAdminLoggedOn then
    Result := ExpandConstant('{pf}\My Program')
  else
    Result := ExpandConstant('{localappdata}\My Program');
end;

If you want to use different folders for different files, just implement more functions like GetDirName. Though if the difference is only about subfolders, you can of course use one scripted constant function to resolve the common root folder and append the subfolder in the DestDir parameter.


If you want to change an overall installation target, use the GetDirName scripted constant in the DefaultDirName directive:

[Setup]
DefaultDirName={code:GetDirName}

[Files]
Source: "..\ABC\CDE.txt"; DestDir: "{app}"

[Code]
{ The same as above }

For a more complex example, see Make Inno Setup installer request privileges elevation only when needed.

Martin Prikryl
  • 167,268
  • 50
  • 405
  • 846
  • You might be able to use the `Check` feature too. You would have the file entry twice with each to a different path. The check function returns true then it gets installed. – Andrew Truckle Dec 28 '16 at 22:08
  • Hi, I'd like to ask more about this question. What if I want to check path C:\ABC and C:\DEF exist in this function? I try to put Direxists in it but it doesn't work! `function GetDirName(Param: string): string; begin if IsAdminLoggedOn or IsPowerUserLoggedOn then Result := ExpandConstant('C:\ABC\My Program') else Result := ExpandConstant('C:\DEF\My Program'); end;` – Sabrina Liu Jan 01 '17 at 14:52
  • What what should it do, when the directories do not exist? – Martin Prikryl Jan 02 '17 at 07:37
  • can I just end this function and run the next function? – Sabrina Liu Jan 09 '17 at 14:39
  • I try to use `if (IsAdminLoggedOn or IsPowerUserLoggedOn) And DirExists('C:\ABC')` and it still doesn't work, should I try other way? – Sabrina Liu Jan 09 '17 at 15:38
  • it will be like if I'm admin and I have C:\ABC, then I will install files in "C:\ABC\My Program", which means I need to create a folder calls "My Program". if I'm not admin, but I have C:\DEF, then the files will install to "C:\DEF\My Other Program" or abort the installation, I write a script about this and put it in the end of this question as above. If there is any advice it will helps a lot! Thanks! – Sabrina Liu Jan 09 '17 at 15:54
  • For that use something like `Check: DirExists(GetDirName(''))`, similarly to the code in your question. – Martin Prikryl Jan 09 '17 at 17:34
  • but the result of GetDirName already include the new folder(My program or My other program), I have to check admin privileges first, then check the path (C:\ABC or C:\DEF, depends on user have privileges or not), then create a new folder in the result(the folder name is not the same), in the end the installation path will be C:\ABC\My program, or C:\DEF\My other program. – Sabrina Liu Jan 10 '17 at 00:39
  • Previously you answered: *"What should it do, when the directories do not exist?"* => *"can I just end this function and run the next function?"* - Now you wrote *"create a new folder in the result"* - I'm lost. – Martin Prikryl Jan 10 '17 at 06:33
  • sorry it's my fault, what I mean is if the user is admin and the user has the folder calls C:\ABC, then the user need to create a new folder under this path then install files in it, if the user is not admin, then just check C\DEF exist or not, if exist then create another folder under C:\DEF and install files, if this path dose not exist then just abort the installation, I've write a script and post it in this page, "CTRL+ F" "20170109UPDATE" then you can find it. but it's not working. thanks for your help! – Sabrina Liu Jan 10 '17 at 06:42
  • OK, these are two separate problems. The admin part: No need to check folder existence. Inno Setup does it for you and will automatically create the directory. - The non-admin part: That requires a different approach. See http://stackoverflow.com/a/38445450/850848 – Martin Prikryl Jan 10 '17 at 06:50
  • I want to put check folder is because I don't want to auto create the directory, if the folder dose not exist then just try another or abort. how should I fix the script I wrote? thanks! – Sabrina Liu Jan 10 '17 at 07:06
  • Ask a new question (or possibly a set of questions). Formulate your problem exactly. Illustrate it with examples. – Martin Prikryl Jan 10 '17 at 07:12