Jump to content

If text string is same then replace it else add new line


Recommended Posts

This is code to write Current Date in File From internet ( I got It from another help :) )

$hFO4 = FileOpen(@ScriptDir & "/DaysArray.txt", 2)
$aResult = GetDateFromINet()

If @error Then
    ConsoleWrite("Error connecting to internet!" & @CRLF)
Else
FileWrite($hFO4,"For Date : " &$aResult[0]&" "&$aResult[1]&" "&$aResult[2]&@CRLF)
FileClose($hFO4)
  ;_ArrayDisplay($aResult)
EndIf

Any way I want for example :

DaysArray.txt=2016 10 18

Read aResult

If $aResult = 2016 10 18 Then

msgbox(0,"","Don't Do Anything")

else

msgbox(0,"","Ok Write New Date in New Line ")

Endif

Link to comment
Share on other sites

14 minutes ago, Muhammad_Awais_Sharif said:

I don't have problem with getting Date  My problem is only when next day come program replace old date .
I want program to :

1- Check Txt File IF it's contains Date of my day

2- In next Day Add the next day Date to same Txt file but in another line .

For Example :

18-10  Is my Day

19-10 Is next Day

1-  If Txt File Has "18-10" Then   Add 19-10 in next line

2- If not Then add My day (18-10 )

Link to comment
Share on other sites

I'd be crazy :( ..... this is another way but failed to read Date Variable from Txt File Same of All another Ways :

#include <File.au3>
#include <Array.au3>
$Calcok=_GetTimeOnline(0)
Global $file = @ScriptDir&"\DaysArray.txt", $search = $Calcok
Global $iLine = 0, $sLine = '', $iValid = 0
Global $hFile = FileOpen($file)
If $hFile = -1 Then 
    MsgBox(0,'ERROR','Unable to open file for reading.')
    Exit 1
EndIf

; find the line that has the search string
While 1
    $iLine += 1
    $sLine = FileReadLine($hFile)
    If @error = -1 Then ExitLoop

    ; test the line for the $search string until the flag $iValid is set
    If StringInStr($sLine, $search) And Not $iValid Then
        $iValid = 1
        ContinueLoop
    EndIf

    If $iValid Then
        $iValid += 1
    MsgBox(0,"Founded","Founded")
    FileWrite($file, @CRLF)
        ConsoleWrite($iLine & ':' & $sLine & @CRLF)
        If $iValid > 5 Then ExitLoop
    EndIf
If $iValid=0 Then 
    MsgBox(0,"NotFound","Sorry")
    FileWrite($file,$Calcok )
    FileClose($file)
    ExitLoop
EndIf
WEnd

; ===============================================================================================================================
Func _GetTimeOnline($iTimeZone)
    Local $aTimeZone[7] = ['utc', 'est', 'cst', 'mst', 'pst', 'akst', 'hast']

    Local $sRead = BinaryToString(InetRead('http://www.timeapi.org/' & $aTimeZone[$iTimeZone] & '/now?format=\Y/\m/\d'))
;DateWithTime   ;Local $sRead = BinaryToString(InetRead('http://www.timeapi.org/' & $aTimeZone[$iTimeZone] & '/now?format=\Y/\m/\d%20\H:\M:\S'))

    If @error Then
        Return SetError(1, 0, @YEAR & '/' & @MON & '/' & @MDAY & ' ' & @HOUR & ':' & @MIN & ':' & @SEC)
    EndIf
    Return $sRead
EndFunc   ;==>_GetTimeOnline

 

Link to comment
Share on other sites

#include <File.au3>
#include <Array.au3>
#include <Date.au3>
$Calcok=_GetTimeOnline(0)
Global $file = @ScriptDir&"\DaysArray.txt"

For $i = 1 To _FileCountLines($file)+1
    $line = FileReadLine($file,$i)
    $status = StringInStr($line,$Calcok)
    If Not $status = 0 Then
        MsgBox(0,"","yes found at line " & $i)
    EndIf
Next


; ===============================================================================================================================
Func _GetTimeOnline($iTimeZone)
    Local $aTimeZone[7] = ['utc', 'est', 'cst', 'mst', 'pst', 'akst', 'hast']

    Local $sRead = BinaryToString(InetRead('http://www.timeapi.org/' & $aTimeZone[$iTimeZone] & '/now?format=\Y/\m/\d'))
;DateWithTime    ;Local $sRead = BinaryToString(InetRead('http://www.timeapi.org/' & $aTimeZone[$iTimeZone] & '/now?format=\Y/\m/\d%20\H:\M:\S'))

    If @error Then
        Return SetError(1, 0, @YEAR & '/' & @MON & '/' & @MDAY & ' ' & @HOUR & ':' & @MIN & ':' & @SEC)
    EndIf
    Return $sRead
