# [SOLVED] Batch file replace line



## Activeradio (Nov 21, 2008)

I need help with my batch file. I am trying to replace line 15 in a external .bat. The code I have does not seem to be working.


```
:replace
cls
if exist %programfiles%\RunSoBetterOnTop
set linecount=1
for /f "tokens=*" %%a in %HOMEPATH%\My Documents\RunSoBetterOnTop\Run.bat do
set ReadLine=%%a
if !linecount! leq 15
set Outline=!ReadLine:%RunSoBetterOnTop%\Base=%programfiles%\RunSoBetterOnTop\base!
set /a varcount+=1
pause
else
echo RunSoBetterOnTop may not be installed.
pause
goto :main
```


----------



## Squashman (Apr 14, 2005)

*Re: Batch file replace line*

Holy missing parenthesis Batman!


----------



## TheOutcaste (Mar 19, 2009)

*Re: Batch file replace line*

You are checking a variable named *linecount*, but incrementing *varcount*
Using *leq* will change every line from 1-15, assuming it has the text you want to replace.
You have the commands for the If statements on the next line. They have to be on the same line, or you have to use line continuation.
*%programfiles%* has a space in it so it must be quoted. This also means you need to use the *Usebackq* option, or use the *Type* command to output the file in the For loop.
You are missing the parenthesis in the For loop, and you have to use line continuation to do multiple commands.
You haven't specified any delimiters, so leading spaces will be removed from each line

you never output anything to a file


```
SetLocal EnableDelayedExpansion
:replace
cls
Set %_OutFile%=%HOMEPATH%\My Documents\RunSoBetterOnTop\NewRun.bat
if exist "%programfiles%" (
if exist "%_OutFile%" Del "%_OutFile%"
  set linecount=1
  For /f "usebackq tokens=* Delims=" %%a In ("%HOMEPATH%\My Documents\RunSoBetterOnTop\Run.bat") Do (
    set ReadLine=%%a
    If !linecount!==15 Set ReadLine=!ReadLine:%RunSoBetterOnTop%\Base=%programfiles%\RunSoBetterOnTop\base!
    >>"%_OutFile%" Echo.!ReadLine!
    set /a linecount+=1
  )
) else (
  echo RunSoBetterOnTop may not be installed.
)
```


----------



## TheOutcaste (Mar 19, 2009)

*Re: Batch file replace line*

Note this will expand the %RunSoBetterOnTop% variable when it does the replacement. If you are wanting to replace the variable itself, you have to double the % signs and escape them, so you need to use this line instead:


```
If !linecount!==15 Set ReadLine=!ReadLine:^%^%RunSoBetterOnTop^%^%\Base=^%^%programfiles^%^%\RunSoBetterOnTop\base!
```
Thanks to Squashman for pointing that out.

If there are any special characters in that line that might cause problems as well, I didn't test for that. Mainly <> and &


----------



## Activeradio (Nov 21, 2008)

*Re: Batch file replace line*

I heard that you can to write to a .bat, but you can to a .txt? After working with my previous code, it reached the end. However, the file was not modified.


```
:fixjdk
cls
if exist "%programfiles%\Java\jdk1.6.0_20\bin" (
rename %HOMEPATH%\My Documents\RunSoBetterOnTop\Run.bat Run.txt
for /f "usebackq delims=" %%a in (Run.txt) do (
    set string=%%a
    echo !string!
    if !string!==%jdk%\bin set string=%programfiles%\RunSoBetterOnTop\Base
    echo !string! >> Run.bat
)
del Run.txt
) else (
echo RunSoBetterOnTop may not be installed.
goto :main
```


----------



## TheOutcaste (Mar 19, 2009)

*Re: Batch file replace line*

Doesn't matter what the extension is, files are just bytes of data. Redirection will output to any file regardless of what it contains, or what it is named. The file can't be in use of course, so you have to output to a different name and rename when done to replace the original.

The only difference between a text file and any other is a text file is comprised mostly of human readable characters.

This will add a trailing space to every line:

```
echo !string! >> Run.bat
```
When redirecting an Echo command, _everything_ before the >> is included. Either remove the space, or place the redirection before the command. (Though even then you can accidentally add a trailing space).

```
echo !string[B]!>[/B]> Run.bat
>> Run.bat echo !string!
```


----------



## Squashman (Apr 14, 2005)

*Re: Batch file replace line*

You have a space in your path.
You need to quote the path in the RENAME cmd!


----------



## Squashman (Apr 14, 2005)

*Re: Batch file replace line*

When your batch file executes this line of code

```
if !string!==%jdk%\bin set string=%programfiles%\RunSoBetterOnTop\Base
```
It will expand the %programfiles% variable to whatever the environmental variable is. This becomes a different variable on 64 bit systems. You need to escape the percent sign to that it echos the variable name and not the variable value. Just as OutCaste pointed out above.


----------

