For loop files vs folders

New to Gambas? Post your questions here. No question is too silly or too simple.
Post Reply
GBowlsby
Posts: 6
Joined: Saturday 24th April 2021 6:22pm

For loop files vs folders

Post by GBowlsby » Saturday 24th April 2021 8:59pm

I want to copy all files from a folder to a new folder excluding folders.
So in my test I have two folders Gambas_Projects and NewFolder2 below code I am using an if statement for individual folders.

Is there a way to say if file = folder vs naming the folder?

Code: Select all

For Each file As String In Dir("/home/gbowlsby/Desktop/")
    If File = "NewFolder2" Or If file = "Gambas_Projects" Then
       TextArea1.Text &= "File: " & File & " copied" & Chr(10)
    Else
       TextArea1.Text &= "File: " & File & " copied" & Chr(10)
     Copy "/home/gbowlsby/Desktop/" & File To "/home/gbowlsby/Desktop/NewFolder2/" & File
    Endif
   Next
  End

User avatar
BruceSteers
Posts: 521
Joined: Thursday 23rd July 2020 5:20pm

Re: For loop files vs folders

Post by BruceSteers » Saturday 24th April 2021 9:40pm

GBowlsby wrote:
Saturday 24th April 2021 8:59pm
I want to copy all files from a folder to a new folder excluding folders.
So in my test I have two folders Gambas_Projects and NewFolder2 below code I am using an if statement for individual folders.

Is there a way to say if file = folder vs naming the folder?

If IsDir(file)

Notes,,
Do not use File , File is a class name so maybe use filename or something else
I changed the Chr(10) bits for \n in the string
I used TextArea1.Insert()
For Each filename As String In Dir("/home/gbowlsby/Desktop/")
    If IsDir(filename) Then
       TextArea1.Insert("File: " & filename & " copied\n")
    Else
       TextArea1.Insert("File: " & filename & " copied\n")
     Copy "/home/gbowlsby/Desktop/" & filename To "/home/gbowlsby/Desktop/NewFolder2/" & filename
    Endif
   Next
  End
Wishing well
Bruce

If at first you don't succeed , try it differently.

GBowlsby
Posts: 6
Joined: Saturday 24th April 2021 6:22pm

Re: For loop files vs folders

Post by GBowlsby » Saturday 24th April 2021 10:38pm

So it is not working. Once it gets to a folder I get an error that says file is a folder and it stops on my Gambas_Projects.

Code: Select all

For Each filename As String In Dir("/home/gbowlsby/Desktop/")
    If IsDir(filename) Then
       TextArea1.Insert("File: " & filename & " is a folder and not copied\n")
    Else
       TextArea1.Insert("File: " & filename & " copied\n")
       Copy "/home/gbowlsby/Desktop/" & filename To "/home/gbowlsby/Desktop/NewFolder2/" & filename
         
    Endif
   Next
      
  End

User avatar
BruceSteers
Posts: 521
Joined: Thursday 23rd July 2020 5:20pm

Re: For loop files vs folders

Post by BruceSteers » Saturday 24th April 2021 10:49pm

GBowlsby wrote:
Saturday 24th April 2021 10:38pm
So it is not working. Once it gets to a folder I get an error that says file is a folder and it stops on my Gambas_Projects.

Code: Select all

For Each filename As String In Dir("/home/gbowlsby/Desktop/")
    If IsDir(filename) Then
       TextArea1.Insert("File: " & filename & " is a folder and not copied\n")
    Else
       TextArea1.Insert("File: " & filename & " copied\n")
       Copy "/home/gbowlsby/Desktop/" & filename To "/home/gbowlsby/Desktop/NewFolder2/" & filename
         
    Endif
   Next
      
  End
aah sorry , Dir may not be giving the full path in the filenames so it returns false as path is not found.
Use IsDir("/home/gbowlsby/Desktop" &/ filename) ...

Code: Select all

For Each filename As String In Dir("/home/gbowlsby/Desktop/")
    If IsDir("/home/gbowlsby/Desktop" &/ filename) Then
       TextArea1.Insert("File: " & filename & " is a folder and not copied\n")
    Else
       TextArea1.Insert("File: " & filename & " copied\n")
       Copy "/home/gbowlsby/Desktop/" & filename To "/home/gbowlsby/Desktop/NewFolder2/" & filename
         
    Endif
   Next
      
  End
Last edited by BruceSteers on Saturday 24th April 2021 10:53pm, edited 2 times in total.
Wishing well
Bruce

If at first you don't succeed , try it differently.

User avatar
BruceSteers
Posts: 521
Joined: Thursday 23rd July 2020 5:20pm

Re: For loop files vs folders

Post by BruceSteers » Saturday 24th April 2021 10:50pm

you missed my deliberate mistake ;)
Wishing well
Bruce

If at first you don't succeed , try it differently.

GBowlsby
Posts: 6
Joined: Saturday 24th April 2021 6:22pm

Re: For loop files vs folders

Post by GBowlsby » Saturday 24th April 2021 11:00pm

Thank you so much. It is working.

Code: Select all

Dim dirstring As String = "/home/gbowlsby/Desktop/"
   
   For Each filename As String In Dir(dirstring)
    If IsDir(dirstring & filename) Then
       TextArea1.Insert("File: " & filename & " is a folder\n")
    Else 
       TextArea1.Insert("File: " & filename & " copied\n")
       Copy dirstring & filename To dirstring & "NewFolder2/" & filename
    Endif
   Next
      
  End

User avatar
cogier
Site Admin
Posts: 721
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: For loop files vs folders

Post by cogier » Sunday 25th April 2021 3:03pm

A simpler solution could be: -
  Dim DirString As String = User.Home &/ "Desktop"

  For Each filename As String In Dir(DirString, "*", gb.File)
    Copy DirString &/ filename To DirString &/ "NewFolder2" &/ filename
  Next
TIP for GBowlsby

Use the gb button for adding code not the </> button, It will then format it as above.
Image

Post Reply