EndFunc   ;==>_GetTimeOnline

This will find if date from website is in file :P 

text file which i test on 
 

2016/10/20
2016/10/21
2016/10/23

 

 

Link to comment
Share on other sites

3 minutes ago, Muhammad_Awais_Sharif said:
#include <File.au3>
#include <Array.au3>
#include <Date.au3>
$Calcok=_GetTimeOnline(0)
Global $file = @ScriptDir&"\DaysArray.txt"

For $i = 1 To _FileCountLines($file)+1
    $line = FileReadLine($file,$i)
    $status = StringInStr($line,$Calcok)
    If Not $status = 0 Then
        MsgBox(0,"","yes found at line " & $i)
    EndIf
Next


; ===============================================================================================================================
Func _GetTimeOnline($iTimeZone)
    Local $aTimeZone[7] = ['utc', 'est', 'cst', 'mst', 'pst', 'akst', 'hast']

    Local $sRead = BinaryToString(InetRead('http://www.timeapi.org/' & $aTimeZone[$iTimeZone] & '/now?format=\Y/\m/\d'))
;DateWithTime    ;Local $sRead = BinaryToString(InetRead('http://www.timeapi.org/' & $aTimeZone[$iTimeZone] & '/now?format=\Y/\m/\d%20\H:\M:\S'))

    If @error Then
        Return SetError(1, 0, @YEAR & '/' & @MON & '/' & @MDAY & ' ' & @HOUR & ':' & @MIN & ':' & @SEC)
    EndIf
    Return $sRead
EndFunc   ;==>_GetTimeOnline

This will find if date from website is in file :P 

text file which i test on 
 

2016/10/20
2016/10/21
2016/10/23

 

 

OMG Finally You Did it Again Thank You very much 2 days trying to do this without any result 
Pls Can You explain To me this code :
If Not $status = 0 Then

Link to comment
Share on other sites

it is basically we are checking if string is found :P 

StringInStr will return 0 if it will not found substring in given string 
so not mean if  StringInStr success in finding substring then do this action 
I hope you got it :P 

it is same like c++ as we make condition if ( a != b )  

for you understanding treat it as

  If  StringInStr not failed in finding substring Then
        MsgBox(0,"","yes found at line " & $i)
    EndIf

Link to comment
Share on other sites

4 minutes ago, Muhammad_Awais_Sharif said:

it is basically we are checking if string is found :P 

StringInStr will return 0 if it will not found substring in given string 
so not mean if  StringInStr success in finding substring then do this action 
I hope you got it :P 

it is same like c++ as we make condition if ( a != b )  

for you understanding treat it as

  If  StringInStr not failed in finding substring Then
        MsgBox(0,"","yes found at line " & $i)
    EndIf

Ty I understood this , but how add "If StringInStr Failed Then" ?

I tried this

If $status = 0 Then
        MsgBox(0,"","Sorry Not Found")
    EndIf

Link to comment
Share on other sites

i think you are trying to determine after searching the whole file at last if we don't find any date same as we get from web then this will work

 

#include <File.au3>
#include <Array.au3>
#include <Date.au3>
$Calcok=_GetTimeOnline(0)
Global $file = @ScriptDir&"\DaysArray.txt"
Global $isFound = False
For $i = 1 To _FileCountLines($file)+1
    $line = FileReadLine($file,$i)
    $status = StringInStr($line,$Calcok)
    If Not $status = 0 Then
        MsgBox(0,"","yes found at line " & $i)
        $isFound = True
    EndIf
Next
If $isFound = False Then
    MsgBox(0,"Sorry","Date is not found")
EndIf


; ===============================================================================================================================
Func _GetTimeOnline($iTimeZone)
    Local $aTimeZone[7] = ['utc', 'est', 'cst', 'mst', 'pst', 'akst', 'hast']

    Local $sRead = BinaryToString(InetRead('http://www.timeapi.org/' & $aTimeZone[$iTimeZone] & '/now?format=\Y/\m/\d'))
;DateWithTime    ;Local $sRead = BinaryToString(InetRead('http://www.timeapi.org/' & $aTimeZone[$iTimeZone] & '/now?format=\Y/\m/\d%20\H:\M:\S'))

    If @error Then
        Return SetError(1, 0, @YEAR & '/' & @MON & '/' & @MDAY & ' ' & @HOUR & ':' & @MIN & ':' & @SEC)
    EndIf
    Return $sRead
EndFunc   ;==>_GetTimeOnline

 

Link to comment
Share on other sites

15 minutes ago, Muhammad_Awais_Sharif said:

