Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Batch File - To write string to a text file

Status
Not open for further replies.

UrbaneRove

Programmer
Mar 21, 2002
48
0
0
CA
Hello there.

I need some help in writing a Batch file to write an update to a text file.

The text file has a modem connection string that needs to be updated on machines aound the network. The idea is to write a simple batch program that will open the file (modem.cfg) which is a standard text file and then find the current connection string (mdmconnect: AS454...) and replace it with the current string (mdmconnect: AS558...). Also at that time to make a note in the same file of when and how it was updated.

What I need to know:
1) Is how to find the line within the text file
2) How to output a string from a batch file to another text file.
3) How to replace a line or characters in that text file
4) How to add a line to that text file. The added line would be in the middle of the page of text.

Any help is greatly appreciated. An example or on-line documentation would be perfered. I've looked but I can't seem to find what I need.

Thanks for any time and help.

Urbane Rove
 
I think that finding and replacing text is beyond the scope of DOS batch commands, although someone else may be able to tell me otherwise.
In UNIX I could do what you want easily enough using SED (Stream Editor). I just did a quick search on Google and came up with at least 1 freeware version of SED for Windows. I'm sure that there are other tools that you could use to complete the task, but this is the only one that I can think of off the top of my head.
 
UrbaneRove !

You could write a simple program in C language, that will take care of yr adding and other querries.

good luck From:
Apoorva Gala
You can mail me at apoo1972@rediffmail.com
 
Thanks everyone for the ideas and time to help. The third party text replacing program works great but is not quite what I need. The idea of writing it myself my be my best bet and packing it into the batch .

Thanks all.

Urbane Rove
 
I whipped up a little batch file that generates a small QBasic program that should do the trick for you. You must, of course, have QBasic.exe on your machine for it to work. Didn't finish the "help" portion yet, but if you copy the last part of this post to a .BAT file, it'll run....;0)

@ECHO OFF
REM ***** KEYSRCH.BAT 11-26-2002 18:05:04 *****>~.BAS

REM Path & name of file to be modified
SET FIL=MODEM.CFG

REM Name of key in file to alter
SET SRCH=mdmconnect:

REM New data for selected key
SET RPLC=""

