Page 1 of 1

List only the files without extensions

Posted: Monday 16th October 2017 4:28pm
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

Re: List only the files without extensions

Posted: Monday 16th October 2017 7:21pm
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

Re: List only the files without extensions

Posted: Tuesday 17th October 2017 2:00pm
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

Re: List only the files without extensions

Posted: Tuesday 17th October 2017 2:37pm
by stevedee
OK, try adding the Start argument:-
  If InStr(sTemp, ".", 2) = 0 Then

Re: List only the files without extensions

Posted: Tuesday 17th October 2017 8:31pm
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

Re: List only the files without extensions

Posted: Wednesday 18th October 2017 1:36pm
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'.

Re: List only the files without extensions

Posted: Wednesday 18th October 2017 4:58pm
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

Re: List only the files without extensions

Posted: Thursday 19th October 2017 10:06am
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.

Re: List only the files without extensions

Posted: Friday 20th October 2017 3:42pm
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 11346 times