Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > General > copy files from folder to folder


Reply
 
Thread Tools Display Modes
  #1  
Old 11-27-2002, 01:55 AM
sirbum69
Guest
 
Posts: n/a
Default copy files from folder to folder


what is the easist way to copy files from one folder to another folder useing names from a text box.....

what i mean by names in a text box is i have the names of the .BMP files in a text box on my form.....

then i will have the path that the actull textures are in...

so i need to pull the texture files from the folder to another folder?

also is it just me or are these boards reaaaaaaaalll slow loading at night
Reply With Quote
  #2  
Old 11-27-2002, 02:13 AM
DarkProphet's Avatar
DarkProphet DarkProphet is offline
Centurion
 
Join Date: Nov 2002
Location: Australia
Posts: 117
Default

try filecopy (path,newpath)

yes, they are slow
__________________
every jumbled pile of person has a thinking part that wonders what the part that isn't thinking isn't thinking of...
Reply With Quote
  #3  
Old 11-27-2002, 03:33 AM
sirbum69
Guest
 
Posts: n/a
Default

well this is what im trying and im getting file not found.....

Code:
Private Sub Command5_Click()
Dim fs
Set fs = CreateObject("scripting.filesystemobject")
FileName = Text1.Text
Debug.Print FileName
fs.MoveFile FileName, Text2.Text
End Sub
filename is the text box that has the name of the file in it...actully there is a list of names...
and the source dir of where that filename would be is held in a text box called txtDir.text
so i tried this but it came out file not found too

filename = "txtDir.text" + "\" + text1.text
Reply With Quote
  #4  
Old 11-27-2002, 03:49 AM
sirbum69
Guest
 
Posts: n/a
Default

ok i know im on the right track here i can just smell it...

this is what i need to do in a nut shell.....

i have 1 folder "textures"
inside that folder are textures and 2 subfolders called "decals" and "sprites"

i need to take the names i have in the textbox all of them and search through the three folders to pull out the bmp or tga file that has the same name....

my problem is this they can be in either of the three files and they can be either a tga file or a bmp file....

im thinking i have to do a recursive search before i do the copyfile process.....
Reply With Quote
  #5  
Old 11-27-2002, 06:03 PM
sirbum69
Guest
 
Posts: n/a
Default

ok maybe i should go about this a diff way.....

would it be better if i opened alll three folders first and read in all the files in those three folders....so that they are in a array of some sort....

problem with this is that the total of all three files can be large..as i have over 2000 textures in just normal texture folder...
Reply With Quote
  #6  
Old 11-27-2002, 06:08 PM
BillSoo's Avatar
BillSoo BillSoo is offline
Code Meister

Retired Moderator
* Guru *
 
Join Date: Aug 2000
Location: Vancouver, BC, Canada
Posts: 10,441
Default

I have a recursive example in tutors corner, but for something like this, hardcoding isn't that terrible....

If you are planning on MOVING (not copying) the files from one dir to another ON THE SAME DRIVE, you might consider the NAME function. Otherwise, try the FileCopy statement.

I don't really see the point of calling the fso for something simple like this....
__________________
"I have a plan so cunning you could put a tail on it and call it a weasel!" - Edmund Blackadder
Reply With Quote
  #7  
Old 11-27-2002, 06:16 PM
BillSoo's Avatar
BillSoo BillSoo is offline
Code Meister

Retired Moderator
* Guru *
 
Join Date: Aug 2000
Location: Vancouver, BC, Canada
Posts: 10,441
Default

Simple example:

Code:
Private Sub CopyFile(Byval fn as string, byval DestDir as string) 'copies file(s) fn.* from 3 fixed directories to destdir Dim srcDir as string Dim s as string if Right$(DestDir,1) <> "\" then DestDir = DestDir & "\" srcDir = "c:\Textures\" s = Dir$(s & fn & ".*") While Len(s) 'while s is not null FileCopy srcDir & fn, DestDir & fn s = Dir$ 'get another file Wend srcDir = "c:\Textures\Decals\" s = Dir$(s & fn & ".*") While Len(s) 'while s is not null FileCopy srcDir & fn, DestDir & fn s = Dir$ 'get another file Wend srcDir = "c:\Textures\Sprites\" s = Dir$(s & fn & ".*") While Len(s) 'while s is not null FileCopy srcDir & fn, DestDir & fn s = Dir$ 'get another file Wend End Sub
__________________
"I have a plan so cunning you could put a tail on it and call it a weasel!" - Edmund Blackadder
Reply With Quote
  #8  
Old 11-27-2002, 06:38 PM
sirbum69
Guest
 
Posts: n/a
Default

ok im gonna give your code a try question i have about it is the name of the file that i need to copy(not move that was my mistake) is in a text box on my application...there is a list of names in the textbox....those are all the ones i need to copy out of one of the folders....so do i need to place that in anywhere....
Reply With Quote
  #9  
Old 11-27-2002, 06:50 PM
BillSoo's Avatar
BillSoo BillSoo is offline
Code Meister

Retired Moderator
* Guru *
 
