Nitish Yadav
Nitish Yadav

Reputation: 55

writing in a file using command from windows command prompt?

I am working to install a product on my windows 7 system, i need to search for a line in a text file, inside the component folder by name IRU_install.properties there i have to search for a line licenseType=LICR and need to append a line "licenseAccepted=9" after the line is found in the properties file.I do not want to prompt the user as the installation need to be through Batch script and silent installation process.

Upvotes: 0

Views: 145

Answers (1)

TechnicalTophat
TechnicalTophat

Reputation: 1725

You can use the findstr command to find text within a file. See below for an example:

findstr /m "licenseType=LICR" IRU_INSTALL.properties
if %errorlevel%==0 (
echo Line found!
)

This finds the string specified in the file specified. The /m switch tells the command to print the filename ONLY if there is a match. use findstr /? to learn more.

As for appending text, this can be super difficult in Batch, but it can be done.

See the below code:

@ECHO OFF
(
  FOR /F "tokens=*" %%A IN (IRU_install.properties) DO (
    ECHO %%A
    IF "%%A" EQU "licenseType=LICR" (
      ECHO licenseAccepted=9
    )
  )
) >output.txt
move /y output.txt IRU_install.properties

What this does is it uses the FOR command to iterate through the lines in the file and if the current line equals the wanted line, then output the license accepted line to a 'output.txt' file. Then move all the text in that to the .properties file.

Upvotes: 1

Related Questions