I want to write a batch file to read an input text file, extract information from it put it in an output file.
- Each line in the input file has different information so I want the batch to loop through each line.
- I want to extract certain information from the input file and discard the rest.
- The input file can have any number of lines.
- I want to leave an error message if the input line has no useful information.
- I need two counters, the first telling the number of lines in the input file, and the second telling the number of lines in the output file (without counting empty lines) .
- I want the batch to treat special characters like normal letters.
For example:
FILE_NAME=apple FILE_SIZE=312 C=fwef sdf asdetg
FILE_SIZE=7867 C=ehtrghr FILE_NAME=sea&ocean G=tryr yujg
C=gert FILE_NAME=chair=12 tgrgd sfsf FILE_SIZE=66
dfgg ertergf C=ert A=344
fgdfg FILE_NAME=cat
I want to extract only the FILE_NAME=XXX and FILE_SIZE=XXX part, discarding everything else in that line. The output should be:
-name apple -size 312
-name sea&ocean -size 7867
-name chair=12 -size 66
ERROR!!!
-name cat
input_count=5 and output_count=4
People don’t give enough credit to batch scripts. Try this:
Here’s a little explanation about what this does:
forloop reads the file line by line, the contents of each line being sent as input arguments to theParseLinesubroutine.ParseLinesubroutine receives each line instrand iterates through its space-separated words (using jeb’s advanced method described here).The logic of the subroutine loop is pretty straightforward: it breaks each word into
tag(the text that precedes=) andvalue(the text that follows=), and setsfile_nameandfile_sizeaccordingly.I believe that this method can handle pretty much any input file, with or without special characters.