Solved Batch script to copy lines from one text file to another

April 2, 2018 at 04:25:39
Specs: Windows 8
Hi,

Below script is used to copy lines from a text file to other

May i know how can I use the below script to copy the text at the beginning of the output file. Currently it is appending at the end of the file


@ECHO OFF
setlocal enabledelayedexpansion

SET /P maxlines=Enter number of lines to be moved to new txt document:
SET /A linecount=0

FOR /F "delims=" %%A IN (textfile1.txt) DO (
IF !linecount! GEQ %maxlines% GOTO ExitLoop
ECHO %%A >> C:\users\username\desktop\textfile2.txt
SET /A linecount+=1
)

:ExitLoop
ECHO All Done.
ECHO.
ECHO Press any key to close this window.
PAUSE>NUL
EXIT


See More: Batch script to copy lines from one text file to another

Report •

#1
April 2, 2018 at 20:32:00
@echo off & setlocal
set maxlines=%1
if not defined maxlines (
echo usage: %0 max_lines_from_start source_filename dest_filename
echo First n lines from source will be prepended to dest in temp file 'tmp'
goto :eof
)
set source=%2
if not defined source goto :eof
set dest=%3
if not defined dest goto :eof
for /f "tokens=1* delims=[]" %%a in ('find /n /v ""^<%source%') do if %%a leq %maxlines% (>>tmp echo %%b)
echo initial lines:
type tmp
pause
echo prepending to %dest%:
copy tmp+%dest%
echo final result IN TMP. copy to dest or add the final move to batch-script...
type tmp
echo done.
rem move tmp %dest%
:: --------- end script


Report •

#2
April 3, 2018 at 03:44:33
Thank you for the reply.
Below is the scenario. I have the source file 'ER_4465_20180118_021100_22' and destination file 'ER_4465_20180118_021100_23'.

Content in source file
4465
Date/Time,Air Brake C/I,AB Lead,BP Air Flow,Alerter Cutout,Bell,Auto Brake Hndl Supp,CS5 Tgt,CS Penalty,CVE

Content in destination file:
25/12/2017 6:58:03 AM,C/I,Lead,0,Cut-In,Off,No,35

The content length varies based on input file type. Currently while testing your code, it is creating a tmp file with the desired result (source file text appended to the beginning of destination file)
But it is not reflecting in the destination file. This is how I have used the script. Please let me know if I need to modify something:

@echo off & setlocal
set maxlines=2
if not defined maxlines (
echo usage: %0 max_lines_from_start source_filename dest_filename
echo First n lines from source will be prepended to dest in temp file 'tmp'
goto :eof
)
set source="C:\Users\sivas5\Desktop\New folder\ER_4465_20180118_021100_22.csv"
if not defined source goto :eof
set dest="C:\Users\sivas5\Desktop\New folder\ER_4465_20180118_021100_23.csv"
if not defined dest goto :eof
for /f "tokens=1* delims=[]" %%a in ('find /n /v ""^<%source%') do if %%a leq %maxlines% (>>tmp echo %%b)
echo initial lines:
type tmp
pause
echo prepending to %dest%:
copy tmp+%dest%
echo final result IN TMP. copy to dest or add the final move to batch-script...
type tmp
echo done.
rem move tmp %dest%
:: --------- end script


Report •

#3
April 3, 2018 at 11:59:31
✔ Best Answer
echo final result IN TMP. copy to dest or add the final move to batch-script...
type tmp
echo done.

this line:
rem move tmp %dest%
should, or is supposed to, do the trick, by removing the 'rem' so it looks like:

move /y tmp %dest%

this will overwrite your destination file, so I left it de-activated pending feedback of fail or success. Be sure, until you are sure, to back up your destination files just in case something does go wrong.
ps: the "/y" was added to keep the system from asking "are you sure?" every time.

message edited by nbrane


Report •

Related Solutions

#4
April 5, 2018 at 02:16:23
Many thanks . This works fine. I would like to check if the same script can be used to copy the lines to multiple files using an identifier like ER_4465*.csv. I usually have 10-15 files in the folder where I am executing the script
e.g ER_4465_20180118_021100_23.csv, ER_4465_20180118_021100_24.csv
I need to copy the lines in all these file using a common identifier
Your inputs will be of great help.