i think you are trying to determine after searching the whole file at last if we don't find any date same as we get from web then this will work

 

#include <File.au3>
#include <Array.au3>
#include <Date.au3>
$Calcok=_GetTimeOnline(0)
Global $file = @ScriptDir&"\DaysArray.txt"
Global $isFound = False
For $i = 1 To _FileCountLines($file)+1
    $line = FileReadLine($file,$i)
    $status = StringInStr($line,$Calcok)
    If Not $status = 0 Then
        MsgBox(0,"","yes found at line " & $i)
        $isFound = True
    EndIf
Next
If $isFound = False Then
    MsgBox(0,"Sorry","Date is not found")
EndIf


; ===============================================================================================================================
Func _GetTimeOnline($iTimeZone)
    Local $aTimeZone[7] = ['utc', 'est', 'cst', 'mst', 'pst', 'akst', 'hast']

    Local $sRead = BinaryToString(InetRead('http://www.timeapi.org/' & $aTimeZone[$iTimeZone] & '/now?format=\Y/\m/\d'))
;DateWithTime    ;Local $sRead = BinaryToString(InetRead('http://www.timeapi.org/' & $aTimeZone[$iTimeZone] & '/now?format=\Y/\m/\d%20\H:\M:\S'))

    If @error Then
        Return SetError(1, 0, @YEAR & '/' & @MON & '/' & @MDAY & ' ' & @HOUR & ':' & @MIN & ':' & @SEC)
    EndIf
    Return $sRead
EndFunc   ;==>_GetTimeOnline

 

Thank You Again :)

Full Solved

Link to comment
Share on other sites

abdulrahmanok,

Another way to look at the problem, simplified.  Also includes writing the new date if not found.

#include <File.au3>
#include <Array.au3>
#include <Date.au3>

$Calcok = _GetTimeOnline(0)
Global $file = @ScriptDir & "\DaysArray.txt"

If StringRegExp(FileRead($file), $Calcok) Then
    MsgBox(0, 'Date on file', 'Date = ' & $Calcok, 5)
    Exit
Else
    MsgBox(0, 'Date NOT on file', 'Adding date = ' & $Calcok, 5)
    FileWrite($file, @CRLF & $Calcok)
EndIf

; ===============================================================================================================================
Func _GetTimeOnline($iTimeZone)
    Local $aTimeZone[7] = ['utc', 'est', 'cst', 'mst', 'pst', 'akst', 'hast']

    Local $sRead = BinaryToString(InetRead('http://www.timeapi.org/' & $aTimeZone[$iTimeZone] & '/now?format=\Y/\m/\d'))
    ;DateWithTime    ;Local $sRead = BinaryToString(InetRead('http://www.timeapi.org/' & $aTimeZone[$iTimeZone] & '/now?format=\Y/\m/\d%20\H:\M:\S'))

    If @error Then
        Return SetError(1, 0, @YEAR & '/' & @MON & '/' & @MDAY & ' ' & @HOUR & ':' & @MIN & ':' & @SEC)
    EndIf
    Return $sRead
EndFunc   ;==>_GetTimeOnline

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

36 minutes ago, kylomas said:

abdulrahmanok,

Another way to look at the problem, simplified.  Also includes writing the new date if not found.

#include <File.au3>
#include <Array.au3>
#include <Date.au3>

$Calcok = _GetTimeOnline(0)
Global $file = @ScriptDir & "\DaysArray.txt"

If StringRegExp(FileRead($file), $Calcok) Then
    MsgBox(0, 'Date on file', 'Date = ' & $Calcok, 5)
    Exit
Else
    MsgBox(0, 'Date NOT on file', 'Adding date = ' & $Calcok, 5)
    FileWrite($file, @CRLF & $Calcok)
EndIf

; ===============================================================================================================================
Func _GetTimeOnline($iTimeZone)
    Local $aTimeZone[7] = ['utc', 'est', 'cst', 'mst', 'pst', 'akst', 'hast']

    Local $sRead = BinaryToString(InetRead('http://www.timeapi.org/' & $aTimeZone[$iTimeZone] & '/now?format=\Y/\m/\d'))
    ;DateWithTime    ;Local $sRead = BinaryToString(InetRead('http://www.timeapi.org/' & $aTimeZone[$iTimeZone] & '/now?format=\Y/\m/\d%20\H:\M:\S'))

    If @error Then
        Return SetError(1, 0, @YEAR & '/' & @MON & '/' & @MDAY & ' ' & @HOUR & ':' & @MIN & ':' & @SEC)
    EndIf
    Return $sRead
EndFunc   ;==>_GetTimeOnline

kylomas

Thank You Works perfectly :drool:

Nice Small And Useful Code

Edited by abdulrahmanok
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...