Ok - we are now in macro territory: a subject near and dear to my heart
(by macro I mean storing commands in variables and then executing the variable)
Because of how the batch parser works, FOR and IF commands within variables cannot be executed using delayed expansion (!cmd!). You must use immediate expansion (%cmd%).
But you have additional problems.
Your method for determining the number of lines will fail if there is a blank line because FOR /F ignores blank lines. It will also fail if a line begins with ; because of the default EOL value (though this is unlikely in a bat file).
You can't simply replace ! with %% in your execution phase because %%line%%w%% will not expand properly.
This code gets you closer:
Code: Select all
for /f %%a in ('type "%~1"^|find /c /v ""') do set linz=%%a
echo ^>^>^> Debug:^> Lines^= %linz%
for /l %%c in (1 1 %linz%) do (
set /p line=
But it still has major problems:
1) Still cannot execute FOR commands because FOR variables need to be %%A in batch file, but in the macro definition you only want %A. Trying to figure out which %% need to be converted to % sounds like a parsing nightmare.
1A) There are probably additional syntax issues where the code needed for a batch file is not exactly what you want in a macro.
2) If the batch file enables delayed expansion, it will be terminated by the implicit ENDLOCAL that occurs when the CALL returns. Subsequent delayed expansion of things like !var! will then fail.
3) Any multi line block such as the following will fail:
Code: Select all
if 1==1 (
echo line 1
echo line 2
4) GOTO :label will fail
5) CALL :label will fail
2) and 3) can be solved by loading the entire batch file into a variable with line feeds between the lines, and then execute the entire thing. This can be done as long as the file is < ~8k. But it hardly seems worthwhile because of the many other problems.
I think the concept of this project is ill advised.