Report •

#5
April 6, 2018 at 20:13:38
@echo off & setlocal
rem this is your "4465". Don't know how to parse it or exactly how to work it in, so
rem I'm just using %1 as this basis.
set base=%1

for /f "tokens=*" %%a in ('dir /b er_%1*.csv') do call :merge 2 %base% "%%a"
goto :eof

:merge
set maxlines=%1
if not defined maxlines (
echo usage: %0 max_lines_from_start source_filename dest_filename
echo First n lines from source will be prepended to dest in temp file 'tmp'
goto :eof
)
set source=%2
if not defined source goto :eof
set dest=%3
if not defined dest goto :eof
for /f "tokens=1* delims=[]" %%a in ('find /n /v ""^<%source%') do if %%a leq %maxlines% (>>tmp echo %%b)
echo initial lines:
type tmp
pause
echo prepending to %dest%:
copy tmp+%dest%
echo final result IN TMP. copy to dest or add the final move to batch-script...
type tmp
echo done. OVERWRITING %dest% with contents of temp
move /y tmp %dest%
:: --------- end script

totally untested, so be sure to use expendable material for testing. All I did was make the initial code into a subroutine based on all qualifying filenames..

message edited by nbrane


Report •

#6
April 6, 2018 at 22:04:01
Thanks for your reply. I changed the base parameter to 4 (considering er_4 identifier). Not pretty sure what to put in the dest parameter. With the below modification, I see a tmp file being created which is copying the text from both the files twice. But the text not yet appended to the files. Please assist.

@echo off & setlocal
rem this is your "4465". Don't know how to parse it or exactly how to work it in, so
rem I'm just using %1 as this basis.
set base=4
for /f "tokens=*" %%a in ('dir /b er_4*.csv') do call :merge 2 %base% "%%a"
goto :eof

:merge
set maxlines=2
if not defined maxlines (
echo usage: %0 max_lines_from_start source_filename dest_filename
echo First n lines from source will be prepended to dest in temp file 'tmp'
goto :eof
)
set source="C:\Users\sivas5\Desktop\New folder\ER test.csv"
if not defined source goto :eof
set dest="C:\Users\sivas5\Desktop\New folder\ER_4465_*.csv"
if not defined dest goto :eof
for /f "tokens=1* delims=[]" %%a in ('find /n /v ""^<%source%') do if %%a leq %maxlines% (>>tmp echo %%b)
echo initial lines:
type tmp
pause
echo prepending to %dest%:
copy tmp+%dest%
echo final result IN TMP. copy to dest or add the final move to batch-script...
type tmp
echo done. OVERWRITING %dest% with contents of temp
move /y tmp %dest%
:: --------- end script


Report •

#7
April 8, 2018 at 08:56:54
I have the below script which splits a file into multiple files. But only the first split file contains the header rows. Can we modify this so that the first two header rows are copied to each split file. That might help in avoiding the above steps:

@echo off
setLocal EnableDelayedExpansion

set limit=100000
set file=ER_4376_20170721_121600_3.csv
set lineCounter=1
set filenameCounter=1

set name=
set extension=
for %%a in (%file%) do (
set "name=%%~na"
set "extension=%%~xa"
)

for /f "tokens=*" %%a in (%file%) do (
set splitFile=!name!!filenameCounter!!extension!
if !lineCounter! gtr !limit! (
set /a filenameCounter=!filenameCounter! + 1
set lineCounter=1
echo Created !splitFile!.
)
echo %%a>> !splitFile!

set /a lineCounter=!lineCounter! + 1
)


Report •

#8
April 10, 2018 at 20:08:26
It would be really good to have a sample "setup": filenames of sources and dests, and some content of each for a few lines, AND what the output arrangement looks like. I got confused if there was one source and multi dests, or multi sources and multi dests, and how/what relates a source to dest*(s) (ie filename, date, content, etc. but I assumed filename). The surgery is the easy part, it's the operation and the "patients" lineup that needs addressed.

Report •

Ask Question