Deletion of last line in a File & adding a new last line

I am having a file named Trade.txt having n number of lines.
I need the last line to get deleted ( The last line is the string "Report type" ) & I need to add new characters to the last line.
The new character is "Trans Date".

Please help me somebody as I am new to scripting
Ragaz MaheAsked:
Who is Participating?
 
Bill PrewCommented:
Okay, give this a try please.

@echo off
setlocal EnableDelayedExpansion

rem Specify input and output files
set FileIn=in.txt
set FileOut=out.txt

rem Specify text for repacement on last line
set ReplaceOld=Report type
set ReplaceNew=Trans Date

rem Delete output file if it exists
if exist "%FileOut%" del "%FileOut%"

rem Get line number of last line in file
set LastLine=0
for /f %%A in ('find /v /c ""^<"%FileIn%"') do set LastLine=%%A

rem Read each line of file, write to output file
for /f "tokens=1* delims=[]" %%A in ('find /v /n ""^<"%FileIn%"') do (
    set Data=%%B

    rem If last line of file, do replacement
    if %%A EQU %LastLine% set Data=!Data:%ReplaceOld%=%ReplaceNew%!

    echo.!Data!>>"%FileOut%"
)

Open in new window


»bp
0
 
Bill PrewCommented:
Give this a try, adjust the file names near the top...

@echo off
setlocal

set FileIn=in.txt
set FileOut=out.txt

find /v /i "report type" <"%FileIn%" >"%FileOut%"
echo Trans Date>>"%FileOut%"

Open in new window


»bp
1
 
aikimarkCommented:
@Ragaz

Is the last line the only place the "Report type" string will ever appear in the file?
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
Ragaz MaheAuthor Commented:
@aikimark, Its like anywhere in the file the string "report type" would be present. But I need the string in the last line alone to be replaced.
0
 
Ragaz MaheAuthor Commented:
@Bill Prew,  Kindly  be noted that, the said string "report type" would be anywhere in the file. But I need the string in the last line alone to be replaced.
0
 
Bill PrewCommented:
That's what my script did.


»bp
0
 
Ragaz MaheAuthor Commented:
@Bill Prew,

I have tested your code, I could see that the last line is getting replaced with Trans date. At the same time, the string(report type) that is present in all the other places other than the last line is getting disappeared in the output file. Please advise
0
 
aikimarkCommented:
Can you run Powershell or VBscript scripts?
0
 
Ragaz MaheAuthor Commented:
@aikimark, I am sorry, but i need to run through a BAT or PERL Script
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.