{"id":11729,"date":"2021-12-08T06:59:55","date_gmt":"2021-12-08T06:59:55","guid":{"rendered":"https:\/\/lgildv5i97.onrocket.site\/answers\/?post_type=question&#038;p=11729"},"modified":"2021-12-08T07:00:30","modified_gmt":"2021-12-08T07:00:30","slug":"solved-copy-first-last-lines-from-multiple-files-and-copy-to-new","status":"publish","type":"question","link":"https:\/\/computing.net\/answers\/programming\/copy-first-last-lines-from-multiple-files-and-copy-to-new\/30294.html","title":{"rendered":"Solved Copy First &#038; Last Lines From Multiple Files And Copy To New"},"content":{"rendered":"<p>Hi,<\/p>\n<p>Can you please help me<\/p>\n<p>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.<\/p>\n<p>The file extension differs as some of them are .log and some are .txt<\/p>\n<p>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;<\/p>\n<p>Any pointers would greatly be appreciated!<\/p>\n<p>Thanks to Various Post in this forum I was able to find the below code<\/p>\n<p>Header Batch:::<br \/>\n(<br \/>\nfor %%a in (*.txt) do (<br \/>\nset &#8220;first=&#8221;<br \/>\nfor \/f &#8220;usebackq delims=&#8221; %%b in (&#8220;%%a&#8221;) do if not defined first (<br \/>\necho(&#8220;%%a |&#8221; %%b<br \/>\nset first=Y<br \/>\n)<br \/>\n))&gt;%~DP0\\Onlyheaderoutput.csv<\/p>\n<p>Footer Batch::::<br \/>\n(<br \/>\nfor %%F in (*.txt) do (<br \/>\n&lt;nul set \/p &#8220;=%%F |&#8221;<br \/>\nfor \/f %%N in (&#8216;type &#8220;%%F&#8221;^|find \/c \/v &#8220;&#8221;&#8216;) do set \/a skip=%%N<br \/>\nif !skip! gtr 0 set \/a skip-=1<br \/>\nmore +!skip! &#8220;%%F&#8221;<br \/>\n)<br \/>\n)&gt;&gt; %~DP0\\Onlyfooteroutput.csv<\/p>\n<p align=\"right\"><span style=\"font-size: xx-small;\"><i>message edited by SridharCR<\/i><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"inline_featured_image":false,"iawp_total_views":34},"question-category":[55],"question_tags":[],"class_list":["post-11729","question","type-question","status-publish","hentry","question-category-programming"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/question\/11729","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/question"}],"about":[{"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/types\/question"}],"author":[{"embeddable":true,"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/comments?post=11729"}],"wp:attachment":[{"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/media?parent=11729"}],"wp:term":[{"taxonomy":"question-category","embeddable":true,"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/question-category?post=11729"},{"taxonomy":"question_tags","embeddable":true,"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/question_tags?post=11729"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}