IF "%1"=="?" GOTO HELP
IF "%1"=="/?" GOTO HELP
IF NOT "%1"=="/HELP" GOTO DOIT
:HELP
CLS
ECHO.
ECHO.
ECHO.
ECHO Syntax: KEYSRCH [file path & name] [key to modify] [new data string]
ECHO.
ECHO Examples:
ECHO KEYSRCH with no arguments looks for the default key "mdmconnect:" (with no space after the colon) in the file "MODEM.CFG" (in the current work directory), and asks for a replacement data string. (Be sure to insert the proper space at the beginning of the string.)
ECHO.
ECHO KEYSRCH "C:\WINDOWS\DESKTOP\Test the water.FIL" "Modem Type:" hellogoodbye
ECHO
ECHO Change default values by editing lines 5, 8 & 11 of this file.
PAUSE
GOTO END
:DOIT
IF NOT "%1"=="" SET FIL=%1
IF NOT "%2"=="" SET SRCH=%2
IF NOT "%3"=="" SET RPLC=%3
ECHO.CLS:LOCATE 12:Q$=CHR$(34):GRT$=CHR$(62):FIL$=ENVIRON$("FIL"):SRCH$=ENVIRON$("SRCH"):RPLC$=ENVIRON$("RPLC"):ON ERROR GOTO 70>~.BAS
ECHO.10IF LEFT$(FIL$,1)=Q$ THEN FIL$=MID$(FIL$,2)>>~.BAS
ECHO.P%%=LEN(FIL$):IF RIGHT$(FIL$,1)=Q$ THEN P%%=P%%-1:FIL$=LEFT$(FIL$,P%%)>>~.BAS
ECHO.IF INSTR(Q$+Q$,FIL$) THEN PRINT"Enter name of file to modify (include path if not in the current directory). ":LINE INPUT FIL$:GOTO 10>>~.BAS
ECHO.20P%%=P%%-1:IF NOT MID$("\"+FIL$,P%%,1)="\" THEN 20 ELSE F$=MID$(FIL$,P%%):IF INSTR(FIL$," ") THEN SHELL "DIR /4 "+Q$+FIL$+Q$+GRT$+"~":OPEN"I",#1,"~":FOR I%%=1 TO 6:LINE INPUT #1,A$:NEXT I%%:RESET:IF NOT UCASE$(MID$(A$,47))=UCASE$(F$) THEN ERROR 53 ELSE F$=RTRIM$(LEFT$(A$,8)):IF NOT MID$(A$,10,3)=" " THEN F$=F$+"."+RTRIM$(MID$(A$,10,3))>>~.BAS
ECHO.FIL$=LEFT$(FIL$,P%%-1)+F$:OPEN"I",#1,FIL$>>~.BAS
ECHO.30IF LEFT$(SRCH$,1)=Q$ THEN SRCH$=MID$(SRCH$,2)>>~.BAS
ECHO.P%%=LEN(SRCH$):IF RIGHT$(SRCH$,1)=Q$ THEN P%%=P%%-1:SRCH$=LEFT$(SRCH$,P%%)>>~.BAS
ECHO.IF INSTR(Q$+Q$,SRCH$) THEN PRINT"Enter name of key to modify.":LINE INPUT SRCH$:GOTO 30>>~.BAS
ECHO.40IF INSTR(Q$+Q$,RPLC$) THEN PRINT"Enter new data string (with leading space if needed).":LINE INPUT RPLC$:GOTO 40 ELSE OPEN"O",#2,"~">>~.BAS
ECHO.50LINE INPUT #1,A$:L%%=L%%+1:IF A$="; *" THEN LINE INPUT #1,A$:LINE INPUT #1,A$:GOTO 50>>~.BAS
ECHO.IF LEFT$(UCASE$(A$),P%%)=UCASE$(SRCH$) THEN B$=LEFT$(A$,P%%)+RPLC$:pRINT#2,"; *":pRINT#2,";"+A$:pRINT#2,";Replaced on "+DATE$+" at "+TIME$+" by:":pRINT A$:pRINT"( Line"; L%%; ") Replaced on "+DATE$+" at "+TIME$+" by:":pRINT B$:SWAP A$,B$:Q%%=1>>~.BAS
ECHO.PRINT#2,A$:IF EOF(1)=0 THEN 50 ELSE RESET:IF Q%%=0 THEN PRINT"**** NO MODIFICATIONS MADE ****":KILL "~" ELSE KILL FIL$:NAME "~" AS FIL$>>~.BAS
ECHO.PRINT"Press any key to exit.">>~.BAS
ECHO.60IF INKEY$="" THEN 60>>~.BAS
ECHO.SYSTEM>>~.BAS
ECHO.70IF ERR=53 AND ERL=20 THEN PRINT Q$+FIL$+Q$+" does not exist.":pRINT:FIL$="":RESUME 10>>~.BAS
CALL QBASIC /RUN ~.BAS
rem IF EXIST ~.BAS DEL ~.BAS
:END
SET FIL=
SET SRCH=
SET RPLC=
 
Since this is WINDOWS 95 & 98 forum, I would suggest that you use QBasic that is located in the WINDOWS\COMMAND directory (if defaults were accepted AND complete install was issued) to handle the type of manipulation you are talking about.

A batch file could be created; however, there would be so many pointers and variables that it would not be a simple task NOR a 'small' batch (as you so quaintly put it.)

If you decide to use QBasic, there is a forum here in Tek-Tips you may get some answers from.

If you still choose to go batch, then I'd suggest you start off with some reading on the following: IF, FIND, >, >>, <, SET [for variables & counters/accummulators], GOTO [for looping], CALL [may/may-not be needed], MORE, and EXIT :just to start off with.

I'd still perfer QB over a batch for this type of work .

--MiggyD
 
The operative in my previous post IS a QB file. It's just loaded and invoked within the batch file. (and that quaint little file works, by the way)
 
To Latefersupper:

We must have posted almost at the same time, else, I wouldn't have responded.

It seems we both must have been on the same wavelength or something.

--MiggyD
 
Must've been. I'd been meaning to whip up something like that for my own use, but never got around to it.
Cheers
 
Is this 'lateral thinking' - or just plain stupid??!
Create a self-extracting zip file containing the directory structure and an example of the modified .cfg file, and simply unzip it on all machines, allowing it to overwrite the original file.... My suggestions are what I would try myself. If incorrect, I welcome corrections to my rather limited knowledge. Andy.
 
to hotfusion
The zip file will be fine if nothing else in the .cfg file is subject to change. Otherwise, zip won't work.
Oh, and maybe you'd be better received if you weren't so rude.
 
Hey there.

I thought about the zip file idea but the .cfg files on the machine are a little different and therefore unique to the machine so we have to work with the resident file.

I will take a look at the previous batch file although I am not familiar with Qbasic at all. Just a new thing to learn.

I was thinking of writing a small vb or c++ app to do what I need but the idea is still developing.

Urbane Rove
 
There are a few characters that cannot be transferred from a batch file (such as &quot;=&quot;) so I added a line of code to handle that issue. Leave your eddress and I'll send you the whole thing with simple instructions.
 
Have you considered using WSH (Windows Script Host)?

Below is a solution written in VBScript: [2thumbsup]
[tt]
Option Explicit
'==========================================================================='
' &quot;modemcfg.vbs&quot; Date Created: 01-Dec-2002 Date Updated: 01-Dec-2002
'
' This VBS (Visual Basic Script) program does the following:
' reads a file, modifies it, writes it to a file, and copies or deletes it.
'
' To test, either double-click on the filename in Windows Explorer or
' from the Command prompt, type &quot;cscript modemcfg.vbs&quot;.
'
' Changes:
' -------------------------------------------------------------------------
' 01-Dec-2002.0 Created.
'
'==========================================================================='
Const cVBS = &quot;modemcfg.vbs&quot;
'*
'* Start Message
'*
MsgBox Now & &quot;: '&quot; & cVBS & &quot;' started.&quot;
'*
'* Declare Variables
'*
Dim intOTF
intOTF = 0
Dim strOTF
Dim strOT1
strOT1 = &quot;modem.cfg&quot;
Dim strOT2
strOT2 = &quot;modem.cfg.txt&quot;
'*
'* Open Files
'*
Dim objFSO
Set objFSO = CreateObject(&quot;Scripting.FileSystemObject&quot;)
'*
Dim objOT1
Set objOT1 = objFSO.OpenTextFile(strOT1,1)
Dim objOT2
Set objOT2 = objFSO.OpenTextFile(strOT2,2,true)
'*
'* Read and Write Files
'*
Do While Not objOT1.AtEndOfStream
strOTF = objOT1.ReadLine()
If (strOTF = &quot;mdmconnect: AS454...&quot;) Then
strOTF = &quot;mdmconnect: AS558...&quot;
objOT2.WriteLine(&quot;The following line was replaced on: &quot; & Now)
End If
objOT2.WriteLine(strOTF)
Loop
'*
'* Close Files
'*
objOT1.Close()
Set objOT1 = Nothing
objOT2.Close()
Set objOT2 = Nothing
'*
'* Replace old file (optional); Delete new file
'*
If MsgBox(&quot;Replace?&quot;,vbYesNo + vbQuestion,cVBS) = vbYes Then
objFSO.CopyFile strOT2, strOT1, True
End If
objFSO.DeleteFile strOT2
'*
Set objFSO = Nothing
'*
'* Finish Message
'*
MsgBox Now & &quot;: '&quot; & cVBS & &quot;' finished.&quot;
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top