(blogs let others gawk)

January 21, 2014

DOS magic (today’s grody hack)

Filed under: Grody Hacks — Tags: , , , , — Bryan @ 6:00 pm

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…)

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’s contents in order to create the new single CSV file and pre-pend today’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.

First, I whip open my friendly Notepad application and create a header.csv text file that contains the column headers for the CSV.

Then using Notepad again, after some unnecessarily painful searching, I finally was able to cobble together this, which I then Select All > Copy, then Paste into my DOS prompt (CD’d to the folder with the files first):

type header.csv > %date:~10,4%%date:~4,2%%date:~7,2%-SERVER1.csv && for %f in (*SERVER1*.log) do @echo. >> %date:~10,4%%date:~4,2%%date:~7,2%-SERVER1.csv && type “%f” >> %date:~10,4%%date:~4,2%%date:~7,2%-SERVER1.csv
type header.csv > %date:~10,4%%date:~4,2%%date:~7,2%-SERVERDB.csv && for %f in (*SERVERDB*.log) do @echo. >> %date:~10,4%%date:~4,2%%date:~7,2%-SERVERDB.csv && type “%f” >> %date:~10,4%%date:~4,2%%date:~7,2%-SERVERDB.csv

(….repeat as needed… etc…)

  • for %f in … (runs a loop of the full command-line against the wildcard based on the number of files matching the wildcard)
  • @echo. (with the important, trailing “.” outputs the new-line)
  • %date:… (captures just a portion of the “date” value and echos it)
  • && (appends additional command to the line)
  • >> (appends the output of the line to the file)

Yeah, it’s crude, but I’ll take the win.

If I really wanted to be fancy, I’d try and capture the date from the source files and use that for the imprint on the destination, but I’m not that inspired. Sorry.

I’m posting this on the hopes that if someone spirals into this kind of situation like I did today, they’ll find this post and go “Yes! That’s exactly what I needed!”. Good luck.

I’ll file this under TIL about abusing “date” and “&&” in a new way, or maybe I only learned how much DOS I’ve forgotten. Hmm…