Advertisement
BrainWaveCC

CheckParameters.BAT (non-debug)

Jun 23rd, 2024 (edited)
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 4.29 KB | Source Code | 0 0
  1. @REM - CheckParameters.BAT (23 Jun 2024 // 30 Jun 2024 / 23 Jun 2024): How to Check Parameters -- Native vs 3rd Party
  2. @ECHO OFF
  3.  
  4.  ::: If you want to check for a parameter of /X or -X, the use the following format:
  5. :::   CALL :GetParams "X EXTENDED" & IF DEFINED #OK (execute whatever commands you want here)
  6. :::
  7. ::: You would call this from the command-line as:
  8. :::   CheckParameters.BAT /X
  9. :::   CheckParameters.BAT -X
  10. ::: -----------------------------------------------
  11. :::
  12. ::: If you want to check for a parameter of /X or -X that contains a value, then use the following format:
  13. :::   CALL :GetParams "X EXTENDED" #X_VARIABLE & IF DEFINED #X_VARIABLE (execute whatever commands you want here)
  14. :::
  15. ::: You would call this from the command-line as:
  16. :::   CheckParameters.BAT /X:1
  17. :::   CheckParameters.BAT /X:NoSpaces
  18. :::   CheckParameters.BAT /X:"Value with Spaces"
  19. :::
  20. ::: You can do this natively or with a 3rd party utility that really streamlines the process.
  21. ::: I use this for pretty much all of my major scripts.
  22. :::
  23. ::: Full script can be found here .....  https://pastebin.com/wjs4Hz5k
  24. ::: Script without debug info .........  https://pastebin.com/5zAGAyrL
  25. :::
  26. ::: I started my testing with: CheckParameters.BAT /H /S /SHOW /L:C:\TEMP\TEST.TXT
  27. :::
  28.  
  29.  
  30. :Variables
  31.  SETLOCAL ENABLEDELAYEDEXPANSION
  32.  SET #ALL_PARAMS=%*
  33.  SET #DIVIDER=--------------------------------------------------------------------
  34.  
  35.  
  36.  rem -- Process Special Command-line Parameters Natively
  37.  FOR %%P IN (%#ALL_PARAMS%) DO CALL :GetParams %%P
  38.  FOR %%S IN (%#SYNTAX%) DO IF "%%~S"=="F" (ECHO *** ERROR: Invalid parameters provided ***)
  39.  IF NOT DEFINED #LOGFILE (SET #LOGFILE="%~1")
  40.  ECHO %#DIVIDER%
  41.  
  42.  rem -- Process Special Command-line Parameters via CHECKPARAMS.EXE
  43.  CALL :GetParams2 "S SHOW"   & IF DEFINED #OK (SET #SHOW=TRUE)
  44.  CALL :GetParams2 "H HELP ?" & IF DEFINED #OK (SET #HELP=TRUE)
  45.  CALL :GetParams2 "L LOGFILE"       #LOGFILE   & IF NOT DEFINED #LOGFILE (SET #LOGFILE="%~1")
  46.  CALL :GetParams2 "D DIR DIRECTORY" #DIRECTORY & IF NOT DEFINED #DIRECTORY (ECHO No Directory was selected)
  47.  ECHO %#DIVIDER%
  48.  
  49.  
  50.  rem -- Test Your Parameters
  51. :Testing
  52.  ECHO:
  53.  ECHO Setup your parameters above, and then test them by calling this script
  54.  ECHO with your desired parameters.  Remember that "-" and "/" are both valid
  55.  ECHO option delimiters.  For compound options, use ":" as the separator
  56.  ECHO between the option and the value.
  57.  ECHO:
  58.  ECHO Valid parameters for this test are as follows:
  59.  ECHO   %~n0 /S
  60.  ECHO   %~n0 /SHOW
  61.  ECHO   %~n0 /H
  62.  ECHO   %~n0 /?
  63.  ECHO   %~n0 /HELP
  64.  ECHO   %~n0 /L:C:\Temp\LogFile.TXT
  65.  ECHO   %~n0 /LOGFILE:"%TEMP%\LogFile.TXT"
  66.  ECHO   %~n0 "%TEMP%\LogFile.TXT"
  67.  ECHO        {you can make the logfile take on the 1st param if not /L is found}
  68.  ECHO   %~n0 /D:%WINDIR%
  69.  ECHO   %~n0 /DIR:"%WINDIR%"
  70.  ECHO   %~n0 /DIRECTORY:"%~dp0"
  71.  ECHO:
  72.  FOR %%P IN (LOGFILE DIRECTORY) DO IF NOT DEFINED #%%P ECHO *** ERROR: #%%P was not defined
  73.  
  74.  
  75. :ExitBatch
  76.  ENDLOCAL
  77.  GOTO :EOF
  78.  
  79.  
  80.  rem -- SUBROUTINE: Use Native Batch Commands to Capture Selected Parameters
  81. :GetParams
  82. rem %1 = Parameter to Evaluate
  83.  
  84.  SET #OK=
  85.  SET #PREFIX=%~1
  86.  SET #SUFFIX=
  87.  IF NOT DEFINED #PREFIX GOTO :EOF
  88.  SET #PREFIX=!#PREFIX:"=!
  89.  FOR /F "TOKENS=1* DELIMS=:" %%a IN ('ECHO !#PREFIX!') DO (
  90.      SET #PREFIX=%%~a
  91.      SET #SUFFIX=%%~b
  92.  )
  93.  
  94.  FOR %%d IN (/ -) DO (
  95.      FOR %%O IN (S SHOW) DO (
  96.          IF /I "%#PREFIX%"=="%%d%%O" (SET #OK=T& SET #SUFFIX=.& SET #SHOW=TRUE)
  97.      )
  98.  
  99.      FOR %%O IN (H HELP) DO (
  100.          IF /I "%#PREFIX%"=="%%d%%O" (SET #OK=T& SET #SUFFIX=.& SET #HELP=TRUE)
  101.      )
  102.  
  103.      FOR %%O IN (L LOGFILE) DO (
  104.          IF /I "%#PREFIX%"=="%%d%%O" (SET #OK=T& SET #LOGFILE=%#SUFFIX%)
  105.      )
  106.  
  107.      FOR %%O IN (D DIR DIRECTORY) DO (
  108.          IF /I "%#PREFIX%"=="%%d%%O" (SET #OK=T& SET #DIRECTORY=%#SUFFIX%)
  109.      )
  110.  )
  111.  IF NOT DEFINED #SUFFIX SET #OK=F
  112.  SET #SYNTAX=%#OK%;%#SYNTAX%
  113.  GOTO :EOF
  114.  
  115.  
  116.  rem -- SUBROUTINE: Use CheckParams.exe (3rd party util) to Capture Selected Parameters
  117. rem -- https://www.majorgeeks.com/files/details/checkparams.html
  118. :GetParams2
  119. rem %1 = Parameters to Search For
  120. rem %2 = Variable to Set
  121.  
  122.  SET #OK=& IF "%~1"=="" GOTO :EOF
  123.  FOR /F "TOKENS=2-3*" %%v IN ('CHECKPARAMS -q -a -c "%~1" -s %#ALL_PARAMS% 2^>NUL') DO IF /I "%%~v"=="TRUE" (
  124.      SET #OK=T
  125.      IF NOT "%~2"=="" SET %~2=%%~x
  126.  )
  127.  GOTO :EOF
  128.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement