From 9a86869435fc6232302189999e1f66564e6927b3 Mon Sep 17 00:00:00 2001 From: Matthew Rath Date: Fri, 24 Aug 2018 15:14:39 +1000 Subject: [PATCH 1/2] Updated read.me Removed superfluous information. Clarified ambiguous information. Fixed spelling mistakes. --- README.md | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 6ac61ab..2655b6c 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # WampServer PHP CLI Version Changer -WampServer PHP CLI version changer is a Microsoft Windows batch script that allows you to easily add and then change between available WampServer PHP CLI versions using the users environmental path. +WampServer PHP CLI Version Changer is a Microsoft Windows batch script that allows you to easily change between installed WampServer PHP CLI versions using the users environmental path. ## Minimum Requirements The following are required for the CLI Changer script to function correctly. @@ -11,20 +11,18 @@ No installation is required. At just over 7kB the CLI Changer script is small enough to be saved anywhere in your file system. -**Note:** To operate seamlessly the Microsoft Windows "cmd.exe" executable must be in your system path. The standard installation path is ``C:\Windows\System32``. - **Tip:** Once you have save the CLI Changer script, create a desktop shortcut to it for quick and easy access. ## Configuration If using a WampServer 32-bit version with default settings, then no configuration is necessary. -If using a WampServer 64-bit version **OR** a different install path was set during installation, then follow the steps below: +If using a WampServer 64-bit version **OR** a different install path was used during installation, then follow the steps below: 1. Open the CLI Changer script in your preferred text editor. -2. Change the value of the `$pathToInstall` variable to match that of your WampServer install path, taking care **not to** use quotes around the value. +2. Change the value of the `$pathToInstall` variable to match that of your WampServer installation path. **Note:** If your path has spaces in it then the line should look like this: `"$pathToInstall=C:\wamp server"` 3. Once you have changed the value, save and close your editor. -For reference, the default WampServer install paths are: +For your reference, the default WampServer install paths are: - `C:\wamp` - For 32-bit installations. - `C:\wamp64` - For 64-bit installations. @@ -45,9 +43,9 @@ There are two ways you can use the CLI Changer script. * Command line interface (CLI). ### Text Based User Interface (TUI) -Upon running the CLI Changer script you will be presented with a numbered list of available PHP versions that you can select from. +Upon running the CLI Changer script you will be presented with a numbered list of installed PHP versions that you can select from. -The exact list of available PHP CLI versions is dependent on what you currently have in your WampServer PHP addon folder. +The exact list of installed PHP CLI versions is dependent on what you currently have in your WampServer PHP addon folder. ``` Available PHP CLI Versions @@ -77,7 +75,7 @@ To select a PHP version: The CLI Changer script will display the newly set PHP version number and prompt you to press any key to exit. -**Note:** The newly selected PHP version will only be available to new command line windows opened after the change. Existing windows will still reference any previously set PHP version. +**Note:** The newly selected PHP version will only be available to new command line windows. Existing windows will still reference the previously set PHP version. To exit the CLI Changer script without making any changes just press the ENTER key. @@ -86,11 +84,17 @@ Should you make an invalid selection or should the selection be the same as the ### Command Line Interface (CLI) To update the PHP CLI version number directly from the command line, you can pass in the desired PHP version via the scripts first argument. This will bypass the selection screen and go straight to setting the desired version number. +From a CMD prompt: +``` +C:\>: "C:\path\to\cli_changer.bat" php7.2.3 +``` + +From a Bash or Powershell prompt: ``` -cmd_prompt>: start "path\to\cli_changer.bat" php7.2.3 +$ start "C:\path\to\cli_changer.bat" php7.2.3 ``` -**Note 1:** You will need to enclose the scripts path in double quotation marks if the path contains any spaces. +**Note 1:** You will need to enclose the CLI Changer script path in double quotation marks if the path contains any spaces. **Note 2:** You will need to know the available PHP CLI version(s) in advance prior to using this command. From fe6fc7d0c8be915849b60811df7db0eb92386cdb Mon Sep 17 00:00:00 2001 From: Matthew Rath Date: Fri, 24 Aug 2018 23:31:52 +1000 Subject: [PATCH 2/2] Added default installation path recognition Added ability to automatically detect WampServer default installation paths (both 32-bit and 64-bit). Added ability to solely specify WampServer custom installation path. Updated readme.md Fixed spelling mistakes in readme.md --- CHANGELOG.md | 10 +++ README.md | 28 ++++---- cli_changer.bat | 166 +++++++++++++++++++++++++++++++++++------------- 3 files changed, 147 insertions(+), 57 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 31f3837..7e3ce1b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,16 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a ## [Unreleased] +## [1.1.0] - 2018-08-24 + +### Added +- Ability to automatically detect WampServer default installation paths (both 32-bit and 64-bit). +- Ability to solely specify WampServer custom installation path. + +### Changed +- Updated readme.md +- Fixed spelling mistakes in readme.md + ## [1.0.0] - 2018-03-23 ### Added diff --git a/README.md b/README.md index 2655b6c..4cff026 100644 --- a/README.md +++ b/README.md @@ -14,28 +14,30 @@ At just over 7kB the CLI Changer script is small enough to be saved anywhere in **Tip:** Once you have save the CLI Changer script, create a desktop shortcut to it for quick and easy access. ## Configuration -If using a WampServer 32-bit version with default settings, then no configuration is necessary. +No configuration is necessary if your installed WampServer in its default directory. -If using a WampServer 64-bit version **OR** a different install path was used during installation, then follow the steps below: - -1. Open the CLI Changer script in your preferred text editor. -2. Change the value of the `$pathToInstall` variable to match that of your WampServer installation path. **Note:** If your path has spaces in it then the line should look like this: `"$pathToInstall=C:\wamp server"` -3. Once you have changed the value, save and close your editor. - -For your reference, the default WampServer install paths are: +The default installation directories are: - `C:\wamp` - For 32-bit installations. - `C:\wamp64` - For 64-bit installations. +If you installed WampServer in a custom directory, then follow the steps below: + +1. Open the CLI Changer script in your preferred text editor. +2. Append your custom install path to the `$customInstallPath` variable. +3. Save the file and close your editor. + Example: A customised WampServer install path. ``` rem +------------------------------------------------+ rem | User Defined Variable(s) | rem +------------------------------------------------+ -rem WampServer install path. -set $pathToInstall=D:\WampServer64 +rem WampServer custom install path. +set $customInstallPath=D:\WampServer 64-Bit ``` + **IMPORTANT:** Do not add quotation marks around your custom installation path, even if the path contains spaces. + ## How To Use There are two ways you can use the CLI Changer script. @@ -94,7 +96,7 @@ From a Bash or Powershell prompt: $ start "C:\path\to\cli_changer.bat" php7.2.3 ``` -**Note 1:** You will need to enclose the CLI Changer script path in double quotation marks if the path contains any spaces. +**Note 1:** You will need to enclose the CLI Changer script path in quotes if the path contains any spaces. **Note 2:** You will need to know the available PHP CLI version(s) in advance prior to using this command. @@ -110,7 +112,7 @@ Following execution, an exit code will be given: As of WampServer v3.1.2 the below error message may be displayed. ``` -ERROR C:/wamp or PHP in PATH" +Error C:/wamp or PHP in PATH ``` Clicking on this error will open a command window displaying the below message. @@ -130,7 +132,7 @@ Wampserver does not use, modify or require the PATH environmental variable. Using a PATH on Wampserver or PHP version is detrimental to the proper functioning of Wampserver. -Press ENTER top continue... +Press ENTER to continue... ``` This error can be suppressed by right-clicking the WampServer icon in the taskbar notification area and selecting: _Wamp Settings -> Do not verify PATH_ diff --git a/cli_changer.bat b/cli_changer.bat index 8726e14..26db169 100644 --- a/cli_changer.bat +++ b/cli_changer.bat @@ -8,8 +8,8 @@ rem +------------------------------------------------+ rem | User Defined Variable(s) | rem +------------------------------------------------+ -rem WampServer install path. -set $pathToInstall=C:\wamp +rem WampServer custom install path. +set $customInstallPath= @@ -20,9 +20,12 @@ rem +------------------------------------------------+ rem --------------------- rem Default Variables rem --------------------- -set $scriptVersion=1.0.0 +set $scriptVersion=1.1.0 -set $pathToPhps=bin\php\ +set $defaultInstallPath[0]=C:\wamp +set $defaultInstallPath[1]=C:\wamp64 + +set $pathToPhpFolders=bin\php set $cliMode=0 @@ -31,6 +34,70 @@ set $colorSuccess=0A set $colorWarning=0E set $colorFailure=0C +rem Set the title. +title WampServer PHP CLI Version Changer v%$scriptVersion% + + +rem ----------------- +rem Install Paths +rem ----------------- + +rem Test for a custom install path. +if defined $customInstallPath ( + + rem Check if the folder exists. + if not exist "%$customInstallPath%" goto invalidCustomInstallPathGiven + + set $installPath=%$customInstallPath% +) + +rem Test for the first default install path. +if not defined $installPath ( + + rem Check if the first default install path exists. + if exist %$defaultInstallPath[0]% ( + set $installPath=%$defaultInstallPath[0]% + ) +) + +rem Test for the second default install path. +if not defined $installPath ( + + rem Check if the second default install path exists. + if exist %$defaultInstallPath[1]% ( + set $installPath=%$defaultInstallPath[1]% + ) +) + +rem Exit if unable to find installation path. +if not defined $installPath goto defaultInstallPathsMissing + + +rem ------------------- +rem PHP Folder Path +rem ------------------- + +rem Set the $pathToPhpFolders path. +if %$installPath:~-1% neq \ ( + set $pathToPhpFolders=%$installPath%\%$pathToPhpFolders% +) else ( + set $pathToPhpFolders=%$installPath%%$pathToPhpFolders% +) + +rem Check the $pathToPhpFolders path exists. +if not exist "%$pathToPhpFolders%" goto invalidPathToPhpFoldersGiven + +rem Iterate through folders in the the $pathToPhpFolders path adding them to the availablePhpVersionsArray. +set counter=0 + +for /F "delims=" %%a in ('dir %$pathToPhpFolders% /AD /B') do ( + set /A counter=counter+1 + set $availablePhpVersionsArray[!counter!]=%%a +) + +rem Set the last available PHP versions array id. +set $lastAvailablePhpVersionsArrayId=!counter! + rem ---------------------------- rem Users Environmental Path @@ -57,34 +124,6 @@ rem Set the last users environmental path array id. set $lastUsersEnvironmentalPathArrayId=!counter! -rem --------------------------------- -rem Available PHP Folder Versions -rem --------------------------------- - -rem Set the $pathToPhps path. -if %$pathToInstall:~-1% neq \ ( - set $pathToPhps=%$pathToInstall%\%$pathToPhps% -) else ( - set $pathToPhps=%$pathToInstall%%$pathToPhps% -) - -rem Check the $pathToPhps path exists. -PUSHD %$pathToPhps% && POPD || ( - goto invalidPathToPhpsGiven -) - -rem Iterate through folders in the the $pathToPhps path adding them to the availablePhpVersionsArray. -set counter=0 - -for /F "delims=" %%a in ('dir %$pathToPhps% /AD /B') do ( - set /A counter=counter+1 - set $availablePhpVersionsArray[!counter!]=%%a -) - -rem Set the last available PHP versions array id. -set $lastAvailablePhpVersionsArrayId=!counter! - - rem ---------------------------- rem Match PHP Folder Version rem ---------------------------- @@ -99,7 +138,7 @@ for /L %%a in (1,1,%$lastUsersEnvironmentalPathArrayId%) do ( rem Loop through the $availablePhpVersionsArray. for /L %%b in (1,1,%$lastAvailablePhpVersionsArrayId%) do ( rem Check if the users environmental path string matches the path to the available PHP version string. - if "!$usersEnvironmentalPathArray[%%a]!"=="%$pathToPhps%!$availablePhpVersionsArray[%%b]!" ( + if "!$usersEnvironmentalPathArray[%%a]!"=="%$pathToPhpFolders%!$availablePhpVersionsArray[%%b]!" ( rem Force the 'for' command parameters into type 'integer'. set /A $currentPhpVersionId=currentPhpVersionId+%%b set /A $currentUserEnvPathId=$currentUserEnvPathId+%%a @@ -146,17 +185,15 @@ rem Display PHP Versions rem ------------------------ rem Set the window. -title WampServer PHP CLI Version Changer v%$scriptVersion% color %$colorNormal% - -rem Show the title. +rem Show the header. echo: echo Available PHP CLI Versions echo -------------------------- echo: -rem Display all available list of PHP folder names. +rem List all installed PHP folder names. for /L %%a in (1,1,%$lastAvailablePhpVersionsArrayId%) do ( if %%a equ %$currentPhpVersionId% ( echo %%a - !$availablePhpVersionsArray[%%a]! - Current @@ -201,15 +238,15 @@ for /L %%a in (1,1,%$lastUsersEnvironmentalPathArrayId%) do ( ) rem Add the selected PHP folder path to the end of the $usersEnvironmentalPathString. -set $result=%$result%%$pathToPhps%!$availablePhpVersionsArray[%$newSelectionId%]! +set $result=%$result%%$pathToPhpFolders%!$availablePhpVersionsArray[%$newSelectionId%]! rem Set the $usersEnvironmentalPathString. setx path "%$result%" >nul -rem -------------------- -rem Exit Subroutines -rem -------------------- +rem ------------------------------ +rem Exit Subroutines - Success +rem ------------------------------ rem The update was successful. :updateSuccessful @@ -237,6 +274,11 @@ if %$cliMode% equ 0 ( exit 0 + +rem ------------------------------ +rem Exit Subroutines - Failure +rem ------------------------------ + rem An invalid selection was given. :invalidSelectionGiven @@ -250,13 +292,49 @@ if %$cliMode% equ 0 ( exit 1 -rem An invalid $pathToPhps was given. -:invalidPathToPhpsGiven +rem ---------------------------- +rem Exit Subroutines - Error +rem ---------------------------- + +rem An invalid $customInstallPath was given. +:invalidCustomInstallPathGiven + +if %$cliMode% equ 0 ( + color %$colorFailure% + echo: + echo The $customInstallPath path "%$customInstallPath%" does not exist. + echo: + echo Press any key to exit. + pause >nul +) + +exit 1 + +rem Both of the default install paths are missing. +:defaultInstallPathsMissing +if %$cliMode% equ 0 ( + color %$colorFailure% + echo: + echo Neither of the default install paths exists. + echo: + echo 1. %$defaultInstallPath[0]% + echo 2. %$defaultInstallPath[1]% + echo: + echo Wampserver does not appear to be installed. + echo: + echo Press any key to exit. + pause >nul +) + +exit 1 + +rem An invalid $pathToPhpFolders was given. +:invalidPathToPhpFoldersGiven if %$cliMode% equ 0 ( color %$colorFailure% echo: - echo The $pathToPhps path "%$pathToPhps%" does not exist. + echo The $pathToPhpFolders path "%$pathToPhpFolders%" does not exist. echo: echo See the WampServer website for help. echo: