For loop files vs folders
Posted
#1
(In Topic #643)
Trainee
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
Posted
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?
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)
- TextArea1.Insert("File: " & filename & " copied\n")
- TextArea1.Insert("File: " & filename & " copied\n")
Posted
Trainee
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
Posted
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
Posted
Guru

Posted
Trainee
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
Posted
Guru

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>
1 guest and 0 members have just viewed this.



