List only the files without extensions

Post your Gambas programming questions here.
Post Reply
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

List only the files without extensions

Post by cogier »

If I run the code below I will get a list of all the files ending in '.png' in my Home directory. Is there a way to list ONLY the files with no extension? :?

Public Sub Main()
Dim sList As String[]
Dim sTemp As String
Dim sPattern As String = "*.png"

sList = Dir(User.home, sPattern, gb.file)

For Each sTemp In sList
  Print sTemp
Next

End
User avatar
stevedee
Posts: 518
Joined: Monday 20th March 2017 6:06pm

Re: List only the files without extensions

Post by stevedee »

This will do it:-

Code: Select all

Public Sub Main()
Dim sList As String[]
Dim sTemp As String
Dim sPattern As String = "*"
 
sList = Dir(User.home, sPattern, gb.file)
 
For Each sTemp In sList
  If InStr(sTemp, ".") = 0 Then
    Print sTemp
  Endif
Next
 
End
...but you probably want a solution using sPattern
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: List only the files without extensions

Post by cogier »

Thanks SteveDee,

I too came up with this solution but I wondered if there was an easier way. There is still a problem with this method as a file like .text which does not have an extension will fail to be displayed so more code is needed.

By the way have a look here regarding putting Gambas code on the forum. viewtopic.php?f=9&t=502
User avatar
stevedee
Posts: 518
Joined: Monday 20th March 2017 6:06pm

Re: List only the files without extensions

Post by stevedee »

OK, try adding the Start argument:-
  If InStr(sTemp, ".", 2) = 0 Then
User avatar
jornmo
Site Admin
Posts: 224
Joined: Wednesday 21st September 2016 1:19pm
Location: Norway

Re: List only the files without extensions

Post by jornmo »

I really do not get the LIKE syntax. It would be nice if there was a flag that could be set that would allow one to use RegEx. To me something like this "*[^.]*" should have worked, but it doesn't...

I'm asking on the new mailing list at list.gambas-basic.org

http://lists.gambas-basic.org/pipermail ... 00033.html
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: List only the files without extensions

Post by cogier »

Thanks SteveDee, good point.

Jornmo you created a flurry of emails about this. I to tried various 'Like' options with no success. Anyway at least I know that I need to write the code 'by hand'.
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: List only the files without extensions

Post by cogier »

Further interesting find. Try the following code that will tell you the extensions of your Home folder's hidden files.

I thought I could use: -
If File.ext(Folder &/ Name) = "" Then..
But it does not work for files that start with a dot '.'
Public Sub Main()
Dim sList As String[]
Dim sTemp As String
Dim sFolder As String = User.Home 
 
sList = Dir(sFolder, "*", gb.file)
 
For Each sTemp In sList
  If sTemp Not Begins "." Then Continue
  Print sTemp & " - Extention = " & File.ext(sFolder &/ sTemp)
Next

End
Or try it here https://gambas-playground.proko.eu/?gis ... 3390b0d32e

The output will show all the extensions for files that start with a dot '.'
. . .
.pam_environment - Extention = pam_environment
.xsession-errors.old - Extention = old
. . .
I also found that File.BaseName for a file named .text returns a blank string

Is that a bug? :?

Work around: -
Public Sub Main()
Dim sList As String[]
Dim sTemp As String
Dim sFolder As String = User.Home 
 
sList = Dir(sFolder, "*", gb.file)
 
For Each sTemp In sList
  If sTemp Not Begins "." Then Continue
  If File.ext(sFolder &/ sTemp) And File.BaseName(sFolder &/ sTemp) = "" Then 
    Print "Extention = NONE\t" & sTemp
  Else
    Print "Extention = " & File.ext(sFolder &/ sTemp) & "   \t" & sTemp
  End If
Next

End
Output

Code: Select all

Extention = NONE        .ICEauthority
Extention = NONE        .so_sane_state
Extention = NONE        .taskrc
Extention = NONE        .node_repl_history
Extention = conf        .fonts.conf
Extention = NONE        .bash_logout
Extention = NONE        .bashrc
Extention = NONE        .wget-hsts
Extention = NONE        .Xauthority
Extention = NONE        .gtk-recordmydesktop
Extention = NONE        .sudo_as_admin_successful
Extention = NONE        .profile
Extention = NONE        .bash_history
Extention = lock        .gksu.lock
Extention = NONE        .xinputrc
Extention = NONE        .pam_environment
Extention = old         .xsession-errors.old
Extention = NONE        .stuff
Extention = png         .TempQRCodeFile.png
didier18
Posts: 38
Joined: Monday 19th December 2016 10:08pm

Re: List only the files without extensions

Post by didier18 »

Hello cogier

To see the files without the extension, I use the command:
File.BaseName
Normally just typing File. gambas3 should offer contextual help ...
You can also use the command:
File.Ext to see only the file extension ...
Or
File.Dir to see the path associated with the file.

Of course if you ask File.BaseName on the name of the file and it is .txt then gambas3 will return an empty string (which is normal)

I hope that will help.

Have a good day.
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: List only the files without extensions

Post by cogier »

Thanks everyone for your help. I'm getting there. If you want to see what it was all about you can see progress here https://github.com/charlie-ogier/Find_In_Files. If you do test the program please report any bugs, an email address is in the "About" section.
Find In Files.png
Find In Files.png (1.23 MiB) Viewed 11330 times
Post Reply