Page 1 of 1

Upload and generate a text file

Posted: Tuesday 21st November 2023 8:00am
by gambafeliz
Hello

I want to do the following:
1. Let the user search the disk like a file explorer. But you can only search for the txt extension.
2. Once found, Gambas should be able to use the.txt file
3. Another option is that instead of searching for it, what I want is for it to create the.txt file and it will create it from a copy that I have in the program path but with a different name. It will then copy the file.xxx and put it in the user's chosen path and rename it from file.xxx to file.txt

More or less explained, what do you suggest I do? Until now what I have done has not worked very well for me, and I always get errors or let's say the user detects errors that I cannot control.

Maybe I don't use the controls properly or I don't know.

Re: Upload and generate a text file

Posted: Tuesday 21st November 2023 11:24am
by vuott
...perhaps it would be appropriate for you to show those codes that generate errors: an attempt could be made to resolve them.

Re: Upload and generate a text file

Posted: Tuesday 21st November 2023 2:13pm
by BruceSteers
Make a folder in project source dir, for example call it Templates
place your template text file in there.


Dim sTargetFile As String = User.Home &/ "new-file.txt"
If not Exist(sTargetFile) Then Copy "./Templates/MyTemplate.txt" To sTargetFile




To search for .txt extension files just use Dir with a pattern *.txt

Dim aResults As String[]

aResults = ListTextFiles(User.Home)

Public Sub ListTextFiles(Path As String) As String[]

 Return Dir(Path, "*.txt", gb.file)

End




To search for text/plain mimetype files use DesktopMime.class from gb.desktop


Dim aResults As String[]
aResults = ListTextMimeFiles(User.Home)

Public Sub ListTextMimeFiles(Path As String) As String[]

  Dim aTextFiles As New String[]

  Dim aFiles As String[] = Dir(Path, "*", gb.File)

  For Each sFile As String In aFiles
    If DesktopMime.FromFile(Path &/ sFile).Type = "text/plain" Then aTextFiles.Add(sFile)
   Next

  Return aTextFiles

End


Re: Upload and generate a text file

Posted: Tuesday 21st November 2023 2:18pm
by BruceSteers
gambafeliz wrote: Tuesday 21st November 2023 8:00am More or less explained, what do you suggest I do? Until now what I have done has not worked very well for me, and I always get errors or let's say the user detects errors that I cannot control.

Maybe I don't use the controls properly or I don't know.
Hehe , more or less lol :)

post example code of what you are having trouble with dude.
our native tongues are different and explanations can be hard to understand sometimes.
We all speak gambas though ;)
that question would have been much easier to understand exactly what you want to do if you had posted the code too :)

Re: Upload and generate a text file

Posted: Tuesday 21st November 2023 4:21pm
by gambafeliz
My fault. The thing is, maybe I haven't been able to explain myself and in the end I don't really know what to ask of you.

I tell you, but I don't see how to put the code.

I read a configuration file and see if the path of the .txt file is there, if it does not exist, I show a form for the user to search for it among the files or create a new one. When creating it, I only ask them to give it a name and I will do it. I create from one existing among my installation files of my program. I also save in the configuration file that this new or searched is the favorite for the user, so that it loads it by default.

I have done all this but in a rudimentary way or with a personalized form.

My question is more about: is it possible to create it with components already created in gambas?

And if not, someone will send me a similar but optimized form. The thing is that mine is currently chaotic and is not going as well as I would like. To say that I don't put it in because it refers to the configuration and routes file that is of no use to anyone. Besides, I don't want to do it because it is poorly developed.

Finally, I understand that I cannot be helped in this case.

And please I apologize for the bad question. Thank you anyway.

Re: Upload and generate a text file

Posted: Tuesday 21st November 2023 5:09pm
by BruceSteers
er no i do not think there is a component for that.

You'll have to code it yourself.
it shouldn't be too hard.
That's the learning process,
I am sure you will figure out and refine an okay way to do it :)
Practice makes perfect :)

Re: Upload and generate a text file

Posted: Wednesday 22nd November 2023 3:55pm
by cogier
1. Let the user search the disk like a file explorer. But you can only search for the txt extension.
2. Once found, Gambas should be able to use the.txt file
Try the following code. 1/. It lists all the .txt files is the Home folder and its sub folders. 2/. If you double-click on a file, it will open it in a TextArea.

Am I going in the correct direction?
GridViewList As GridView
TextAreaEdit As TextArea
sFiles As String[]
sLang As String[]

Public Sub Form_Open()
  
  Setup
  GetFiles
  
End

Public Sub GetFiles()
  
  Dim iRow As Integer 
  
  sFiles = RDir(User.Home, "*.txt", gb.File).Sort()
  
  For iRow = 0 To sFiles.Max
    If sFiles[iRow] Begins "." Then Continue ''Don't show hidden files (No mostrar archivos ocultos)
    Inc GridViewList.Rows.Count
    GridViewList[GridViewList.Rows.Max, 0].Text = User.Home &/ sFiles[iRow]
  Next
  
  Me.Title = sLang[0] & "(" & Str(GridViewList.Rows.Count) & ")"
  
End

Public Sub GridViewList_Click()
  
  Dim iLast As Integer = Last.Row
  Dim iRow, iColour As Integer 
  Dim bBold As Boolean
  
  For iRow = 0 To GridViewList.Rows.Max
    iColour = Color.Default 
    bBold = False
    If iRow = iLast Then 
      iColour = Color.LightGray
      bBold = True
    End If
    GridViewList[iRow, 0].Background = iColour
    GridViewList[iRow, 0].Font.Bold = bBold
  Next
  
End

Public Sub GridViewList_DblClick()
  
  GridViewList.Visible = False
  TextAreaEdit.Visible = True
  
  TextAreaEdit.Text = File.Load(GridViewList[Last.Row, 0].Text)
  Me.Title = GridViewList[Last.Row, 0].Text
  
End

Public Sub Setup()
  
  If System.Language Begins "en" Then sLang = ["Text file list", "Double click to open"]
  If System.Language Begins "es" Then sLang = ["Lista de archivos de texto", "Doble clic para abrir"]
  
  With Me
    .Arrangement = Arrange.Vertical
    .Padding = 5
    .Height = 550
    .Width = 850
  End With
  
  With GridViewList = New GridView(Me) As "GridViewList"
    .Columns.Count = 1
    .Rows.Count = 0
    .ScrollBar = GridView.Vertical
    .Grid = False
    .Expand = True
    .Header = GridView.Vertical
    .Tooltip = sLang[1]
    .Visible = True
  End With
  
  With TextAreaEdit = New TextArea(Me)
    .Expand = True
    .Wrap = True
    .Visible = False
  End With
  
End

Re: Upload and generate a text file

Posted: Wednesday 22nd November 2023 4:04pm
by gambafeliz
Thanks cogier

I'll look at it to see if it serves as a guide for my file opening.

Re: Upload and generate a text file

Posted: Wednesday 22nd November 2023 4:51pm
by BruceSteers
cogier wrote: Wednesday 22nd November 2023 3:55pm

  sFiles = RDir(User.Home, "*.txt", gb.File).Sort()
  
Wow , after all these years of making many a recursive Dir function I have just learned there is an RDir command :shock: :D

Cool 8-)