2

I would like the Inno Setup compiler to automatically detect USB drive letter and use it a source path for installer files.

But I dont know exactly how to identify the right drive. What is the right SourceDir= for that?

The source drive should not be a fix drive.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
FabiBiu
  • 155
  • 6

1 Answers1

1

Inno Setup cannot do this on its own. But you can invoke a simple PowerShell code from Inno Setup preprocessor.

Based on Get the drive letter of USB drive in PowerShell, the following will set SourceDir to first removable drive (not necessarily the USB drive, and it won't use USB hard drives). If you really want the first USB drive, try the answer by @CB.

#define GetUsbDrive() \
  Local[0] = AddBackslash(GetEnv("TEMP")) + "usb_drive.txt", \
  Local[1] = \
    "-ExecutionPolicy Unrestricted -Command """ + \
    "$drive = @(Get-WmiObject Win32_Volume -Filter DriveType='2'); " + \
    "if ($drive) { $drive = $drive[0].DriveLetter }; " + \
    "Set-Content -Path '" + Local[0] + "' -NoNewline -Value $drive " + \
    """", \
  Exec("powershell.exe", Local[1], SourcePath, , SW_HIDE), \
  Local[2] = FileOpen(Local[0]), \
  Local[3] = FileRead(Local[2]), \
  FileClose(Local[2]), \
  DeleteFileNow(Local[0]), \
  Local[3]

#define UsbDrive GetUsbDrive()
#if Len(UsbDrive) == 0
#error No USB drive found
#endif

[Setup]
SourceDir={#UsbDrive}
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • 1
    Thank you very much for your help, it worked. You re doing a great job @martin here on stack overflow. You really help me a lot with my projects :) – FabiBiu Jul 29 '20 at 11:27