2

I have a batch file where I need to write a variable into a specific line of a text file and override what is all ready in that line. I have the code to read specific lines from the file maybe I could switch it around to also write?

Reading lines code:

setLocal EnableDelayedExpansion
for /f "tokens=* delims= " %%a in (variables.txt) do (
set /a N+=1
set v!N!=%%a
)
set variable1=!v1!
set variable2=!v2!
set variable3=!v3!
set variable4=!v4!

I've tried to add echo %variable1% > !v4! something like that but it doesn't work.

Spencer May
  • 4,266
  • 9
  • 28
  • 48

3 Answers3

10

I figured it out!! Here is the code for anyone else who might ever need it.

@echo off
setlocal enableextensions enabledelayedexpansion

set inputfile=variables.txt

set tempfile=%random%-%random%.tmp

copy /y nul %tempfile%

set line=0

for /f "delims=" %%l in (%inputfile%) do (
    set /a line+=1
    if !line!==4 (
        echo WORDS YOU REPLACE IT WITH>>%tempfile%
    ) else (
        echo %%l>>%tempfile%
    )
)

del %inputfile%
ren %tempfile% %inputfile%

endlocal
Spencer May
  • 4,266
  • 9
  • 28
  • 48
  • Good job figuring this out! Not sure why it didn't work for you with a single redirection for the entire loop, but well done anyway. – Andriy M May 22 '12 at 12:56
2

Another option might be to overwrite the file entirely. Here's the part to do that:

:saveVars
(
ECHO %v1%
ECHO %v2%
ECHO %v3%
ECHO %v4%
ECHO %v5%
) >variables.txt
GOTO :EOF

That is, if the number of lines is fixed and known beforehand. If not, you might want to store the last value of the increment in your example code and, when saving the variables, use it like this:

:saveVars
SETLOCAL EnableDelayedExpansion
(
  FOR /L %%i IN (1,1,%N%) DO (ECHO !v%%i!)
) >variables.txt
ENDLOCAL
GOTO :EOF

I'm assuming here that the v1/v2 etc. variables would be used only for synchronising with the file: when it is read, the lines are stored in those variables, and when any of them (variables) gets changed, you just call the saveVars subroutine immediately. Here's an example how you would use it:

…
SET v2=something
CALL :saveVars
…
SET v4=%somevar%
CALL :saveVars
…

If the file is small, the rewriting should be fast enough.

Andriy M
  • 76,112
  • 17
  • 94
  • 154
1

Not absolutely sure I've understood everything correctly, but if you want to substitute something for an existing part of a text file with a batch script, you'll need to write everything (including the changed part) to a new file first, then delete the original and rename the new file to the original name.

I can't really see a point of reading everything into variables, unless I'm missing something. You could simply iterate over the lines writing them one by one into the new file and replacing the specific line's contents with the substitute text along the way:

setLocal EnableDelayedExpansion
>newFile.txt (
  for /f "tokens=* delims= " %%a in (variables.txt) do (
    set /a N+=1
    if !N! == 4 (ECHO substitute text) ELSE ECHO %%a
  )
)
del variables.txt
rename newFile.txt variables.txt

If the substitute text must, in turn, be derived from one of the lines, you could do something like this:

setLocal EnableDelayedExpansion
>newFile.txt (
  for /f "tokens=* delims= " %%a in (variables.txt) do (
    set /a N+=1
    if !N! == 1 SET subst_text=%%a
    if !N! == 4 (ECHO !subst_text!) ELSE ECHO %%a
  )
)
del variables.txt
rename newFile.txt variables.txt
Andriy M
  • 76,112
  • 17
  • 94
  • 154
  • I have no clue what you are doing, in this I just want a batch file to change a specific line in a text file that already exists. Say line 5 of a text file says hello. And I would like to have batch take a variable and write over line 5 with the variables contents. Thats all. – Spencer May May 22 '12 at 00:55
  • You mean, `variables.txt` isn't the file where a line needs to be substituted? – Andriy M May 22 '12 at 04:26
  • It is...but I don't want to create a new file. I see what your doing but I feel like there has to be a better way other than creating a new file :/ – Spencer May May 22 '12 at 11:48
  • 1
    Using only batch syntax and standard tools, it is impossible to do what you want to do. You can append lines to a text file, but you can't modify lines somewhere in the middle of a file without having to write to a new file. In Windows batch scripting, there's no way of opening files, writing to them, saving the results etc. – Andriy M May 22 '12 at 11:56
  • Please add an example (preferably to your question) of what the text file might look like, what the input parameters might be and what result/output should be produced. – Andriy M May 22 '12 at 11:59
  • It is around 300 lines and it doesn't use this, I want to simplify my code by adding this, but ok I guess I could use this solution but it doesn't work. I know it seems useless but this is batch file is a game that I'm making just for fun. – Spencer May May 22 '12 at 12:01
  • It would be helpful to have a glimpse at an example file and the desired result. You don't need to show all the 300 lines. Just 4-5 lines, the input parameters (perhaps the number of the line to overwrite and maybe the number of the line to read from, I'm not sure, you tell me), and the expected result. – Andriy M May 22 '12 at 12:04
  • In the above code is level one, at the very first part I want to read all the information from different text files from different lines and then be able to replace it as you see the variables are changed a lot and the game is dependent on it. The text files clutter up too fast with only one variable being able to be stored in it. – Spencer May May 22 '12 at 12:10
  • Okay, let me take some time to see what your script is doing, thanks. – Andriy M May 22 '12 at 12:12
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/11577/discussion-between-spencer-may-and-andriy-m) – Spencer May May 22 '12 at 12:14