Jump to content
Sign in to follow this  
Karl1983

_FileWriteToLine

Recommended Posts

Karl1983

Hello. Noob alert! I am willing to learn and write my own code so apologies if I am plain stupid. I will try not to be one of those who supplies little information as I see replies to suggest that it really annoys you.

So Maybe some background before I issue the problem statement. 

I have a simple GUI that allows you to enter a location and destination for a folder copy and to elect to override files or not (why write such a programme when software already out there? Well because I want to, its fun to learn)

So anyway, what I do is I create a file at the start to pre-populate the fields with a destination and source (if it doesnt already exist)

Func _FilePathCall()
$file = FileOpen(@ScriptDir & "\filepath.csv", 0)

If $file = -1 Then
   FileWrite(@scriptdir & "\filepath.csv", "C:\" & @CRLF )
   FileWrite(@scriptdir & "\filepath.csv", "C:\")
   FileClose($file)
   MsgBox(0,"Notification","Done")
EndIf
EndFunc

This works fine (and hopefully isn't causing my issue later on)

Later I call this file to set the input boxes

Func _SetSrcFromFile()
   $file = FileOpen(@ScriptDir & "\filepath.csv", 0)
   $src1 = FileReadLine($file,1)
   FileClose($file)
   GUICtrlSetData($srcinput,$src1)    
EndFunc

But I also have a button to allow people to select a new source and so as part of that process I want it to write back to the file so that this becomes the new default source location (like a memory if you like). Now I am struggling with this function. I cant seem to get it to work. I will post to variations I have tried, but neither seem to update the file in question.

Version 1;

Func _SetNewSrc()
   $src2 = FileSelectFolder("Please Select Backup SOURCE","") ;user prompt to select a folder
   _FileWriteToLine($file, 1, $src2, 1)
EndFunc

So as you can hopefully see in this version I simply just use the function to write to line of  file without using fileopen...this could be wrong but I tried this incase the fileopen was not needed. I am attempting to replace the text already saved in line 1

Version 2 - Exactly the same except using FileOpen

Func _SetNewSrc()
   $file = FileOpen(@ScriptDir & "\filepath.csv", 1)
   $src2 = FileSelectFolder("Please Select Backup SOURCE","") ;user prompt to select a folder
   _FileWriteToLine($file, 1, $src2, 1)
   FileClose($file)
EndFunc

I set it to allow write access in the FileOpen and even tried deleting the data in that process but the _FileWriteToLine still does not work. And actually...deleting the data is not what I want anyway as line 2 contains destination data which I would want to keep and update as part of another similar function.

Can anyone help?

I can post full code if you like but it is Nooblike and rather scary and probably uses 10x the amount of code needed ;)

Look forward to some help. And of course I have looked up this elsewhere...thats where I found to use the _FileWriteToLine in the first place but it wasnt on a post that was related closely enough

 


Regards

Karl Griffiths

Share this post


Link to post
Share on other sites
ajag

Hi Kalrl1983,

try

Func _SetNewSrc()
    $src2 = FileSelectFolder("Please Select Backup SOURCE","") ;user prompt to select a folder    
    _FileWriteToLine(@ScriptDir & "\filepath.csv", 1, $src2, 1)
EndFunc

It seems that _FileWriteToLine() does not accept a filehandle, so give the path-string to it.

A-Jay

Edited by ajag

Rule #1: Always do a backup         Rule #2: Always do a backup (backup of rule #1)

Share this post


Link to post
Share on other sites
Karl1983

Hi A-Jay

Spot on. Thank you for responding so quickly! I have run this and it now updates correctly. Many thanks :)

JohnOne - noted re using error handling to assist in debugging

Thanks again


Regards

Karl Griffiths

Share this post


Link to post
Share on other sites
Karl1983

Thanks Universalist.

I have taken on board the suggestions and also amended the code so the function can be called for each input and relevant line in csv to call

Thanks again all for your help.

;Setting a new default input(done everytime the Dir location is amended)
Func _SetNewDir($inputname, $csvLine)
   $tmp = FileSelectFolder("Please Select Backup Source","") ;user prompt to select a folder
    If @error Then Return SetError(1, 0, 'Error')
   _FileWriteToLine(@ScriptDir & "\filepath.csv", $csvLine, $tmp, 1) ;Writes the new location to the filepath csv used in 'Func _FilePathCall()'
   $file = FileOpen(@ScriptDir & "\filepath.csv", 0) ;Opens file with read access
   $DirSelect = FileReadLine($file,$csvLine) ;Reads the new location that has been set
   FileClose($file)
   GUICtrlSetData($inputname,$DirSelect) ;Sets the input box to reflect the new location
EndFunc ;==>_SetNewDir
Edited by Karl1983

Regards

Karl Griffiths

Share this post


Link to post
Share on other sites
Karl1983

Link back to full script. A work in progress:- 


Regards

Karl Griffiths

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  

×