|
Answer» I have x text files, each of which has multiple lines. I need to grab the first line of each text file and WRITE them all to a single new text file.
I found this link: http://www.computing.net/answers/programming/extract-first-line-of-text/13939.html -- it works for a single text file, but I can't get it to handle multiples, it either ends up returning all lines or just the first line of the first text file, I -think- because of the way it does variable substitution.
*** The end objective is actually that I have a LIST of filename prefixes, I need to find the most recent file in the directory matching each prefix. If DIR had a switch saying only report the first MATCH by date or something similar, that would circumvent the above problem entirely.
Suggestions? Pointers? I'm confident enough with standard DOS commands but am still getting my head around how FOR works in batch files.Hi The very simplest way to grab the first line of a file is this :
Set /P MyLine= The top line of YourFileName now lives in %MyLine%
So what you want to do is a series of directory listings, sorting the output by date (reversed to put the latest on top) :
Code: [Select]Echo Off :: blank out the contents of the list file > ListFile Echo.
:: for each prefix value, find the latest file with the prefix For %%A in (Prefix1 Prefix2 Prefix3) Do Call :ListAPrefix %%A
:: the end GoTo :EOF
::======================================= :ListAPrefix
:: get a directory listing based on the passed in prefix > filelist Dir /b /o-d %1*.*
:: get the top line (newest file) Set /P MyLine=<filelist
:: add it to the list >> ListFile Echo %MyLine%
The file Listfile now CONTAINS all of the wanted files Hope this helps Graham
|