Solved copy first & last lines from multiple files and copy to New

February 4, 2016 at 02:56:19
Specs: Windows 7
Hi,

Can you please help me

I am trying to Create a Batch file what would read multiple files from a folder and copy only the first and last lines from each file to a New text or csv file.

The file extension differs as some of them are .log and some are .txt

I Have 2 batch files that would read first and last lines separately but I am not able to combine them; so that I have the file name, first line and Last line in a single line with pipe delimiter;

Any pointers would greatly be appreciated!

Thanks to Various Post in this forum I was able to find the below code

Header Batch:::
(
for %%a in (*.txt) do (
set "first="
for /f "usebackq delims=" %%b in ("%%a") do if not defined first (
echo("%%a |" %%b
set first=Y
)
))>%~DP0\Onlyheaderoutput.csv


Footer Batch::::
(
for %%F in (*.txt) do (
<nul set /p "=%%F |"
for /f %%N in ('type "%%F"^|find /c /v ""') do set /a skip=%%N
if !skip! gtr 0 set /a skip-=1
more +!skip! "%%F"
)
)>> %~DP0\Onlyfooteroutput.csv

message edited by SridharCR


See More: copy first & last lines from multiple files and copy to New

Report •

#1
February 4, 2016 at 08:09:48
✔ Best Answer
@echo off & setlocal EnableDelayedExpansion
pushd YOUR DIRECTORY
for %%i in (*.txt *.log) do (
  (set row=) & (set last=)
  for /F "delims=" %%j in ('type "%%i"') do (
    if not defined row (set row=%%i^|%%j) else (set last=^|%%j)
  )
  echo.!row!!last!
) >> result.csv
popd


Report •

#2
February 4, 2016 at 18:47:17
Wooowww Awesome... Thanks for the Solution IVO....

Report •

#3
February 4, 2016 at 19:20:37
Hello IVO,

Just a Quick Check - Will it be possible to Validate for the File Created Date and run the above Batch based on the Specific Date....

As Mentioned earlier - I have a couple of very large Folder with each having thousands of text files in them... when I am trying to run the above it is taking good amount of time;

Appreciate All your Help...

Thanks
Sridhar R


Report •

Related Solutions

#4
February 5, 2016 at 03:43:21

"Just a Quick Check - Will it be possible to Validate for the File Created Date and run the above Batch based on the Specific Date...."

Please, explain better since I don't understand clearly your need.. Also post exactly your date format as date is country dependent, e.g. in Italy where I live we use dd/mm/yyyy.

Report •

#5
February 9, 2016 at 19:03:44
Hello IVO,

Extremely Sorry for Responding late, was travelling around;

Regarding the above: Can we configure the Batch to be able to read files based on the File Date Created; The format for the date is MM/DD/YYYY;

I am trying to make the Batch file - Accept an optional date parameter..
If present then read the files from that date until yesterday.
If no parameter is given only read the files from last yesterday.

I am working from India for US;

Appreciate all your Time;

Thanks
Sridhar R


Report •

#6
February 10, 2016 at 03:20:13
Hi SridharCR,

what you need is indeed possible, but comparing dates is a chinese job. Let me see if I can find some piece of code from my archive since I give away this kind of scripting years ago. I am not sure also if creation date is different from modification date. Anyway let me know if you prefer the date passed as a command tail parameter or prompted at execution time. I'll post my answer about the feasibility as soon as possible.

message edited by IVO


Report •

#7
February 11, 2016 at 10:09:01
Good news, I solved my doubt about creation date and i think date filtering may be no so chinese job. If all goes right you may get the script ready for monday as I have to run my business too. Please let me know how you like to get the processing date, i.e. by command tail parameter or prompting to enter at launch time.

Report •

#8
February 12, 2016 at 04:00:27

Please, I need explanation about the exact meaning of

"If [the date is] present then read the files from that date until yesterday."

The above is clear;

"If no parameter is given only read the files from last yesterday."

What means "from last yesterday"? Since such behavior can leave some files unprocessed if the batch does not run every day while "from last execution" adds complexity to the script.


Report •

Ask Question