Skip to content

Instantly share code, notes, and snippets.

@maphew
Last active August 29, 2015 14:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maphew/0cdd85e44691429ac33b to your computer and use it in GitHub Desktop.
Save maphew/0cdd85e44691429ac33b to your computer and use it in GitHub Desktop.
A pattern I find myself repeating a lot: "Test if program is in path, attempt to extend path, list available commands in .\bin." The example here is for LAS Tools, but really it could be anything.
@echo off
:: Test if Lastools are in path, if not extend path and try again.
:: we assume desired tool folder is ".\bin" relative to the batch file
lasinfo -i >nul 2>&1
if %errorlevel%==9009 set path=%~dp0\bin;%path%
lasinfo -i >nul 2>&1
if not "%errorlevel%"=="1" goto :NoTools
call :ShowCommands
:: Allows starting shell from Windows Explorer
:: can be removed if you never do that.
cmd
goto :eof
:: --------------------------------------------------------------------
:NoTools
echo.
echo. *** Didn't find LASTools in "%~dp0\bin"
echo.
goto :eof
:ShowCommands
:: Generate list of executables in the .\bin folder and report nicely to user
echo. -={ LAS Tools Commands }=-
setlocal EnableDelayedExpansion
rem Generate executables list
pushd "%~dp0\bin"
for %%a in (exe com bat) do (
echo. >> "%temp%\o-help-list.txt"
if exist *.%%a dir /d *.%%a |find ".%%a" >>"%temp%\o-help-list.txt"
)
popd
rem Strip extensions and report just names
rem c.f. http://ss64.com/nt/syntax-replace.html
for /f "usebackq delims=?" %%g in ("%temp%\o-help-list.txt") do (
set _s=%%g
set _s=!_s:.exe=!
set _s=!_s:.com=!
set _s=!_s:.bat=!
echo. !_s!
)
echo.
del "%temp%\o-help-list.txt"
goto :eof
@maphew
Copy link
Author

maphew commented May 1, 2015

this is what it looks like when run:

R:\GISDATA\LiDAR\LAStools> .\lastools_shell.bat
                   -={ LAS Tools Commands }=-

       7z                  lascolor            lasprecision
       blast2dem           lascontrol          lassort
       blast2iso           lasdiff             lassplit
       bytecopy            lasduplicate        lasthin
       e572las             lasgrid             lastile
       gzip                lasground           lastool
       las2dem             lasground_new       lastrack
       las2iso             lasheight           lasvalidate
       las2las             lasindex            lasview
       las2shp             lasinfo             laszip
       las2tin             laslayers           shp2las
       las2txt             lasmerge            sonarnoiseblaster
       lasboundary         lasnoise            txt2las
       lascanopy           lasoverage          unzip
       lasclassify         lasoverlap
       lasclip             lasplanes



Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

R:\GISDATA\LiDAR\LAStools>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment