Replace a date in a .txt file

February 8, 2011 at 03:32:07
Specs: Windows XP
Hi,

I have file .txt file that contains date=04-FB-2011
How do I create a script that will prompt for a new date and replace the old one.


Jari


See More: Replace a date in a .txt file

Report •

#1
February 8, 2011 at 04:11:18
If your file looks like this:

one
two
date=04-FB-2011
four
five
six


:: ==========================================
::
:: newdate.bat Tue 08-02-2011 18:38:01.42
@echo off > newfile & setLocal enableDELAYedeXpansion

for /f "tokens=1 delims=[]" %%a in ('find /n "date=" ^< my.txt') do (
set A=%%a
)

set /p D=new date ? :

set N=
for /f "tokens=* delims= " %%a in (my.txt) do (
set /a N+=1
if !A! equ !N! (
echo.date=!D!
) else (
echo.%%a
)
) >> newfile


=====================================
Life is too important to be taken seriously.

M2


Report •

#2
February 8, 2011 at 04:40:32
Great script just what i needed!

One thing I can't modify the scipt so that it would delete the old date row from the file.
In the newfile there will be two rows containing
date=04-FB-2011
date=09-FEB-2011

/Jari


Report •

#3
February 8, 2011 at 12:48:57
Mu Really great script.

i would love to know the idea behind it..I mean how did you replaced just one line from the text file

Subhash Chandra.
www.s-chandra.co.cc


Report •

Related Solutions

#4
February 9, 2011 at 03:53:58
#2, Is that a question?

#3, First loop finds the line number to be changed.


=====================================
Life is too important to be taken seriously.

M2


Report •

#5
February 9, 2011 at 04:34:51
Hi,

I had in my.txt file some remarks in the beginning like:

# text
# text

# text
date=04-MAR-2001
# text

and this resulted in a newfile

# text
# text
# text
date=04-MAR-2001
date=22-mar-2011

I just removed the empty row between the hash marks from my.txt and it works now ok.
I just didn’t realise immediately that the empty row would have such an effect on this script

Jari


Report •

#6
February 9, 2011 at 05:09:02
oops, I didn't account for blank lines. Glad you made it go.


=====================================
Life is too important to be taken seriously.

M2


Report •

Ask Question