Batch script to delete the last line & to add new last line

December 4, 2017 at 10:38:02
Specs: Android
I am having a file named Trade.txt. 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

message edited by Balagi


See More: Batch script to delete the last line & to add new last line

Reply ↓  Report •

#1
December 4, 2017 at 23:50:58
You should find plenty of scripting help & tutorials on the WWW with the help of Google Search.

Reply ↓  Report •

#2
December 5, 2017 at 00:50:52
Does "Report type" appear just once?

=====================

M2 Get custom script or take private lessons


Reply ↓  Report •

#3
December 5, 2017 at 02:30:49
please post example data, as in before & after the script.
what should it look like exactly
also please answer post #2

i5-6600K@4.723GHz/4.623GHz cache@1.36v | 2x4GB Crucial-DDR4-2133@ 14-13-13-28 1T 2814MHz@1.345v | ASUS Z170-K | Samsung 250GB SSD 850 EVO | MSI RX 570 ARMOR 4GB@1295cc@1.058v&2050mem | Corsair VS450


Reply ↓  Report •

Related Solutions

#4
December 5, 2017 at 05:00:22
@Mechanix2Go,

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.


Reply ↓  Report •

#5
December 5, 2017 at 05:04:35
@hidde663,

Trade_Before.txt

A
E
F
G
Report type
H
K
Report type
Blank Space

Trade_After.txt

A
E
F
G
Report type
H
K
Trans date
Blank Space

message edited by Balagi


Reply ↓  Report •

#6
December 5, 2017 at 06:34:35
Will continue this tomorrow, i can get the last Report type line number into a variable, but i can not yet display everything before it + everything after that.

This command might come in handy for anyone else willing to accept this challenge:

for /f "tokens=1 delims=[]" %%a in ('type Trade_Before.txt ^| find /N "Report type"') do set lastline=%%a

& this will diplay everything after the last "Report type" line

more +%lastline% Trade_Before.txt

I have not figuered out how to display everything before the last "Report type"

i5-6600K@4.723GHz/4.623GHz cache@1.36v | 2x4GB Crucial-DDR4-2133@ 14-13-13-28 1T 2814MHz@1.345v | ASUS Z170-K | Samsung 250GB SSD 850 EVO | MSI RX 570 ARMOR 4GB@1295cc@1.058v&2050mem | Corsair VS450

message edited by hidde663


Reply ↓  Report •

#7
December 6, 2017 at 01:46:03
----start of test.bat----

@echo off&set cnt=0
for /f "tokens=1 delims=[]:" %%a in ('type Trade_Before.txt ^| find /I /N "Report type"') do set lastline=%%a
set/a secondlastline=%lastline%-1
:loop
if %cnt% equ %secondlastline% for /f "tokens=*" %%a in ('more Trade_Before.txt +%lastline%') do echo Trans date>>Trade_After.txt&echo %%a>>Trade_After.txt&exit /b
for /f "tokens=*" %%a in ('more Trade_Before.txt +%cnt%') do echo %%a>>Trade_After.txt&set/a cnt+=1&goto loop

----end of test.bat----

edit: forgot to add Trans date, now Trade_after.txt data is the same as #5

i5-6600K@4.723GHz/4.623GHz cache@1.36v | 2x4GB Crucial-DDR4-2133@ 14-13-13-28 1T 2814MHz@1.345v | ASUS Z170-K | Samsung 250GB SSD 850 EVO | MSI RX 570 ARMOR 4GB@1295cc@1.058v&2050mem | Corsair VS450

message edited by hidde663


Reply ↓  Report •

#8
December 6, 2017 at 05:29:43
::====== script starts here ===============
::
:: report.bat 2017-12-06 20:08:49.00
@echo off > NEWFILE & setLocal enableDELAYedeXpansioN

:main
set N=
for /f "tokens=* delims= " %%a in (myfile) do (
set/a N+=1
)

:sub1
set L=
for /f "tokens=* delims= " %%i in (myfile) do (
set/a L+=1
if !L! equ !N! (
echo Trans date
) else (
echo.%%i
)
) >> NEWFILE
goto :eof
::====== script ends here =================

=====================

M2 Get custom script or take private lessons


Reply ↓  Report •

#9
December 6, 2017 at 05:38:05
i love how you type delayedexpansion

i5-6600K@4.723GHz/4.623GHz cache@1.36v | 2x4GB Crucial-DDR4-2133@ 14-13-13-28 1T 2814MHz@1.345v | ASUS Z170-K | Samsung 250GB SSD 850 EVO | MSI RX 570 ARMOR 4GB@1295cc@1.058v&2050mem | Corsair VS450


Reply ↓  Report •

Ask Question