I have a text document where I'd like to add an increment to each line of text, while ignoring any blank rows.
ie. from this:
Jack and Jill went up the hill,
to fetch a pail of water.
Jack fell down,
and broke his crown.
And Jill came tumbling after
to this:
001.Jack and Jill went up the hill,
002.to fetch a pail of water.
003.Jack fell down,
004.and broke his crown.
005.And Jill came tumbling after.
If possible, the ability to indicate the directory location /name of the text document to be processed would be great!
Thanks for any help
Jeff
Batch file to add Increment to text list
Moderator: DosItHelp
Re: Batch file to add Increment to text list
You can try this:
Code: Select all
@echo off &setlocal
set "folder=d:\myfolder"
pushd "%folder%"
set /a cnt=1000
(for /f "tokens=1*delims=:" %%i in ('^<"input.txt" findstr /n "^"') do (
set "line=%%j"
if defined line (
set /a cnt+=1
setlocal enabledelayedexpansion
set "pre=!cnt:~-3!"
echo(!pre!.!line!
endlocal
) else echo(
))>"output.txt"
popd
Re: Batch file to add Increment to text list
Just be aware that the above code will corrupt a file that has any : characters at the start of a line.
Re: Batch file to add Increment to text list
Yes, you are completely right
But I was able to fix it:

But I was able to fix it:
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: Batch file to add Increment to text list
Thanks for the script, Endoro!
But I'm unable to get this to work.
I've set up a simple text document containing a single sentence.
I've included my path directory and text file name to the code.
When I run the bat file, the command prompt window opens but hangs there.
An output.txt file is created but it is empty.
Any ideas?
Thanks
But I'm unable to get this to work.
I've set up a simple text document containing a single sentence.
I've included my path directory and text file name to the code.
When I run the bat file, the command prompt window opens but hangs there.
An output.txt file is created but it is empty.
Any ideas?
Thanks
Re: Batch file to add Increment to text list
Never mind. i needed to add an Enter at the end of my file.
Many thanks for the help.
Works Great!!
Many thanks for the help.
Works Great!!