Jump to content

stringreplace


Recommended Posts

i have a .txt LOG file for a LAN chat room thing... uhhh and i want to search the file and if it says '{CurrentUserNames.}' then i want it to say the @Username = ClientName ..... but 1 second later i want it to replace '{CurrentUserNames.}' with ''... i have searched, tried stringreplace()... and

$File_Read = @scriptdir & '\LOG.txt'

StringRegExpReplace ($File_Read,'{CurrentUserNames.}',' ')

but nothing, anyhelp?

Link to comment
Share on other sites

If a character is a metacharacter and it's not within a class then it has to be escaped or use the \Q...\E block to ignore all metacharacters meaning except for the \Q...\E it-self. For example the '.' match anything if it can (even multi-lines with (?s) switch), and '{}' pair are for repetition amount. Read the help file and if it's not the case then post some code please.

Link to comment
Share on other sites

here is my Code for the LAN chat NO FINISHED

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_icon=\\Svrss1\RSUsers\barrettc\My Documents\Mail.ico
#AutoIt3Wrapper_outfile=LANCR.exe
#AutoIt3Wrapper_Compression=4
#AutoIt3Wrapper_Res_Comment=Used For Shared DRIVES And Also A Substitute For TCP
#AutoIt3Wrapper_Res_Description=LAN ChatRoom
#AutoIt3Wrapper_Res_Fileversion=0.2.3.71
#AutoIt3Wrapper_Res_LegalCopyright=Bob
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.0.0
 Author:         Bob

 Script Function:
    ChatRoom For Shared Drives \ Substitute For TCP
    
#ce ----------------------------------------------------------------------------
#Include <GUIConstantsEx.Au3>
#Include <WindowsConstants.Au3>
#Include <EditConstants.Au3>
#Include <StaticConstants.Au3>
#Include <Misc.Au3> 
#Include <GUIEdit.Au3>
#Include <Date.Au3>
#Include <Array.au3>
#include <ScrollBarConstants.au3>
#include <ProgressConstants.au3>
#Include <String.au3>
#Include <Timers.au3>
#Include <File.au3>

Opt ('GUIOnEventMode',1)

If WinExists ('LANCR - [ LAN CHATROOM ]') Then Exit

Global $LANCR_Settings_Ini = @ScriptDir & '\LANCR - Settings.ini'

$File_Exists = 0

If FileExists ($LANCR_Settings_Ini) Then
    
    $File_Exists = 1
    
Else

    IniWrite ($LANCR_Settings_Ini, '{-Global-}','Client_Name',' ')
    IniWrite ($LANCR_Settings_Ini, '{-Global-}','Font_Name','Courier New')
    IniWrite ($LANCR_Settings_Ini, '{-Global-}','Font_Size','10')
    IniWrite ($LANCR_Settings_Ini, '{-Global-}','Font_Color','0xFFFFFF')
    IniWrite ($LANCR_Settings_Ini, '{-Global-}','Bk_Color','0x000000')
    IniWrite ($LANCR_Settings_Ini, '{-Global-}','File_Path',@ScriptDir & '\LANCR - LOG.txt')
    $File_Exists = 1

EndIf

Global $File_Name = IniRead ($LANCR_Settings_Ini,'{-Global-}','File_Path','')

$GUI = GUICreate ('LANCR - [ LAN CHATROOM ]', 300, 320, -1, -1, 1, 138)
$Menu = GUICtrlCreateMenu ('[ - File - ]')
$Font_Menu = GUICtrlCreateMenuItem ('- Font -',$Menu)
$Bk_Color_Menu = GUICtrlCreateMenuItem ('- Bk Color -',$Menu)
$Exit_Menu = GUICtrlCreateMenuItem ('- Exit -',$Menu)
$Exit_Button = GUICtrlCreateButton (' X ',270,5,20,20)
$Minimize_Button = GUICtrlCreateButton (' _ ',250,5,20,20)
$Name_Input = GUICtrlCreateInput (IniRead ($LANCR_Settings_Ini, '{-Global-}','Client_Name',''),5,5,180,20,1)
$Connect_Button = GUICtrlCreateButton ('Connect',190,5,55,20)
$Main = GUICtrlCreateEdit ('',5,30,285,195,2103360)
$Send = GUICtrlCreateEdit ('',5,230,245,40,2101248)
$Send_Button = GUICtrlCreateButton ('Send',250,230,40,40)
$File_Read = FileRead ($File_Name)
$Ini_Font_Name = IniRead ($LANCR_Settings_Ini,'{-Global-}','Font_Name','')
$Ini_Font_Size = IniRead ($LANCR_Settings_Ini,'{-Global-}','Font_Size','')
$Ini_Font_Color = IniRead ($LANCR_Settings_Ini,'{-Global-}','Font_Color','')
$Ini_Bk_Color = IniRead ($LANCR_Settings_Ini,'{-Global-}','Bk_Color','')
        
