Page 1 of 1
For loop files vs folders
Posted: Saturday 24th April 2021 8:59pm
by GBowlsby
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
Re: For loop files vs folders
Posted: Saturday 24th April 2021 9:40pm
by BruceSteers
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
Re: For loop files vs folders
Posted: Saturday 24th April 2021 10:38pm
by GBowlsby
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
Re: For loop files vs folders
Posted: Saturday 24th April 2021 10:49pm
by BruceSteers
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
Re: For loop files vs folders
Posted: Saturday 24th April 2021 10:50pm
by BruceSteers
you missed my deliberate mistake
Re: For loop files vs folders
Posted: Saturday 24th April 2021 11:00pm
by GBowlsby
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
Re: For loop files vs folders
Posted: Sunday 25th April 2021 3:03pm
by cogier
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.