Enter Search Query:

  • 3049
  • Close

Find a string and copy its whole line to new file

The other day I got a zip archive of a hundred xml files. The task was to identify a single key word/phrase (STR_RESTORING_FILE_FAILED) in each xml file that indicated a failure to restore a file (part of a larger job) and create a new file with just line in each xml file that matched the key word/phrase. Here’s what I did to obtain the new file:


Windows PC capable of running DOS


[step 1] Create a new batch script named find-string-copy-results-new-file.bat. Ensure that the files you want to search and pull data from are in a folder on your computer (i.e. Desktop\logs).

[step 2] Edit the following code by specifying the source and destination folders you want to use (NOTE: Destination must exist) and paste it into your editor:

@echo off
setlocal enableextensions
set "source=C:\Users\david\Desktop\logs"
set "target=C:\Users\david\Desktop\output\output.txt"
pushd "%source%"
(for /f "tokens=1,* delims=:" %%a in ('findstr /i /l /c:"STR_RESTORING_FILE_FAILED" "*.xml"') do (
)) > "%target%"

[step 3] Run your batch file and you’re done.

That’s it.

May 19, 2015

Related Posts