Increment to text list Add prefix?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
jeff p
Posts: 42
Joined: 28 Jul 2009 10:21

Increment to text list Add prefix?

#1 Post by jeff p » 21 May 2013 19:22

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

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: Increment to text list Add prefix?

#2 Post by Endoro » 21 May 2013 23:27

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

jeff p
Posts: 42
Joined: 28 Jul 2009 10:21

Re: Increment to text list Add prefix?

#3 Post by jeff p » 22 May 2013 00:22

Awesome!

Thank you very much!

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Increment to text list Add prefix?

#4 Post by Squashman » 22 May 2013 08:30

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.

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: Increment to text list Add prefix?

#5 Post by Endoro » 22 May 2013 09:07

sed would also be a good choice:

Code: Select all

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

Post Reply