I expect developers to program like I do, which didn't happen this week.
The task is to concatenate log files in a folder into a single file to import into a database. The Windows command to do the job is for /f %a in ('dir /b *.csv') do type %a >> dest.csv
. (dir /b
lists entries in a folder without any heading.) While testing using echo
in place of type
, I noticed that due to the way dir
sorts file, the script would concatenate log files in this order: Log-02.csv, Log-03.csv, ..., Log.csv i.e. the first log file last. This order seems to correspond with the characters in the ASCII table.
(Aside: Windows Explorer lists Log.csv first because Explorer doesn't use ASCII sort order.)
Of course, just to get the job done, the work-around is to rename Log.csv to Log-01.csv before running the script.
It threw me that developers wouldn't name log files consistently.
I make log files of the form year-month-day
ReplyDeleteThe only problem is that sometimes (depending on which bit of code generates the filenaem) I get a month of 1 digit
2001-1-1
vs
2001-01-1
This is a coding problem on my part.
I disagree Kam.
ReplyDeleteYou said '...I noticed that due to the way dir sorts file...', but you have not specified any sort order for the 'dir' command.
MS says: "If you omit /o, dir displays the names in the order in which they occur in the directory". Whatever that means. Perhaps it's the order in which they occur in the MFT?
(http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/dir.mspx?mfr=true)
Regardless, if you omit some form of ordering in your command you are on your own. Perhaps 'dir /b /o:n' or 'dir /b | sort'.
Regards
P.S. If you have no expectations of other developers you can avoid disappointment. :)
Good tip. 'dir /b /o:n' gives the result I want so I don't need the workaround any more. I had assumed (incorrectly) that the result is always sorted in some order.
ReplyDelete