Join Date: Aug 2000
Location: Vancouver, BC, Canada
Posts: 10,441
Default

First, a caveat: I wrote this on the fly so it is UNTESTED.

Anyway, it's written as a sub (copyfile fn, destdir) so all you need to do is have another sub and call it in a loop:

Code:
dim i as integer dim names as string dim aFn() as string 'array of strings names = text1.text 'collect the names into a single string 'PRESUMABLY you've separated the names somehow, maybe by crlf or tabs or spaces or commas. ' If not, you got a problem.... aFn = Split(names," ") 'assumes filenames are separated by spaces For i = 0 to ubound(aFn) if len(aFn(i)) then 'simple error check CopyFile aFn(i), "c:\temp\" end if next i
__________________
"I have a plan so cunning you could put a tail on it and call it a weasel!" - Edmund Blackadder
Reply With Quote
  #10  
Old 11-27-2002, 07:02 PM
sirbum69
Guest
 
Posts: n/a
Default

ok thanks bill for the help...its giveing me a file path not found....

i think this little adventure i took on was way past my noobie skills so i think im gonna just hang in the towel....but thank you for replying.....
Reply With Quote
  #11  
Old 11-28-2002, 02:55 AM
sirbum69
Guest
 
Posts: n/a
Default

can someone let me know how do you loop through a text box...

i know for a file it would be while not EOF but i need to loop teh above code in my command button_click and its reading out of a text box....

Code:
Private Sub Command5_Click()
Dim i As Integer
Dim names As String
Dim aFn() As String  'array of strings

names = Text1.Text  'collect the names into a single string
'PRESUMABLY you've separated the names somehow, maybe by crlf or tabs or spaces or commas.
' If not, you got a problem....
aFn = Split(names, vbCrLf) 'assumes filenames are separated by spaces
Do
For i = 0 To UBound(aFn)
     If Len(aFn(i)) Then  'simple error check
          FileCopy txtDir.Text & "\" & aFn(i) & ".tga", "c:\textures"
     End If
Next i
Loop
End Sub
this is what im useing and its giveing me a path/file axcess error..when i hold the cursor over the aFn(i) it shows the file and the correct path and the file is in that folder to copy it...do i have the .tga part wrong...couse in the text box it just has the name not the file ext...

Last edited by sirbum69; 11-28-2002 at 03:39 AM.
Reply With Quote
  #12  
Old 11-28-2002, 04:03 AM
BillSoo's Avatar
BillSoo BillSoo is offline
Code Meister

Retired Moderator
* Guru *
 
Join Date: Aug 2000
Location: Vancouver, BC, Canada
Posts: 10,441
Default

That Do Loop you put in will cause your program to lock up since it will run infinitely....it's a good thing you get a file error; otherwise, you could fill up your hard drive.

I see you are using FileCopy. I seem to recall that it requires a name of a file, not just a directory to work.

src=TxtDir.Text & "\" & aFn(i) & ".tga"
dest = "c:\textures\" & aFn(i) & ".tga"
Debug.print "copying from ";src;" to ";dest
FileCopy src, dest
__________________
"I have a plan so cunning you could put a tail on it and call it a weasel!" - Edmund Blackadder
Reply With Quote
  #13  
Old 11-28-2002, 04:07 AM
sirbum69
Guest
 
Posts: n/a
Default

lol that would not have been a good thing.....i can see it now i had 3 gigs now i have nothing....ok ill give this a shot....

see what happens....one other quick question on this line...if there is a chanve that the file is either of the two... .bmp or .tga can that be added in there so that it will look for either...reason i ask is couse i know that the first one in the list is a .tga but i dont know what the 2nd and so forth is...i just know that for sure it will either be a tga or a bmp...
Reply With Quote
  #14  
Old 11-28-2002, 04:19 AM
sirbum69
Guest
 
Posts: n/a
Default

well that worked like a charm finnaly....it went through and copied the first 5 till it hit a texture it couldnt find in the deacls folder which is why i was trying to work on the looking in all three folders.....
Reply With Quote
  #15  
Old 11-28-2002, 04:34 AM
BillSoo's Avatar
BillSoo BillSoo is offline
Code Meister

Retired Moderator
* Guru *
 
Join Date: Aug 2000
Location: Vancouver, BC, Canada
Posts: 10,441
Default

Hmmm.....you do realize that I gave you code for TWO functions? The one you are using was supposed to call the other one (CopyFile). That one should be able to go into the subdirectories and find all the .tga or .bmp files required.....
__________________
"I have a plan so cunning you could put a tail on it and call it a weasel!" - Edmund Blackadder
Reply With Quote
  #16  
Old 11-28-2002, 05:23 AM
sirbum69
Guest
 
Posts: n/a
Default

Code:
Private Sub CopyFile(ByVal fn As String, ByVal DestDir As String)
'copies file(s) fn.* from 3 fixed directories to destdir
Dim srcDir As String
Dim s As String

If Right$(DestDir, 1) <> "\" Then DestDir = DestDir & "\"
srcDir = "c:\Sierra\SWAT3\Missions\sample\mission\texture\"
s = Dir$(s & fn & ".*")
While Len(s)   'while s is not null
     FileCopy srcDir & fn, DestDir & fn
     s = Dir$  'get another file
