Check for spaces in a directory path: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
m (Reverted edits by 221.224.108.85 to last version by 88.104.156.235)
 
(12 intermediate revisions by 5 users not shown)
Line 2: Line 2:


== Description ==
== Description ==
This script will tell the user that he has entered spaces (" ") in the installation directory.
This script will check a string (or directory path) for spaces and returns the number of spaces found in the string.
This can be used if you don't want spaces in the directory path, and should be placed in the directory page's leave function (because if the directory page has spaces, then it will go back to the directory page).


Updated 10th October 2003
Updated 12th January 2005


== Usage ==
== Usage ==
The CheckForSpaces function should be called in the Leave function of the Directory page. Below are the two ways to implement this (through Classic UI and Modern UI). Both implement a function called "DirectoryLeave" as the Directory pages' Leave function.
=== MUI ===
Use this if you are using Modern UI (<i>!include MUI.nsh</i>).
<highlight-nsis>
!define MUI_PAGE_CUSTOMFUNCTION_LEAVE "DirectoryLeave"
!insertmacro MUI_PAGE_DIRECTORY
</highlight-nsis>
=== Non-MUI ===
=== Non-MUI ===
Use this if you aren't using Modern UI.
<highlight-nsis>
<highlight-nsis>
Page Directory "" "" "CheckForSpaces" </highlight-nsis>
Page Directory "" "" "DirectoryLeave"
</highlight-nsis>


=== MUI ===
=== DirectoryLeave Function ===
This gives an example of what your "DirectoryLeave" function should look like.
<highlight-nsis>
<highlight-nsis>
!define MUI_PAGE_CUSTOMFUNCTION_LEAVE "CheckForSpaces"
Function DirectoryLeave
!insertmacro MUI_PAGE_DIRECTORY </highlight-nsis>
 
  # Call the CheckForSpaces function.
  Push $INSTDIR # Input string (install path).
  Call CheckForSpaces
  Pop $R0 # The function returns the number of spaces found in the input string.
 
  # Check if any spaces exist in $INSTDIR.
  StrCmp $R0 0 NoSpaces
 
    # Plural if more than 1 space in $INSTDIR.
    StrCmp $R0 1 0 +3
      StrCpy $R1 ""
    Goto +2
      StrCpy $R1 "s"
 
    # Show message box then take the user back to the Directory page.
    MessageBox MB_OK|MB_ICONEXCLAMATION "Error: The Installaton directory \
    has $R0 space$R1.$\nPlease remove the space$R1."
    Abort
 
  NoSpaces:
 
FunctionEnd
</highlight-nsis>


== The Function ==
== The Function ==
Line 24: Line 58:
  Push $R2
  Push $R2
  Push $R3
  Push $R3
Push $R4
  StrCpy $R1 -1
  StrCpy $R1 -1
  StrCpy $R3 $R0
  StrCpy $R3 $R0
Line 32: Line 65:
   IntOp $R1 $R1 - 1
   IntOp $R1 $R1 - 1
   StrCmp $R2 "" done
   StrCmp $R2 "" done
   StrCmp $R2 " " found
   StrCmp $R2 " " 0 loop
  StrCpy $R4 "$R2$R4"
Goto loop
found:
   IntOp $R0 $R0 + 1
   IntOp $R0 $R0 + 1
  Goto loop
  Goto loop
  done:
  done:
Pop $R4
  Pop $R3
  Pop $R3
  Pop $R2
  Pop $R2
Line 46: Line 75:
FunctionEnd
FunctionEnd
</highlight-nsis>
</highlight-nsis>
-Stu
-Stu


[[Category:Disk, Path & File Functions]]
[[Category:Disk, Path & File Functions]]

Latest revision as of 18:17, 26 December 2008

Author: Afrow UK (talk, contrib)


Description

This script will check a string (or directory path) for spaces and returns the number of spaces found in the string.

Updated 12th January 2005

Usage

The CheckForSpaces function should be called in the Leave function of the Directory page. Below are the two ways to implement this (through Classic UI and Modern UI). Both implement a function called "DirectoryLeave" as the Directory pages' Leave function.

MUI

Use this if you are using Modern UI (!include MUI.nsh).

!define MUI_PAGE_CUSTOMFUNCTION_LEAVE "DirectoryLeave"
!insertmacro MUI_PAGE_DIRECTORY

Non-MUI

Use this if you aren't using Modern UI.

Page Directory "" "" "DirectoryLeave"

DirectoryLeave Function

This gives an example of what your "DirectoryLeave" function should look like.

Function DirectoryLeave
 
  # Call the CheckForSpaces function.
  Push $INSTDIR # Input string (install path).
   Call CheckForSpaces
  Pop $R0 # The function returns the number of spaces found in the input string.
 
  # Check if any spaces exist in $INSTDIR.
  StrCmp $R0 0 NoSpaces
 
    # Plural if more than 1 space in $INSTDIR.
    StrCmp $R0 1 0 +3
      StrCpy $R1 ""
    Goto +2
      StrCpy $R1 "s"
 
    # Show message box then take the user back to the Directory page.
    MessageBox MB_OK|MB_ICONEXCLAMATION "Error: The Installaton directory \
    has $R0 space$R1.$\nPlease remove the space$R1."
    Abort
 
  NoSpaces:
 
FunctionEnd

The Function

Function CheckForSpaces
 Exch $R0
 Push $R1
 Push $R2
 Push $R3
 StrCpy $R1 -1
 StrCpy $R3 $R0
 StrCpy $R0 0
 loop:
   StrCpy $R2 $R3 1 $R1
   IntOp $R1 $R1 - 1
   StrCmp $R2 "" done
   StrCmp $R2 " " 0 loop
   IntOp $R0 $R0 + 1
 Goto loop
 done:
 Pop $R3
 Pop $R2
 Pop $R1
 Exch $R0
FunctionEnd

-Stu