echo "batch.bat" Help?

April 8, 2011 at 20:51:15
Specs: Windows 7
I need help with that code line. It works but I need to know how to add the line on the 9th line from the bottom instead of the bottom one!! Is that possible?!?!?

See More: echo "batch.bat" Help?

Report •

April 8, 2011 at 20:53:00
The code line didn't publish properly... It was supossed to say:
echo <code> >>"batch.bat" Help?

So I need help with that code

Report •

April 8, 2011 at 21:32:21
>>"batch.bat" echo ^<code^>

Also see:

Report •

April 9, 2011 at 10:42:46
Well that works but it still only gose on the bottom line. Like I said I need it on the 9th to bottom line so it should go 9 lines up from the bottom and insert it there instead of at the bottom. What are the ^ for?

Report •

Related Solutions

April 9, 2011 at 17:25:25
"^" is the escape character, like "\" in most languages.

Does not deal with:
[1] Empty lines in the file
[2] Exclamation marks in the file
[3] files less that 9 lines

Creates a new file with the changes.

@echo off
:: insert line to be 9th last.
SetLocal EnableDelayedExpansion
set oldfile="somefile.txt"
set newfile="newfile.txt"

set newline=^<code^>

for /l %%a in (1 1 8) do set B%%a=
set line=0

for /f "usebackq delims=" %%a in (%oldfile%) do (
    set B8=%%a
    set /a line+=1
    if !line! gtr 8 >> !newfile! echo !B1!
    for /l %%b in (1 1 7) do (
        set /a drop=%%b + 1
        for %%c in (!drop!) do set b%%b=!b%%c!

>> !newfile! echo !newline!
for /l %%a in (1 1 8) do >> !newfile! echo !B%%a!

Report •

April 11, 2011 at 19:24:50
Ok but thats a little long... Is there somthing shorter cause I want to create multiple lines to it but re-typing that for every line will take forever... Is there a shortened version of that code. Also I want it to add the line to itself. BTW I'm not going to insert the line <code> but I'm going to replace that with what I want to insert. Also I use a simpler form of batch coding and I did not understand most of that, but I'm sure it can be condensed to less then 5 lines... hopfuly 1 line.

Report •

April 11, 2011 at 21:12:57
Batch isn't a very practical text editor. If you think you can take into account all of the caveats of batch and do this in less than 6 lines(of practical length) I would like to see it.

If you want it to be easier/shorter use a language with proper text handling, or at least an outside utility.

The below script only needs 4 variables edited and you could use "set /p" to prompt for the info, I just don't feel like validating the input.....

This script does take into account blank lines and the "!" character.

@echo off
SetLocal DisableDelayedExpansion

rem ************************************************
rem only these variables need to be edited
rem ************************************************

set oldfile="somefile.txt"
set newfile="newfile.txt"
rem linepos from LAST
set linepos=9
set insertline=whatever the line is going to be

rem ************************************************
rem You could use "set /p" or parameters if you like
rem ************************************************

for /f %%a in (' find /c /v "" ^< %oldfile% ') do set nlines=%%a 
set /a split=(nlines - linepos) + 2
for /f "delims=" %%a in ('find /n /v "" ^< %oldfile% ') do (
    set line=%%a
    SetLocal EnableDelayedExpansion
    set line=!line:*]=!
    for /f "tokens=1 delims=[]" %%b in ("%%a") do (
        if %%b==%split% >> !newfile! echo !insertline!
        if %split% leq 0 if %%b==1 >> !newfile! echo !insertline!
        if defined line (
            >> !newfile! echo !line!
        ) else >> !newfile! echo:

Report •

Ask Question