1

My setup is the following:

I am reading an .ini file with variables that look like that inside var=value

for /f "tokens=1* delims==" %%i in (file.ini) do (
  set %%i=%%j
)

I am having a standard for loop checking if the variables have values (been set)

for %%p in (var1 var2 var3..) do (
   if not defined %%p (
      call :error "%%p has not been set"
      goto exit
   ) else (
      echo %%p=!%%p!
   )
)

What could I add to it to check for trailing whitespace, as if the user hit space after he would set a value it will mess up with the flow of things as the variables represent paths and what not.

Kris
  • 322
  • 8
  • 19
  • There is a ?typo? `echo %%p=!&&p!` -> `echo %%p=!%%p!` –  Jul 22 '18 at 15:37
  • Possible duplicate of [Remove trailing spaces from a file using Windows batch?](https://stackoverflow.com/questions/9310711/remove-trailing-spaces-from-a-file-using-windows-batch) – Squashman Jul 22 '18 at 15:51
  • Thanks for that, I was writing everything from a different computer not copy/pasting the original source. – Kris Jul 22 '18 at 15:59
  • it is a config file for user to manually set his preferences against variable names, as system paths/names etc. And I am doing that to avoid the human misstake of just pressing the space bar right after he finishes setting the variable name moving to the next. – Kris Jul 22 '18 at 18:07

1 Answers1

1

I have a function/subroutine :Trim I call for such issues.

  • It uses the fact that %* will leave all passed arguments as they are but trim leading/trailing spaces.
  • Because no other arguments are possible it assigns the result to the var Trim, or - if var Trim has content assigns the result to a var referenced by %Trim%.

:: Q:\Test\2018\07\22\SO_51466512.cmd
::
@Echo off&SetLocal EnableDelayedExpansion

:: generating a file.ini with leading/trailing spaces in the vars
( echo var1=  test blah   
  echo var2=  .  
) >file.ini

for /f "tokens=1* delims==" %%i in (file.ini) do (
  set %%i=%%j
)

for %%p in (var1 var2 var3) do (
   if not defined %%p (
      call :error "%%p has not been set"
      exit /B 1
   ) else (
      Set "Trim=%%p"
      Call :Trim !%%p!
      echo [%%p=!%%p!]
   )
)

Goto :Eof
:Trim    text to be trimmed text  .
:: Func: If the variable Trim is defined, the content is interpreted 
::       as the variable Name to receive the trimmed text,
::       If not the trimmed text is stored in the var Trim.
:: Args: all are treated as text to be trimmed
::
If defined Trim (Set "%Trim%=%*") Else (set "Trim=%*")
goto :eof
:error
Echo [%date% %time%] Error: %*

Sample output:

> Q:\Test\2018\07\22\SO_51466512.cmd
[var1=test blah]
[var2=.]
[2018-07-22 17:59:13,17] Error: "var3 has not been set"

Disclaimer: this technique won't work if the variable to trim contains poisenous characters <|>&

  • Hey, thank you for the help, I am trying to fit this function in the same place where you call it from but does not do the trimming, is that not possible? – Kris Jul 22 '18 at 16:54
  • Setting/using the var `Trim` is essential to receive the trimmed content, either directly or indirectly. Please reread my answer. –  Jul 22 '18 at 17:15
  • I do not understand since Trim is variable why you adress it as Trim but not %Trim% in your function bit? – Kris Jul 22 '18 at 17:26
  • Well it's a general sub, which leaves the option to hand over a variable name to receive the trimmed result in the variable trim, or if trim is not defined the result is directly passed to the variable trim itself. So while iterating the vars %%p is copied to trim. This procedure has the advantage that we don't need double delayed expansion as we can still use %%p as a pointer to the content `!%%p!`. As I don't know your complete batch, it's difficult to show how to embed my sub exactly. –  Jul 22 '18 at 17:33
  • The batch installs a program, creates services etc. it's pretty big that's why I wish to embed the trimming in this for loop that esentially would - read>trim>pass. – Kris Jul 22 '18 at 17:40
  • Sorry, but the idea is to `call` with arguments - that can't be inside a for. It has to be a seperate sub somewhere in the same batch file. –  Jul 22 '18 at 17:43
  • Ok, everything seems to be working as you suggested. There is one thing, are the square brackets mandatory as the echo on screen looks a bit weird with them? I am getting an error before every variable that is output on screen? – Kris Jul 22 '18 at 17:55
  • 1
    No, I inserted the brackets just to demonstrate there are no trailing spaces any more. –  Jul 22 '18 at 18:01