Running a File as Being of a Different Extention

From NSIS Wiki
Jump to navigationJump to search
Author: deguix (talk, contrib)


Description

This function can run a file as being of a different extention without renaming it by using the registry. NOTE: It's not guaranteed that it will work with every extention.

Function Call

Push "[path\]filename.ext" ; File (i.e. ${NSISDIR}\MakeNSIS.exe)
Push "[ext]" ; Extention which the file will be considered to be ("" = file real extension) (i.e. txt)
Push "[action]" ; Action to do with the file ("" = default one)(i.e. open)
Call ExecRegBased

Function Code

;----------------------------------------------------------------------------
; Title             : Running a file as being of a different extention
; Short Name        : ExecRegBased
; Last Changed      : 22/Feb/2005
; Code Type         : Function
; Code Sub-Type     : One-way Input
;----------------------------------------------------------------------------
; Required          : Nothing.
; Description       : Runs a file as being of a different extention using the
;                     registry. It's not guaranteed that it will work with
;                     every extention.
;----------------------------------------------------------------------------
; Function Call     : Push "[path\]filename.ext"
;                       File to run.
;
;                     Push "[ext]"
;                       Extention which the file will be considered to be
;                       ("" = file real extension).
;
;                     Push "[action]"
;                       Action to do with the file ("" = default one)
;
;                     Call ExecRegBased
;----------------------------------------------------------------------------
; Author            : Diego Pedroso
; Author Reg. Name  : deguix
;----------------------------------------------------------------------------
Function ExecRegBased
 
  # Prepare variables
  Exch $1 ;Action
  Exch
  Exch $2 ;Extension
  Exch
  Exch 2
  Exch $3 ;File
  Exch 2
  Push $R0
  Push $R1
  Push $R2
 
  # Clean variables
  StrCpy $R2 0
 
  # Verify if the file exists
  IfFileExists $3 FileExists
    SetErrors
    Goto End
  FileExists:
 
  # If a different extention is not used, use file's
  StrCmp $2 "" 0 EndExtention
 
    DetectExtention:
    StrCpy $R0 0
    ExtentionLoop:
      StrCpy $R1 $3 1 $R0
      StrCmp $R1 . ExtentionDone
      StrCmp $R1 "" ExtentionDone
      IntOp $R0 $R0 + 1
      Goto ExtentionLoop
    ExtentionDone:
    IntOp $R0 $R0 + 1
    StrCpy $2 $3 "" $R0
 
  EndExtention:
 
  # Verify if extention exists
  Push $2
 
  ReadRegStr $2 HKCR ".$2" ""
 
  StrCmp $2 "" 0 DetectExtentionEnd
    Pop $2
 
    StrCmp $R2 1 0 +6
      ReadRegStr $2 HKCR ".$2" "Content Type"
 
      StrCmp $2 "" +5
        ReadRegStr $2 HKCR "MIME\Database\Content Type" "$2"
        StrCpy $R2 2
        Goto EndExtention
 
    StrCmp $R2 2 0 +3
      SetErrors
      Goto End
 
    StrCpy $R2 1
    Goto DetectExtention
 
  DetectExtentionEnd:
  Pop $R1
  StrCpy $R2 0
 
  # Verify if command exists
  DetectCommand:
 
  StrCmp $1 "" +3
 
  ReadRegStr $2 HKCR "$2\shell\$1\command" ""
  StrCmp $2 "" 0 Execution
 
    StrCmp $R2 1 0 +3
      SetErrors
      Goto End
 
    ReadRegStr $1 HKCR "$2\shell" ""
 
    StrCmp $1 "" 0 +2
      StrCpy $1 Open
 
    StrCpy $R2 1
    Goto DetectCommand
 
  # Command line adjustments
  Execution:
 
  StrCpy $R1 0
  ExecutionLoop:
    StrCpy $R2 $2 2 $R1
    StrCmp $R2 %1 ExecutionDone
    StrCmp $R2 %L ExecutionDone
    StrCmp $R2 "" ExecutionDone
    IntOp $R1 $R1 + 1
    Goto ExecutionLoop
  ExecutionDone:
 
  StrCpy $1 $2 $R1
  IntOp $R1 $R1 + 2
  StrCpy $R0 $2 "" $R1
 
  StrCpy $R0 '$3$R0'
  StrCpy $1 '$1$R0'
 
  StrCmp $1 $3 0 +2
    StrCpy $1 '$2"$3"'
 
  StrCpy $2 $1
 
  StrCmp $1 exe 0 +3
    StrCpy $2 $2 -4
    StrCpy $2 '$2"'
 
  # Execute the program
  Exec '$2 $0'
 
  # Return variables
  End:
  Pop $R2
  Pop $R1
  Pop $R0
  Pop $1
  Pop $2
  Pop $3
 
FunctionEnd