Jump to content

Dont write if line exists


Recommended Posts

$path = @ScriptDir & "\default.txt"

Case $Button1
$fod = FileOpenDialog("Select File", @ScriptDir, "Default Files(*.txt;*.cfg;*.dat;*.ini)|Text Files(*.txt)|Configuration Files(*.cfg)|Data Files(*.dat)|Windows Initialization Files(*.ini)|All Files(*.*)")
If @error Then
MsgBox(0, "Commander", "No file selected")
Else
$fo3 = FileOpen($path)
If $fo3 = -1 Then FileWrite($path, $fod & @CRLF)
$line2 = _FileCountLines($path)
For $i2 = 1 To $line2
$rd2 = FileReadLine($fo3, $i2)
If $rd2 == $fod Then
MsgBox(0, "Commander", "This file exists")
Else
FileWriteLine($path, $fod)
GUICtrlSetData($List1, $fod)
EndIf
If $i2 == $line2 Then ExitLoop
Next
EndIf

i need to write the path of the selected files only if the path doesent exist

right now its writeing the same line 2 times and gives File exists error 2 times

Edited by HeavenlyDemon
Link to comment
Share on other sites

HeavenlyDeamon,

You will find it easier to get help in the future if you post RUNNABLE code. What you did post has several logical errors as well.

The following is ONE way that this can be done.

; *** Start added by AutoIt3Wrapper ***

#include <Constants.au3>
#include <GUIConstantsEx.au3>
#include <file.au3>

#AutoIt3Wrapper_Add_Constants=n

; read the contents of control file to a string variable
local $path = @ScriptDir & "\default.txt"
local $file_contents = fileread($path)

; your gui might look something like the following
local $gui010   =   guicreate('TEST')
local $aGuisize =   wingetclientsize($gui010)
local $list1    =   guictrlcreatelist('',10,10,$aGuisize[0]-20,$aGuisize[1]-50)
local $Button1  =   guictrlcreatebutton('My Test Button',10,$aGuisize[1]-30,$aGuisize[0]-20,20)
                    guisetstate()
while 1
    switch guigetmsg()
        case $gui_event_close
            ; drive this routine at exit to write the new control file
            _Exit()
        case $Button1
            $fod = FileOpenDialog("Select File", @ScriptDir, "Default Files(*.txt;*.cfg;*.dat;*.ini)|Text Files(*.txt)|Configuration Files(*.cfg)|Data Files(*.dat)|Windows Initialization Files(*.ini)|All Files(*.*)")
            If @error Then
                MsgBox(0, "Commander", "No file selected")
            Else
                ; search the string variable for the selected file...see help doc for "stringinstr"
                if StringInStr($file_contents,$fod) > 0 then
                    msgbox($mb_ok,'Yoda','FILE Exists' & @lf & 'File = ' & $fod)
                Else
                    $file_contents &= $fod & @CRLF
                    guictrlsetdata($list1,$fod)
                endif
            endif
    endswitch
wend

func _exit()

    ; delete the existing control file
    filedelete($path)
    ; write a nw control file
    filewrite($path,$file_contents)
    ; use existing program association to open control file
    shellexecute($path)
    Exit

endfunc

It might also be advantageous for you to post a brief, concise description of what you are trying to accomplish. The heavy hitters on this forum have pretty much seen it all and may suggest a better/different approach.

Good Luck,

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

HeavenlyDeamon,

You will find it easier to get help in the future if you post RUNNABLE code.

It might also be advantageous for you to post a brief, concise description of what you are trying to accomplish. The heavy hitters on this forum have pretty much seen it all and may suggest a better/different approach.

Il remember that and thank you for the help

The script works great

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...