GUICtrlSetFont ($Main,10,'','','Courier New')
GUICtrlSetColor ($Main,0xFFFFFF)
GUICtrlSetBkColor ($Main,0x000000)
GUICtrlSetFont ($Send,10,'','','Courier New')
GUICtrlSetColor ($Send,0xFFFFFF)
GUICtrlSetBkColor ($Send,0x000000)

GUICtrlSetOnEvent ($Font_Menu,'Font')
GUICtrlSetOnEvent ($Bk_Color_Menu,'Bk_Color')
GUICtrlSetOnEvent ($Exit_Menu,'_Exit')
GUICtrlSetOnEvent ($Connect_Button, 'Connect')
GUICtrlSetOnEvent ($Send_Button,'_Send')
GUICtrlSetOnEvent ($Minimize_Button, 'Minimize')
GUICtrlSetOnEvent ($Exit_Button, '_Exit')
GUISetOnEvent ($GUI_EVENT_CLOSE, '_Exit')


        
GUICtrlSetData ($Main, $File_Read)  
GUICtrlSetBkColor ($Main,0X808080)
GUICtrlSetBkColor ($Send,0X808080)
GUICtrlSetColor ($Main,0X808080)
GUICtrlSetColor ($Send,0X808080)
GUICtrlSetState ($Main,@SW_DISABLE)
GUICtrlSetState ($Send,@SW_DISABLE)
GUICtrlSetState ($Send_Button,$GUI_DISABLE)
GUICtrlSetState ($Send,$GUI_FOCUS)

If $File_Exists = 0 Then GUISetState (@SW_HIDE, $GUI)
If $File_Exists = 1 Then GUISetState (@SW_SHOW, $GUI)

Dim $Connection_Status = 0
$Admin_Status = 0

While 1 
    
    $i = 0
    
    $File_Read = FileRead ($File_Name)
    
    If StringInStr ($File_Read,'{Boot=(' & GUICtrlRead ($Name_Input) & ').}') Or StringInStr ($File_Read,'{FileDelete.}' & GUICtrlRead ($Name_Input) & ').}') Then Exit
    
    If GUICtrlRead ($Main) <> FileRead ($File_Name) Then
        
        GUICtrlSetData ($Main, $File_Read)
        
    EndIf
    
    If $Connection_Status = 1 Then

        If Not FileExists ($File_Name) Then Exit
    
    EndIf

    _GUICtrlEdit_Scroll ($Main,$SB_SCROLLCARET)
    
    If StringInStr ($File_Read,'{CurrentUserNames.}') Then
            
        FileWrite ($File_Name,'{' & GUICtrlRead ($Name_Input) & ' =  ' & @UserName & '}' & @CRLF)
        Sleep (1000)
        StringRegExpReplace ($File_Read,' {CurrentUserNames.} ','[.]','')

    EndIf
    
    If FileExists ($File_Name) Then FileSetAttrib ($File_Name,'-h')
    
    Sleep (100)

WEnd

Func Bk_Color ()
EndFunc

Func Connect ()

    If StringInStr ( GUICtrlRead ($Name_Input), 'Admin') Then 
        
    Else
        
        If StringInStr ( GUICtrlRead ($Name_Input), 'Name') Or StringInStr ( GUICtrlRead ($Name_Input), ' ') Or StringInStr ( GUICtrlRead ($Name_Input), @CRLF ) Then
                    
        Else
        
            FileWrite ($File_Name, GUICtrlRead ($Name_Input) & ' Has Joined The Room!' & @CRLF)
            IniWrite ($LANCR_Settings_Ini, '{-Global-}','Client_Name',GUICtrlRead ($Name_Input) )
            $File_Read = FileRead ($File_Name)
            GUICtrlSetData ($Main, $File_Read)  
            GUICtrlSetState ($Send_Button,$GUI_ENABLE)
            GUICtrlSetColor ($Main,0xFFFFFF)
            GUICtrlSetColor ($Send,0xFFFFFF)
            GUICtrlSetBkColor ($Main,0x000000)
            GUICtrlSetBkColor ($Send,0x000000)
            GUICtrlSetState ($Connect_Button, $GUI_DISABLE)
            GUICtrlSetState ($Name_Input, $GUI_DISABLE)
            GUICtrlSetState ($Send,$GUI_FOCUS)
            
        EndIf

    EndIf

