Link to home
Start Free TrialLog in
Avatar of icecom4
icecom4Flag for United States of America

asked on

Windows batch script to edit existing files

Hello all.  I need a batch script that can do the following...

Search in the directory c:\program files\bquest\

1) Find any and all files called "config.cfg" (basically a text file) in directory and subdirectories
2) Find 3 lines in the file, delete them individually, and then add 3 new lines to the file

exact lines to be deleted...
com_hunkmegs "56"
com_soundmegs "24"
com_zonemegs "24"

exact lines to be added (only if lines above were found and deleted)
com_hunkmegs "192"
com_soundmegs "32"
com_zonemegs "32"

There should be no output, and no error message if it can not find the exact lines to be deleted. If these exact line values are not found...the new lines should NOT be added to the file.  

Thanks in advance!!
Avatar of Qlemo
Qlemo
Flag of Germany image

Do you always require ALL 3 lines to be there, or is each line to be considered and replaced individually?
Would it be ok to just replace the contents line by line, with a 3rd-party tool?
Avatar of icecom4

ASKER

Also, i tried to edit but you replied too quickly for me.  There are five lines to be edited actually...

No 3rd party tools please, all in 1 batch...

And actually, I am ok with simply deleting all the lines and replacing with the lines below it.  It might be a nightmare to do otherwise.  

delete values starting with...
seta com_hunkmegs
seta com_soundmegs
seta com_zonemegs
seta seta cl_punkbuster
seta cl_punkbuster

replace with new line and values...
seta com_hunkmegs "192"
seta com_soundmegs "32"
seta com_zonemegs "32"
seta seta cl_punkbuster "1"
seta cl_punkbuster "1"
One question left is, and let me try to be more explicit this time:
If «seta com_hunkmegs» is found, delete it and append «seta com_hunkmegs "192"»
and the same for each of the 5 lines individually.
OR
Only if all 5 lines are found, "replace" them.
Avatar of icecom4

ASKER

Yes, replace.  Will the value also get deleted after seta com_hunkmegs for example?  Because these values change and I dont want value left over by itself.  
Avatar of icecom4

ASKER

and this is each line...not only if all 5 are found
SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of icecom4

ASKER

I do not know much about vbs, does the average windows user have everything ready to run such a file?

Also, do I just copy and paste all this into a file called whatever.vbs?

Sorry, this is my first attempt at using a vbs file.  

Also, a batch solution is still welcome :D
Avatar of Bill Prew
Bill Prew

==> I do not know much about vbs, does the average windows user have everything ready to run
==> such a file?

Yes.

==> Also, do I just copy and paste all this into a file called whatever.vbs?

Yes.

~bp
Are there any blank lines in the file?

~bp
Getting late here, but here's a quick shot at a BAT approach.  Save it as a BAT file, and run it like:

EE27510872.bat "c:\program files\bquest\config.sys"


@echo off
setlocal EnableDelayedExpansion

if "%~1" == "" (
  echo ERROR - No source file spcified.
  exit /b 1
)
set InFile=%~1

set OutFile=%TEMP%\_temp_.txt

if exist "%OutFile%" del "%OutFile%"

for /F "usebackq tokens=*" %%A in ("%InFile%") do (
  set OutData=%%A
  if /I "%%A" == "seta com_hunkmegs"       set OutData=seta com_hunkmegs "192"
  if /I "%%A" == "seta com_soundmegs"      set OutData=seta com_soundmegs "32"
  if /I "%%A" == "seta com_zonemegs"       set OutData=seta com_zonemegs "32"
  if /I "%%A" == "seta seta cl_punkbuster" set OutData=seta seta cl_punkbuster "1"
  if /I "%%A" == "seta cl_punkbuster"      set OutData=seta cl_punkbuster "1"
  echo !OutData!
)>>"%OutFile%"

if exist "%OutFile%" (
  copy /Y "%OutFile%" "%Infile%
  del "%OutFile%"
)

Open in new window

~bp
Avatar of icecom4

ASKER

I don't see where this batch references the directory or config.cfg file.  Does it have to be run in same directory is that why?

The file name to process is passed as a command line parm, not hard coded into the script.  See my prior post for example command line.

~bp
Avatar of icecom4

ASKER

I notice that you put "config.sys" and that does not find anything, but the file name it is config.cfg
so I changed it and it does find.  However I also notice that this does not find config.cfg in all subdirectories and has to be pointed to a specific one.  That will not work as there are many different user folders that may have this file.  But for testing purposes I am ok with pointing directly to file.  

I ran the vbs script with passing command...
All it said was...
"Microsoft (R) Windows Script Host Version 5.7
Copyright (C) Microsoft Corporation. All rights reserved"
It did not add or delete any lines

I ran the batch and it did not report anything and did not edit any lines either





Avatar of icecom4

ASKER

perhaps to simply this even further, can we make a batch that finds all text files in a directory/subdirectories named "config.cfg"  and simply append the below lines to end of file?

seta com_hunkmegs "192"
seta com_soundmegs "32"
seta com_zonemegs "32"
seta seta cl_punkbuster "1"
seta cl_punkbuster "1"

I am ok with not deleting because any duplicate commands at end of file take priority over the ones before it.  So maybe this will allow for a less complex batch?

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of icecom4

ASKER

Thanks guys.  All of these worked to some extent with minor adjustments, Qelmo's worked perfectly.