Wend
srcDir = "c:\Sierra\SWAT3\Missions\sample\mission\texture\decals\"
s = Dir$(s & fn & ".*")
While Len(s)   'while s is not null
     FileCopy srcDir & fn, DestDir & fn
     s = Dir$  'get another file
Wend
srcDir = "c:\Sierra\SWAT3\Missions\sample\mission\texture\sprites"
s = Dir$(s & fn & ".*")
While Len(s)   'while s is not null
     FileCopy srcDir & fn, DestDir & fn
     s = Dir$  'get another file
Wend
End Sub



Private Sub Command5_Click()
Dim i As Integer
Dim names As String
Dim aFn() As String  'array of strings
Dim src As String
Dim dest As String

names = Text1.Text  'collect the names into a single string
'PRESUMABLY you've separated the names somehow, maybe by crlf or tabs or spaces or commas.
' If not, you got a problem....
 aFn = Split(names, vbCrLf)  'assumes filenames are separated by spaces


For i = 0 To UBound(aFn)
     If Len(aFn(i)) Then  'simple error check
     
     
src = CopyFile

dest = CopyFile
Debug.Print "copying from "; src; " to "; dest

FileCopy src, dest
End If
Next i
 
End Sub
ok that is what i have there now...and it said argument not optional and highlighted where i put src= copyfile
Reply With Quote
  #17  
Old 11-28-2002, 12:33 PM
BillSoo's Avatar
BillSoo BillSoo is offline
Code Meister

Retired Moderator
* Guru *
 
Join Date: Aug 2000
Location: Vancouver, BC, Canada
Posts: 10,441
Default

OK.....I see that you might be a bit new to subroutines....

When you create a function or sub, you define it's parameters (or argument list) and it's return value. The only difference between subs and functions is that subs have no return value, but both MAY have parameters.

In this case, the CopyFile sub has been defined as:

Private Sub CopyFile(ByVal fn As String, ByVal DestDir As String)

This means
- there are 2 parameters required, fn and DestDir
- they are both strings
- The BYVAL indicates that a COPY of the actual variable is made and used within the function. It would be similar to:

fn = filename

now both fn and filename contain the same value and we can modify fn WITHOUT modifying filename since fn is just a copy. In contrast, passing a variable BYREF means that a pointer to the ACTUAL variable is passed in. In that case, modifying fn WILL modify filename.

Anyway, CopyFile has been defined as a sub that requires 2 parameters and returns NO value.

So all you have to do is pass the filename and the destination directory to the sub. You can do that in the click event you have.

Code:
Private Sub Command5_Click() Dim i As Integer Dim names As String Dim aFn() As String 'array of strings names = Text1.Text 'collect the names into a single string 'PRESUMABLY you've separated the names somehow, maybe by crlf or tabs or spaces or commas. ' If not, you got a problem.... aFn = Split(names, vbCrLf) 'assumes filenames are separated by spaces For i = 0 To UBound(aFn) If Len(aFn(i)) Then 'simple error check CopyFile aFn(i), "c:\textures\" End If Next i End Sub
__________________
"I have a plan so cunning you could put a tail on it and call it a weasel!" - Edmund Blackadder
Reply With Quote
  #18  
Old 11-28-2002, 12:56 PM
sirbum69
Guest
 
Posts: n/a
Default

ok but see whats happning and the reason i thought it wasnt working is couse when i put the code above in the click event...

and i hit the button nothing happens....i dont get an error but it doesnt copy the files from the source to the dest folder....

and where i was understanding something else is that no where in the copyfile sub do i see a refrence to what fn is...i see it used in the function but i dont see anywhere where it says something like
fn= filname....
Reply With Quote
  #19  
Old 11-28-2002, 01:14 PM
sirbum69
Guest
 
Posts: n/a
Default

ok and i just used the step into debugging to look at each step as it went through.....

and for the line that says copyfile afn(i) the afn(i) shows the name of the first file to be copied....as "1_graf_decal" then it steps out of it....so the statement

Code:
 
copyfile afn(i), "c:\textures"
to me looks like it should do the event filecopy couse in the sub for copy file it has the code FileCopy.....so im not seeing why its not doing it.....

and your right i havnt had much in the way of working with sub routines other then just as a event driven thing....another words ive always coded what i wanted right under the event .....click etc..but im trying to learn it....lol....much as it seems that im not...
Reply With Quote
  #20  
Old 11-28-2002, 01:17 PM
BillSoo's Avatar
BillSoo BillSoo is offline
Code Meister

Retired Moderator
* Guru *
 
Join Date: Aug 2000
Location: Vancouver, BC, Canada
Posts: 10,441
Default

Well, I said it was untested.....I took another look and found a typo in CopyFile:

s = Dir$(s & fn & ".*")


Should be:

s = Dir$(srcDir & fn & ".*")

In 3 places.

(BTW: It's still untested )
__________________
"I have a plan so cunning you could put a tail on it and call it a weasel!" - Edmund Blackadder
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->