-2

this has to be done in batch.

I need to be able to read an input file, and parse out certain sections only of lines that contain a specific string, then write that to an output file. For example:

input =test.properties file
SQL
Datamodel
sale_detail.sql
bpid_exclusion.sql

expected output =

SQL
Datamodel
sale_detail.sql
bpid_exclusion.sql

This is what I have so far:

@setlocal enableextensions enabledelayedexpansion
set 
log_filepath=PATH/config.sql
FOR /F "usebackq tokens=* delims=" %%A IN (test.properties) DO (
set temp=%%A
ECHO %%A >> %log_filepath%

The problem I have right now is that when I run this script, it's printing the complete properties file, but what i want is after reading the file it should check if the word SQL exists in the file or not if yes it should check for the next word datamodel if data model exists it should print the next two lines which is sale_Detail.sql and bpid_exclusion.sql in the log_filepath

  • What have you tried so far? –  Apr 27 '18 at 04:38
  • This is my bat file FOR /F "usebackq tokens=1* delims==" %%A IN ("%~dp0\test1.properties") DO ( ECHO %%A>> %log_filepath% ) @endlocal my requirement is to validate if string1 exists go to string 2 if string 2 exists print all the contents of string2. You can refer to the below test1.properties file S1 S2 fix1.sql fix2.sql – Shalini Singh Apr 27 '18 at 04:47
  • Put that in your question in a readable format for us to review. If you start a line with 4 spaces, then begin typing, it will come up as code like `this` –  Apr 27 '18 at 04:49
  • Please help me on this – Shalini Singh Apr 27 '18 at 05:22
  • You want to validate if `String1`, `String2`, `file1.sql` and `file2.sql` exists where? It helps when writing a question, more especially one which requires an 'immediate' answer, to explain your task fully. – Compo Apr 27 '18 at 11:05
  • Yes I completely agree. My bat file is reading the property file as of now. In that property file i want to add validation on words i.e; if string1 in line1 exists then only it should read the contents of next line. if string2 in line 2 exists then it should read other line. – Shalini Singh Apr 28 '18 at 06:02

1 Answers1

0

Your question is unclear, but I am guessing you have a file with filenames. If the filename in the file exists in a directory echo the content. If that is the case, try this.

@echo off
Set "infile=%~dp0test1.properties"
set "log_filepath=d:\somedir\logfile.log"
for /f "delims=" %%a in (%infile%) do If exist "%%a" type "%%a" >> %log_filepath%
Gerhard
  • 22,678
  • 7
  • 27
  • 43
  • my file is something like this SQL Datamodel sale_detail.sql exclusion.sql There are two files in Data model it should be printed if it finds datamodel and before that it should check the presence of SQL – Shalini Singh Apr 27 '18 at 09:01
  • Please edit your question and clarify. Give examples of each file and expected result. – Gerhard Apr 27 '18 at 09:22
  • I've edited and updated my necessary requirements please see and provide a solution if you can - Gerhard Barnad – Shalini Singh Apr 30 '18 at 07:17