For loop files vs folders

Post

Posted
Rating:
#1 (In Topic #643)
Trainee
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

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

Post

Posted
Rating:
#2
Guru
BruceSteers is in the usergroup ‘Guru’

GBowlsby said

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 (gambas)

  1.  
  2.  
  3.  

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()

Code (gambas)

  1. For Each filename As String In Dir("/home/gbowlsby/Desktop/")
  2.     If IsDir(filename) Then
  3.        TextArea1.Insert("File: " & filename & " copied\n")
  4.     Else
  5.        TextArea1.Insert("File: " & filename & " copied\n")
  6.      Copy "/home/gbowlsby/Desktop/" & filename To "/home/gbowlsby/Desktop/NewFolder2/" & filename
  7.     Endif
  8.    Next
  9.   End
  10.  
Online now: No Back to the top

Post

Posted
Rating:
#3
Trainee
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

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

Post

Posted
Rating:
#4
Guru
BruceSteers is in the usergroup ‘Guru’

GBowlsby said

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

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

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

Post

Posted
Rating:
#5
Guru
BruceSteers is in the usergroup ‘Guru’
you missed my deliberate mistake ;)
Online now: No Back to the top

Post

Posted
Rating:
#6
Trainee
Thank you so much. It is working.

Code

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

Post

Posted
Rating:
#7
Avatar
Guru
cogier is in the usergroup ‘Guru’
A simpler solution could be: -

Code (gambas)

  1.   Dim DirString As String = User.Home &/ "Desktop"
  2.  
  3.   For Each filename As String In Dir(DirString, "*", gb.File)
  4.     Copy DirString &/ filename To DirString &/ "NewFolder2" &/ filename
  5.   Next

TIP for  GBowlsby

Use the gb button for adding code not the </> button, It will then format it as above.
<IMG src="https://www.cogier.com/gambas/gb.png"> </IMG>
Online now: No Back to the top
1 guest and 0 members have just viewed this.