Page 1 of 1

Increment to text list Add prefix?

Posted: 21 May 2013 19:22
by jeff p
Hello,

I acquired this batch script here on this forum ( from Endoro) which works great!, It increments each line of text, but I'm wondering if its possible to include a prefix before the increment?
Perhaps to add the initials: JP. at the beginning of each line?

Thanks for any help!

Jeff



Code: Select all

@echo off &setlocal
set "folder=d:\myfolder"
pushd "%folder%"

set /a cnt=1001
(for /f "delims=" %%i in ('^<"input.txt" findstr /n "^"') do (
    set "line=%%i"
    setlocal enabledelayedexpansion
    set "line=!line:*:=!"
    if defined line (
        set "pre=!cnt:~-3!"
        echo(!pre!.!line!
        endlocal
        set /a cnt+=1
    ) else (
        echo(
        endlocal
    )
))>"output.txt"
popd

Re: Increment to text list Add prefix?

Posted: 21 May 2013 23:27
by Endoro
add a prefix to each line:

Code: Select all

@echo off &setlocal
set "folder=d:\myfolder"
set "prefix=JP."
pushd "%folder%"

set /a cnt=1001
(for /f "delims=" %%i in ('^<"input.txt" findstr /n "^"') do (
    set "line=%%i"
    setlocal enabledelayedexpansion
    set "line=!line:*:=!"
    if defined line (
        set "pre=!cnt:~-3!"
        echo(%prefix%!pre!.!line!
        endlocal
        set /a cnt+=1
    ) else (
        echo(
        endlocal
    )
))>"output.txt"
popd

Re: Increment to text list Add prefix?

Posted: 22 May 2013 00:22
by jeff p
Awesome!

Thank you very much!

Re: Increment to text list Add prefix?

Posted: 22 May 2013 08:30
by Squashman
I use a 3rd party utility to Add information the beginning and ending of a line. Reason being is because native batch is too slow and I work with files that are sometimes Gigabytes in size. The Utility is called SFK and it has lots of features built-in to it besides the addhead and addtail features.

Re: Increment to text list Add prefix?

Posted: 22 May 2013 09:07
by Endoro
sed would also be a good choice:

Code: Select all

sed -i.bak "s/.*/%prefix%&/" "input.txt"