Home > Software design >  Batch file to write for-variables into other files
Batch file to write for-variables into other files

Time:04-28

I am trying to write a batch file that will create other, more complex batch files.

This is a portion of my script for right now:

(
    echo @echo off
    echo for /f "delims=" %%a in (themeset.txt) do set color=%%a&goto setcolor
    echo :setcolor
    echo color %%color%%
    echo pause
) >otherfile.cmd

Theoretically, the output should be a .cmd file with these exact contents:

@echo off
for /f "delims=" %%a in (themeset.txt) do set color=%%a&goto setcolor
:setcolor
color %color%
pause

However, the batch file does not run, and when I attempted to double the %% signs it does not write the entire line correctly.

Any suggestions or solutions would be appreciated.

Max

CodePudding user response:

Magoo's comment beat me to most of the issues.

  1. There is a mostly obscure situation where ECHO text to echo fails, so many people switched to ECHO.text to echo. But somewhere along the way I hit an even more obscure line where that failed, and discovering that semicolon didn't have that problem, I switched to ECHO;text to echo. Which so far, I've not seen any issues.
  2. The FOR /F command has issues, with EOL defaulting to the semicolon being one of them. Some web-page out there made the claim that DELIMS was executed before EOL, so setting EOL to the same as DELIMS would disable it. In my testing it does. But now if we want the whole line, it seems that TOKENS=* disables DELIMS. As far as I know, this is true, but I will admit that I haven't tested that one as much as I would like, so it is probably best to set DELIMS=~, or to similar character that isn't likely to be found in the file.
  3. The % has to be escaped with another percent as in %%, but FOR requires %%, so now you need 4 percents %%%% total.
  4. %%~aa, %%~dd, %%~ff, %%~nn, %%~pp, %%~ss, %%~tt, %%~xx, and %%~zz are all ambiguous. So it probably best to use only capital letters with FOR variables, and probably safest to use only these letters: %%B, %%C, %%E, %%G, %%H, %%I, %%J, %%K, %%L, %%M, %%O, %%Q, %%R, %%U, %%V, %%W, and %%Y See the bottom sections of the help produced by FOR /? for more info on this.
  5. Use the caret to escape special characters. This page, Escape using caret(^), gives some useful info, but its NOT complete. You NEED the caret before the closing parentheses ^), which that page fails to tell you. And to play it safe, I also caret the opening parentheses ^(. But the good thing about that page is that it does points out that when DelayedExpansion is on, you have to double escape the exclamation mark ^^!.

Most of the rest is conventions, I normally capitalize all commands and place a colon in front of all labels, both GOTO :Label and CALL :Label.

The following code:

@ECHO OFF
(
    ECHO;@ECHO OFF
    ECHO;FOR /F "EOL=~ TOKENS=* DELIMS=~" %%%%L IN ^(themeset.txt^) DO SET Color=%%%%L^&GOTO :SetColor
    ECHO;:SetColor
    ECHO;Color %%Color%%
    ECHO;PAUSE
) >otherfile.cmd

Producted the following file:

@ECHO OFF
FOR /F "EOL=~ TOKENS=* DELIMS=~" %%L IN (themeset.txt) DO SET Color=%%L&GOTO :SetColor
:SetColor
Color %Color%
PAUSE

CodePudding user response:

It is clear from your code that you are not even performing the task in the most efficient manner. The following example will therefore perform the same task, but using another methodology. (i.e. retrieve the first line of the text file content and use it as the color command parameter from another Windows Command Script)

@(  Echo @Set /P "colr=" 0^< "themeset.txt"
    Echo @Color %%colr%%
    Echo @Pause) 1> "otherfile.cmd"
  • Related