0

I do my best to be clearer this time!

I am writing a .bat file to compile single (for the moment) files of different supported languages (fortran, C, C++, etc..). Since for the moment it is for a single file, I made up with this architecture:

buildfile [-lang] filename

where if specified -lang can be either -cpp, -c, -for, etc.. If not specified, -lang will be assumed from file extension.

Now, I report the first piece of code (very beginning, so nothing comes before):

@echo off


:: check first input
if "%1"=="" goto :syntax
if "%1"=="-h" goto :syntax
if "%1"=="/h" goto :syntax
if "%1"=="/?" goto :syntax
if "%1"=="--help" goto :syntax
if "%1"=="/help" goto :syntax

echo %1 | findstr "^-" > inp.log
echo Not found >> inp.log
set "var="
for /f "tokens=* delims=" %%i in (inp.log) do (
    echo Big I writes %%i
    set "var=%%i"
    set var
    if "%var%"=="Not found" (
        echo String not found
        goto :end
        if "%~x1"=="" goto :syntax
    )
    goto :end
)

After check if user asked for help, I want to check if character "-" is present (that means if -lang has been specified).

  1. As first I had thought to redirect echo %1 | findstr "^-" > %avariable% and then if "%avariable%"=="" then character "-" was not specified, hence go to check for file extension with "%~x1" (DID NOT WORK).

  2. Second I thought to place the findstr command in echoing %1 directly as the argument of the FOR /F loop, but if "-" was not present that exploded since the searching string was empty! (i.e. for /f "tokens=* delims=" %%i in (' echo %1 ^| findstr "^-" ') do ( )

So, lastly is what you see in the piece of code, writing output into a file and rereading it, but there's something not working properly. I added the line "Not found" to avoid reading an empty file (since apparently was giving same error as option 2). I see that when I do set var I see correctly "var=Not found", that would mean that var is correctly set. But as soon as I get to the IF condition inside the FOR /F loop, that does not work.

I can imagine a much better and cleaner solution exists, so I am here to ask your help. I would say same something not far from option 1 could be best, since you only do 2 operation (redirect and then IF condition), maybe I am missing some syntax to make it working.

Many thanks!

EDIT: of course, if "-" character is found, then I do a simple spell check to assume language (via many IF statements)

PS: all goto are there as debug.

aschipfl
  • 33,626
  • 12
  • 54
  • 99
mEm
  • 353
  • 3
  • 12
  • `echo %1 | findstr "^-" > inp.log` writes the content of `%1` *plus a space* to the file. So `if "%1"=="-cpp"` results in `if -cpp "=="-cpp"`, which can never be true. Remove the space before te pipe, as you did later with `echo Not found>> inp.log` (or safer: `(echo/%1) | findstr....` – Stephan Dec 27 '20 at 09:59
  • (PS: your first version had a [delayed expansion issue](https://stackoverflow.com/questions/30282784/variables-are-not-behaving-as-expected/30284028#30284028) - just for completeness) – Stephan Dec 27 '20 at 10:00
  • @Stephan many thanks for your replies. First remark I noticed while trying to debug (I saw in the inp.log file that a blank character was there after either -cpp or Not found, so tried to remove the space after echo %1| ). W.r.t. second remark, I can assure that I tried all possible solutions (included adding local ENABLEDELAYEDEXPANSION, but also there not working..). Regarding echo/%1, could you explain it better gently? Thanks – mEm Dec 27 '20 at 10:06
  • @Stephan btw, `echo %1 | findstr "^-" > inp.log` is not actually affected by what you underlined (which I thank you for that, is an addition to my knowledge), since here I'm only interested in finding "-". In fact, in latter solution, this falls under ELSE condition. In it, I switch to %1 (which then does not contains wrongly typed spaces). Do you agree? – mEm Dec 27 '20 at 10:11
  • if `%1` is empty, `echo %1` would result in `echo is OFF`. the `/` (no space!) cures that. (Note: the safest char for this trick is `(` (`echo(%emptyVar%`), but it might be confused with an actual "Start of CodeBlock", so I prefer the slightly less safe `/`). `Setlocal enabledelayed expansion` just enables it, to actually use it, you need to use `!var!` instead of `%var%`. – Stephan Dec 27 '20 at 10:11
  • yes, I agree `:)` Nevertheless - be aware of this, or you will run into trouble some day. – Stephan Dec 27 '20 at 10:15
  • `%1` cannot be empty at that place, because I already checked for that. I still cannot properly understand why you say you prefer `echo/%emptyvar%` solution to `( (echo(%emptyvar%)) )` one. Of course I used `!var!` semantics instead of %% one. – mEm Dec 27 '20 at 10:18
  • @Stephan I'm not an advanced programmer, so I try to capture as much as I can from all of you guys. `:)` – mEm Dec 27 '20 at 10:21
  • Please do not include solutions in your question, post an answer for that instead… – aschipfl Dec 27 '20 at 17:23

1 Answers1

0

EDIT EDIT:

it seems I solved the problem with removing var variable, using directly %%i one:

@echo off


:: check first input
if "%1"=="" goto :syntax
if "%1"=="-h" goto :syntax
if "%1"=="/h" goto :syntax
if "%1"=="/?" goto :syntax
if "%1"=="--help" goto :syntax
if "%1"=="/help" goto :syntax

echo %1 | findstr "^-" > inp.log
echo Not found>> inp.log
set "var="
for /f "tokens=* delims=" %%i in (inp.log) do (
    echo Big I writes %%i
    if "%%i"=="Not found" (
        echo String not found
        echo %~x1 
        if "%~x1"=="" goto :syntax
    ) else (
        echo String found!
        if "%1"=="-cpp" shift & goto :cppfile
        if "%1"=="-c" shift & goto :cppfile
        if "%1"=="-for" shift & goto :fortranfile
        :: if one comes here, format not supported.
        goto :syntax
    )
)
mEm
  • 353
  • 3
  • 12