Upload and generate a text file

New to Gambas? Post your questions here. No question is too silly or too simple.
Post Reply
User avatar
gambafeliz
Posts: 141
Joined: Friday 2nd September 2022 7:50pm
Location: I live in the same city as Picasso

Upload and generate a text file

Post 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.
For your misfortunes I am Spanish and I only know Spanish, please, be patient with me, Thank you. :)
vuott
Posts: 263
Joined: Wednesday 5th April 2017 6:07pm
Location: European Union

Re: Upload and generate a text file

Post by vuott »

...perhaps it would be appropriate for you to show those codes that generate errors: an attempt could be made to resolve them.
Europaeus sum !

Amare memorentes atque deflentes ad mortem silenter labimur.
User avatar
BruceSteers
Posts: 1578
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Upload and generate a text file

Post 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

If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1578
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Upload and generate a text file

Post 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 :)
If at first you don't succeed , try doing something differently.
BruceS
User avatar
gambafeliz
Posts: 141
Joined: Friday 2nd September 2022 7:50pm
Location: I live in the same city as Picasso

Re: Upload and generate a text file

Post 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.
For your misfortunes I am Spanish and I only know Spanish, please, be patient with me, Thank you. :)
User avatar
BruceSteers
Posts: 1578
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Upload and generate a text file

Post 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 :)
If at first you don't succeed , try doing something differently.
BruceS
Online
User avatar
cogier
Site Admin
Posts: 1127
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Upload and generate a text file

Post 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
User avatar
gambafeliz
Posts: 141
Joined: Friday 2nd September 2022 7:50pm
Location: I live in the same city as Picasso

Re: Upload and generate a text file

Post by gambafeliz »

Thanks cogier

I'll look at it to see if it serves as a guide for my file opening.
For your misfortunes I am Spanish and I only know Spanish, please, be patient with me, Thank you. :)
User avatar
BruceSteers
Posts: 1578
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Upload and generate a text file

Post 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-)
If at first you don't succeed , try doing something differently.
BruceS
Post Reply