1

I have ~800 .txt files. I need extract the name of each file, and one line inside the file, and export it to a new .txt file.

The line I need to copy will always start with "System Boot Time:"

I'd like it to output inside the new .txt file something like this:

txtfile1

System Boot Time: xx/xx/xxxx

txtfile2

System Boot Time: xx/xx/xxxx

txtfile3

System Boot Time: xx/xx/xxxx

etc...

What commands can I use to do this?

  • In addition you haven't told us what operating system and scripting language you are using. – DavidPostill Mar 3 '16 at 0:22
  • I am using windows 7 and writing a .bat script – Snugglasaurus Mar 3 '16 at 0:26
  • for /f, dir, findstr, echo should get you started. – DavidPostill Mar 3 '16 at 0:27
  • An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related. – DavidPostill Mar 3 '16 at 0:29
  • Thank you! It's difficult to know what to search for when you are very new to scripting. We know what we want in plain English but it's difficult to translate it to script-speak. – Snugglasaurus Mar 3 '16 at 0:33
0

I need extract the name of each file, and one line inside the file, and export it to a new .txt file.

The line I need to copy will always start with System Boot Time:

I'd like it to output inside the new .txt file something like this:

txtfile1
System Boot Time: xx/xx/xxxx
txtfile2
System Boot Time: xx/xx/xxxx
txtfile3
System Boot Time: xx/xx/xxxx

Use the following batch file.

Merge.cmd:

@echo off
setlocal 
for /f "useback delims=: tokens=1-3" %%i in (`findstr /C:"System Boot Time:" "*.txt"`) do (
  echo %%i >> new.txt
  echo %%j:%%k >> new.txt
)
endlocal

Example output:

F:\test>type ?.txt

1.txt


System Boot Time: 01/01/1900

2.txt


System Boot Time: 01/02/2003

3.txt


System Boot Time: 02/03/2004
    
F:\test>merge

F:\test>type new.txt
1.txt
System Boot Time: 01/01/1900
2.txt
System Boot Time: 01/02/2003
3.txt
System Boot Time: 02/03/2004

Notes:

  • All files matching *.txt are searched for a string matching `System Boot Time
  • Results are output to new.txt
  • If you want to run Merge.cmd multiple times remember to del new.txt first.

Further Reading

| improve this answer | |
  • Wow! That's great. Thank you so much - you've done a lot better than my pitiful attempts. One question - the new.txt file is returning every line that contains any of the words "System", "Boot" or "time". Is there a way I can make it match the entire string? Thanks! – Snugglasaurus Mar 3 '16 at 17:59
0
  • You can use sed or even grep to output a line from a text file.

    (oh, I see you're on Windows. If you don't have grep or sed installed, you should be able to use findstr instead.)

  • Put this command in a loop over the files needed.
  • Redirect output to a file.
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.