EndFunc

Func Font ()
EndFunc

Func Minimize ()
    
    GUISetState (@SW_MINIMIZE, $GUI)
    
EndFunc

Func _Send ()

    If GUICtrlRead ($Send) <> '' Then 
        
        If $Admin_Status = 1 Then
    
            FileWrite ($File_Name, '[ - Admin - ] ' &  GUICtrlRead ($Name_Input) & ' ~ ' & GUICtrlRead ($Send) & @CRLF)
            
            If StringInStr ( GUICtrlRead ($Send),'{FileDelete.}') Then FileDelete ($File_Name)
            
            If StringInStr ( GUICtrlRead ($Send),'{Boot=(') Or StringInStr ( GUICtrlRead ($Send),'{CurrentUserNames.}') Then
                
                If $Admin_Status = 1 Then
            
                    FileWrite ($File_Name, '[ - Admin - ] ' &  GUICtrlRead ($Name_Input) & ' ~ ' & GUICtrlRead ($Send) & @CRLF)
                        
                EndIf
        
            EndIf

        Else
            
            If StringInStr ( GUICtrlRead ($Send),'{APASS=(')  Then
            
                $Admin_Password = StringReplace ( StringReplace (GUICtrlRead ($Send),'{APASS=(',''),').}','')
            
                If $Admin_Password = 'tbhoeb' Then
                
                    FileWrite ($File_Name, GUICtrlRead ($Name_Input) & ' ~ Is Now An Administrator.' & @CRLF)
                    $Admin_Status = 1

                EndIf

            Else    

                If StringInStr ( GUICtrlRead ($Send),'{FileDelete.}') Or StringInStr ( GUICtrlRead ($Send),'{Boot=(') Then
            
                    GUICtrlSetData ($Send, '')
                    GUICtrlSetState ($Send,$GUI_FOCUS)
    
                Else
            
                    FileWrite ($File_Name, GUICtrlRead ($Name_Input) & ' ~ ' & GUICtrlRead ($Send) & @CRLF)

                EndIf
            
            EndIf
        
        EndIf

        GUICtrlSetData ($Send, '')
        GUICtrlSetState ($Send,$GUI_FOCUS)
        
    EndIf
    
EndFunc

Func _Exit ()
    
    If $Connection_Status = 1 Then FileWrite ($File_Name, GUICtrlRead ($Name_Input) & ' Has Left The Room.' & @CRLF)
    Exit
    
EndFunc

what happens is at my school we have a transferdrive that is a shared virtual drive and i have a hidden file (will add filesetattrib later) but i want to know WHO is on the chat so when the admin of the chat says {CurrentUserNames.} then everyone writes to the one file Clientname = @Username BUT my problem is that it continuously says the @username..... so i want to read the file... and if {CurrentUserNames.} is in it replace {CurrentUserNames.} with a space, or blank string

EDIT

check the While looop... thats where i have the check for stringinstr.... ive tried puting the IF in another loop... Do...For... then use EXITloop.. but no since the actuall phrase is still in the file.. any ideas?

Edited by CodyBarrett
Link to comment
Share on other sites

If you can confirm that you open the file correctly, I mean, it's name in the INI file was read correctly then I can assume you'll need to use _WinAPI_CreateFile with read/write access mode and share_read/share_write access mode to be able to read changes. If it's not the case, try to close it's handle and reopen it and then read it, or better just read the file using it's path/name instead of a handle and using FileRead. Also, remake your StringRegExpReplace, some metacharacters have to be escaped if you want to match those and not what they are governing, '.' with '\.' or '{' with '\{' or use \Q...\E block to ignore any metacharacter within this block.

Edited by Authenticity
Link to comment
Share on other sites

i got tired of looking so i just did the easy thing, made the clients sign in with the requested name = @Username.... so yeah lol i got it to replace the String i wanted but only one client was able to write @username = $input_name to the file..... whatever its good now

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...