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