1

I have several Textfiles with a header of no interest and some relevant data. I want a new file with all the important data:

Example:

File1 has text:

Created on 02.02.2016
Country: Chile
Figure of Station: CAL
Frequency: 220 Hz
Messuring: 15 hours

File2 has text:

Created on 02.02.2016
Country: Chile
Figure of Station: GUA
Frequency: 220 Hz
Messuring: 14 hours

I would like to have new csv with all important data looking like:

CAL 220 Hz 15 hours
GUA 220 Hz 14 hours

Is it possible to do something like this with a batch file?

I know about copy .csv all .txt but this doesn't work here.

Here is a batch script I have as well.

@echo off
setlocal EnableDelayedExpansion
if exist result.csv del result.csv
for %%f in (*.txt) do (
    set i=0
    for /F "delims=" %%l in (%%f) do (
        set /A i+=1
        set line!i!=%%l
    )
    echo %%f, !line1!, !line2!, !line3!, !line4!, !line5!, >> result.csv
)
3
  • Yes, it is possible, however please note that Super User is not a script writing service. If you tell us what you have tried so far (including any scripts you are using) and where you are stuck then we can try to help with specific problems. You should also read How do I ask a good question?.
    – DavidPostill
    Commented Jun 3, 2016 at 18:52
  • Hello, yes I understand. I m pretty clueless with cmd commands. My programming "skills" are limited to Matlab and GMT and even there give me a hard time! My script idea looks like this. My main problem seems now it copies everything but I just want the relevant part of my stations i measured
    – Loomdock
    Commented Jun 3, 2016 at 23:45
  • It's a start I suppose. I might look at it tomorrow if I have time.
    – DavidPostill
    Commented Jun 3, 2016 at 23:49

1 Answer 1

0

I want a new file with all the important data

I've modified your batch file to produce the results you want.

Use the following batch file (test.cmd):

@echo off
setlocal EnableDelayedExpansion
if exist result.csv del result.csv
for %%f in (file*.txt) do (
  set _line=
  rem we don't need the first 2 lines so skip them
  rem we only need the data after the : 
  rem so use : as the delimeter and get the rest of the tokens after the delimeter 
  for /f "skip=2 tokens=2* delims=:" %%l in (%%f) do (
    set _value=%%l
    rem strip off the space that is after the : and build up the output line
    set _line=!_line!!_value:~1! 
    )
  rem write the output line to the file
  echo !_line! >> result.csv
  )
endlocal

Notes:

  • Change file*.txt to match your text files.

Example usage:

F:\test>type file*.txt

file1.txt


Created on 02.02.2016
Country: Chile
Figure of Station: CAL
Frequency: 220 Hz
Messuring: 15 hours
file2.txt


Created on 02.02.2016
Country: Chile
Figure of Station: GUA
Frequency: 220 Hz
Messuring: 14 hours
F:\test>test

F:\test>type result.csv
CAL 220 Hz 15 hours
GUA 220 Hz 14 hours

F:\test>

Further Reading

2
  • First of all. Thanks very much it does the trick. Now a question for the curious: Skip can be used for starting lines is there also a command where it stops at a certain point? I only found the possibility do define it by lines. ss64.com/nt/for_f.html
    – Loomdock
    Commented Jun 7, 2016 at 7:51
  • @Loomdock You have to add your own logic to stop the loop. There are many ways to do that depending on the specific circumstances. Basically you use goto label to break out of for loops.
    – DavidPostill
    Commented Jun 7, 2016 at 8:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .