2

How can I determine whether a certain file exists in the same location where Setup.exe is running from?

i.e. something like this:

if (FileExists(App.Path + '\config_file*.txt') = True) then

Note: I don't want to know the folder into which the application is being installed, I want to know the folder from which Setup.exe is running (eg. "C:\Documents and Settings\Jennifer Cox") - in Visual Basic
For example the equivalent of App.Path.

Mayuri
  • 402
  • 6
  • 13
Andy Groom
  • 619
  • 1
  • 7
  • 15

2 Answers2

2

You can use {src} to obtain the path (excluding filename) to the installer itself. This is most commonly useful when using [Files] entries that have the external flag, but it can be used in [Code] as well, like so:

// procedure/function Whatever();
var
  S: String;
begin
  S := ExpandConstant('{src}\File.txt');
  // ...
end;
Miral
  • 12,637
  • 4
  • 53
  • 93
1

To get the path from where the setup was executed you need to extract it from the {srcexe} constant. To extract the path from a file you can use the ExtractFilePath function so putting this together you can get something like this:

var
  S: string;
begin
  S := AddBackslash(ExtractFilePath(ExpandConstant('{srcexe}'))) + 'File.txt');
  if FileExists(S) then
    ...
end;
TLama
  • 75,147
  • 17
  • 214
  • 392
  • Is there any way to make FileExists - or FileSearch - find a file using a wildcard? I am trying to find a file which begins "config_file" and which ends ".txt" but will be something like "config_file2763547.txt", but I won't know the 2763547 bit. – Andy Groom Apr 17 '14 at 11:00
  • Ask that as a new question, please. That is a good topic which might be useful to share with others and would be lost in comments. – TLama Apr 17 '14 at 11:14
  • OK will do http://stackoverflow.com/questions/23132072/how-to-test-using-wildcards-whether-a-file-exists-in-inno-setup – Andy Groom Apr 17 '14 at 11:37
  • 1
    Just use `{src}` instead of trying to extract the path from `{srcexe}`. Much simpler. – Miral Apr 18 '14 at 20:08
  • @Miral, forgot on `{src}`. Please post that as answer. Andy, could you unaccept this post, please ? I'll delete it... – TLama Apr 18 '14 at 20:10
  • You shouldn't delete your answer. Even if it's not the best answer, it's still a good answer, and may be useful to someone who has a peripherally related question. – Miral Apr 21 '14 at 20:49
  • @Miral, but my answer is actually a workaround for `{src}`. Your answer is accurate and thus mine should disappear ;) – TLama Apr 21 '14 at 21:32