Jump to content
Sign in to follow this  
hirschy

How to replace a word in a txt file?

Recommended Posts

hirschy

First off, great product!

What I have done so far, is create an input form to set the machine info for an XP installer.

This does work, but I can only add text to the end of the file I want to modify.

I have searched the website and read a LOT of help files, but I cannot find what I need.

Bottom line, I want to have these 3 lines modified in the uaf text file. I would like to just replace part of a line, say FNAME, ONAME and CNAME in the uaf.txt file.

#include <GUIConstants.au3>
#include <File.au3>

GUICreate("XP Installer, ver 1", 400, 250)
GUISetFont(12)

GUICtrlCreateLabel("Please verify information:", 4, 4)
GUICtrlCreateLabel ("PC Name", 10,30)
GUICtrlCreateLabel ("User Name", 10,80)
GUICtrlCreateLabel ("Company", 10,130)
$put1 = GUICtrlCreateInput("TrainingXX", 100, 30, 200, 25) 
$put2 = GUICtrlCreateInput("Student", 100, 80, 200, 25)
$put3 = GUICtrlCreateInput("TRAINING", 100, 130, 200, 25)
$send = GUICtrlCreateButton("Save Changes", 4, 200, 120, 25)
$EXIT = GUICtrlCreateButton("Exit", 150 ,200, 120, 25)

GUISetState()

While  1
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
    If $msg = $send Then SendMyData ()
    If $msg = $EXIT then Exit
WEnd

Func SendMyData()
    $data = @CRLF & 'ComputerName="' & GUICtrlRead($put1) & '"' & @CRLF & 'FullName="' & GUICtrlRead($put2) & '"' & @CRLF & 'OrgName="' & GUICtrlRead($put3) & '"' 
    FileWrite('c:\install\uaf.txt', $data)
    MsgBox(0,'Info','Data written to UAF.txt')
EndFunc

The uaf text file is like this:

;SetupMgrTag

[UserData]
    FullName="FNAME"
    OrgName="ONAME"
    ComputerName="CNAME"

[Data]
    AutoPartition=1
    MsDosInitiated="0"
    UnattendedInstall="Yes"
    AutomaticUpdates="Yes"

[Unattended]
    UnattendMode=FullUnattended
    OemSkipEula=Yes
    OemPreinstall=Yes
    TargetPath=\WINDOWS
    FileSystem=ConvertNTFS

Is this possible? And it has to have the quotes around it, hence the code above.

Thank you

Share this post


Link to post
Share on other sites
hirschy

:D

I know, RTFM...

I looked at that for hours yesterday, but did not see the writing on the screen...

I finally figured out it was me, since I have only used AutoIT for a week :)

All I needed to do was change my function, and it works gret now!

Thanks for pointing me back to the right place :D

Func SendMyData()
$filename = "c:\install\uaf.txt"
$find1 = "CName"
$replace1 = GUICtrlRead($put1)
$find2 = "FName"
$replace2 = GUICtrlRead($put2)
$find3 = "OName"
$replace3 = GUICtrlRead($put3)

$retval1 = _ReplaceStringInFile($filename,$find1,$replace1)
$retval2 = _ReplaceStringInFile($filename,$find2,$replace2)
$retval3 = _ReplaceStringInFile($filename,$find3,$replace3)
MsgBox(0,'Info','Data written to UAF.txt')
EndFunc

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×