{"id":531,"date":"2014-01-21T18:00:45","date_gmt":"2014-01-22T01:00:45","guid":{"rendered":"http:\/\/www.kitchencloset.com\/home\/bryan\/blog\/?p=531"},"modified":"2014-01-23T12:21:21","modified_gmt":"2014-01-23T19:21:21","slug":"dos-magic-todays-grody-hack","status":"publish","type":"post","link":"https:\/\/www.kitchencloset.com\/home\/bryan\/blog\/2014\/01\/dos-magic-todays-grody-hack\/","title":{"rendered":"DOS magic (today&#8217;s grody hack)"},"content":{"rendered":"<p>So, I recently had 8000+ text files. Each file contained a single line of CSV text without a trailing new-line.<\/p>\n<p>This 8000+ files also had subsets to them based on a unique word in the file name as well (eg: server1, serverdb etc&#8230;)<\/p>\n<p>My challenge was to concatenate all of the files from each group into their own individually collected files while also adding line-feeds between each file&#8217;s contents in order to create the new single CSV file and pre-pend today&#8217;s date to the file name, which would give me a proper CSV file that would be easier to share, store and process by others.<\/p>\n<p>First, I whip open my friendly Notepad application and create a header.csv text file that contains the column headers for the CSV.<\/p>\n<p>Then using Notepad again, after some unnecessarily painful searching, I finally was able to cobble together this, which I then Select All &gt; Copy, then Paste into my DOS prompt (CD&#8217;d to the folder with the files first):<\/p>\n<blockquote><p>type header.csv &gt;\u00a0%date:~10,4%%date:~4,2%%date:~7,2%-SERVER1.csv &amp;&amp; for %f in (*SERVER1*.log) do @echo. &gt;&gt; %date:~10,4%%date:~4,2%%date:~7,2%-SERVER1.csv &amp;&amp; type &#8220;%f&#8221; &gt;&gt; %date:~10,4%%date:~4,2%%date:~7,2%-SERVER1.csv<br \/>\ntype header.csv &gt;\u00a0%date:~10,4%%date:~4,2%%date:~7,2%-SERVERDB.csv &amp;&amp; for %f in (*SERVERDB*.log) do @echo. &gt;&gt; %date:~10,4%%date:~4,2%%date:~7,2%-SERVERDB.csv &amp;&amp; type &#8220;%f&#8221; &gt;&gt; %date:~10,4%%date:~4,2%%date:~7,2%-SERVERDB.csv<\/p><\/blockquote>\n<p>(&#8230;.repeat as needed&#8230; etc&#8230;)<\/p>\n<ul>\n<li>for %f in &#8230; (runs a loop of the full command-line against the wildcard based on the number of files matching the wildcard)<\/li>\n<li>@echo. (with the important, trailing &#8220;.&#8221; outputs the new-line)<\/li>\n<li>%date:&#8230; (captures just a portion of the &#8220;date&#8221; value and echos it)<\/li>\n<li>&amp;&amp; (appends additional command to the line)<\/li>\n<li>&gt;&gt; (appends the output of the line to the file)<\/li>\n<\/ul>\n<p>Yeah, it&#8217;s crude, but I&#8217;ll take the win.<\/p>\n<p>If I really wanted to be fancy, I&#8217;d try and capture the date from the source files and use that for the imprint on the destination, but I&#8217;m not that inspired. Sorry.<\/p>\n<p>I&#8217;m posting this on the hopes that if someone spirals into this kind of situation like I did today, they&#8217;ll find this post and go &#8220;Yes! That&#8217;s exactly what I needed!&#8221;. Good luck.<\/p>\n<p>I&#8217;ll file this under TIL about abusing &#8220;date&#8221; and &#8220;&amp;&amp;&#8221; in a new way, or maybe I only learned how much DOS I&#8217;ve forgotten. Hmm&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So, I recently had 8000+ text files. Each file contained a single line of CSV text without a trailing new-line. This 8000+ files also had subsets to them based on a unique word in the file name as well (eg: server1, serverdb etc&#8230;) My challenge was to concatenate all of the files from each group [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[117],"tags":[121,122,120,118,119],"class_list":["post-531","post","type-post","status-publish","format-standard","hentry","category-grody-hacks","tag-append","tag-concatination","tag-date","tag-dos","tag-echo"],"_links":{"self":[{"href":"https:\/\/www.kitchencloset.com\/home\/bryan\/blog\/wp-json\/wp\/v2\/posts\/531","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kitchencloset.com\/home\/bryan\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kitchencloset.com\/home\/bryan\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kitchencloset.com\/home\/bryan\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kitchencloset.com\/home\/bryan\/blog\/wp-json\/wp\/v2\/comments?post=531"}],"version-history":[{"count":15,"href":"https:\/\/www.kitchencloset.com\/home\/bryan\/blog\/wp-json\/wp\/v2\/posts\/531\/revisions"}],"predecessor-version":[{"id":541,"href":"https:\/\/www.kitchencloset.com\/home\/bryan\/blog\/wp-json\/wp\/v2\/posts\/531\/revisions\/541"}],"wp:attachment":[{"href":"https:\/\/www.kitchencloset.com\/home\/bryan\/blog\/wp-json\/wp\/v2\/media?parent=531"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kitchencloset.com\/home\/bryan\/blog\/wp-json\/wp\/v2\/categories?post=531"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kitchencloset.com\/home\/bryan\/blog\/wp-json\/wp\/v2\/tags?post=531"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}