List only the files without extensions

Post

Posted
Rating:
#1 (In Topic #139)
Avatar
Guru
cogier is in the usergroup ‘Guru’
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?  :?  

Code (gambas)

  1.  
  2. Public Sub Main()
  3. Dim sList As String[]
  4. Dim sTemp As String
  5. Dim sPattern As String = "*.png"
  6.  
  7. sList = Dir(User.home, sPattern, gb.file)
  8.  
  9. For Each sTemp In sList
  10.   Print sTemp
  11.  
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Regular
stevedee is in the usergroup ‘Regular’
This will do it:-

Code

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
Online now: No Back to the top

Post

Posted
Rating:
#3
Avatar
Guru
cogier is in the usergroup ‘Guru’
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. http://forum.gambas.on…e/viewtopic.php?f=9&t=502
Online now: No Back to the top

Post

Posted
Rating:
#4
Avatar
Regular
stevedee is in the usergroup ‘Regular’
OK, try adding the Start argument:-

Code (gambas)

  1.   If InStr(sTemp, ".", 2) = 0 Then
  2.  
Online now: No Back to the top

Post

Posted
Rating:
#5
Avatar
Regular
jornmo is in the usergroup ‘Regular’
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/user/2017-October/000033.html

Online now: No Back to the top

Post

Posted
Rating:
#6
Avatar
Guru
cogier is in the usergroup ‘Guru’
 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'.
Online now: No Back to the top

Post

Posted
Rating:
#7
Avatar
Guru
cogier is in the usergroup ‘Guru’
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: -

Code (gambas)

  1. If File.ext(Folder &/ Name) = "" Then..
But it does not work for files that start with a dot '.'

Code (gambas)

  1. Public Sub Main()
  2. Dim sList As String[]
  3. Dim sTemp As String
  4. Dim sFolder As String = User.Home
  5.  
  6. sList = Dir(sFolder, "*", gb.file)
  7.  
  8. For Each sTemp In sList
  9.   If sTemp Not Begins "." Then Continue
  10.   Print sTemp & " - Extention = " & File.ext(sFolder &/ sTemp)
  11.  

Or try it here https://gambas-playgro…c9259a90637bddd3390b0d32e

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: -

Code (gambas)

  1. Public Sub Main()
  2. Dim sList As String[]
  3. Dim sTemp As String
  4. Dim sFolder As String = User.Home
  5.  
  6. sList = Dir(sFolder, "*", gb.file)
  7.  
  8. For Each sTemp In sList
  9.   If sTemp Not Begins "." Then Continue
  10.   If File.ext(sFolder &/ sTemp) And File.BaseName(sFolder &/ sTemp) = "" Then
  11.     Print "Extention = NONE\t" & sTemp
  12.   Else
  13.     Print "Extention = " & File.ext(sFolder &/ sTemp) & "   \t" & sTemp
  14.   End If
  15.  

Output

Code

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
Online now: No Back to the top

Post

Posted
Rating:
#8
Regular
didier18 is in the usergroup ‘Regular’
 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.

Online now: No Back to the top

Post

Posted
Rating:
#9
Avatar
Guru
cogier is in the usergroup ‘Guru’
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.

Image

(Click to enlarge)

Online now: No Back to the top
1 guest and 0 members have just viewed this.