 |

September 2nd, 2008, 02:06 PM
|
 |
Needs Regular Fix
|
|
Join Date: Feb 2007
Location: United States
Age: 37
Posts: 460
|
|
Copying File
i have tried to copy a file but it doesn't work can anybody tell me why by looking at this code. and try to help me get it right.
Code:
Private Sub cmdCopy_Click()
Dim ofilesysobj As FileSystemObject 'This One Doesn't work At All
Dim ofolder As Folder 'Error: Compile "Expected Function or Variable"
Set ofolder = ofilesysobj.CopyFolder("F:\Music\Tesla\*Tesla - 1986 - Mechanical resonance", "C:\Tesla")
MsgBox "File Has Been Copied"
End Sub
I keep Getting an Error
lee123
|

September 2nd, 2008, 03:12 PM
|
 |
Familiar Sight
|
|
Join Date: Jul 2007
Location: tamil nadu, INDIA
Age: 20
Posts: 187
|
|
For filesystemobject to come into action you have to add microsoft scripting runtime lib to your project references..did you do that lee?
|

September 2nd, 2008, 03:38 PM
|
 |
Needs Regular Fix
|
|
Join Date: Feb 2007
Location: United States
Age: 37
Posts: 460
|
|
yes, i have that on. did i do the code right?
lee123
|

September 2nd, 2008, 04:04 PM
|
 |
Needs Regular Fix
|
|
Join Date: Feb 2007
Location: United States
Age: 37
Posts: 460
|
|
The way i have this is
1- I am copying this folder:
this is the: Source
Code:
"F:\Music\Tesla\*Tesla - 1986 - Mechanical resonance"
and my Destination is:
which is a folder i have in there already.
but when i click the cmd btn it gives me the error: (Compile error Expected Function or Variable)
Why is that?
lee123
|

September 2nd, 2008, 06:02 PM
|
 |
Familiar Sight
|
|
Join Date: Jul 2007
Location: tamil nadu, INDIA
Age: 20
Posts: 187
|
|
Ah..yes i remember.i tried the same function some time ago but i couldnt use it(i got the same error as u got)..since then i found an alternative and i'm constantly using it for file and folder operations..Try this..
[code]
dim fso as filesystemobject
dim fld as folder
set fld = getfolder(source)
fld.copy(destination)
[\CODE]
a similar method can be used for file copying too..i dont know why its not working.if i find or you find lets post here,so that the other can know.
|

September 2nd, 2008, 06:08 PM
|
|
Familiar Sight
|
|
Join Date: Mar 2008
Posts: 244
|
|
I have tested this code and it works fine although there is no testing or exception catching it simply copies 'c:\test1' to 'c:\test2' -
to use it in VB.net you will need to add the refernece as above by right clicking your application name in solution explorer , selecteing add reference then clicking on the COM tab and adding ' microsoft scripting runtime '
Code:
Dim fso As New Scripting.FileSystemObject
fso.CopyFolder("C:\test2", "c:\test1")
|

September 2nd, 2008, 07:40 PM
|
 |
Needs Regular Fix
|
|
Join Date: Feb 2007
Location: United States
Age: 37
Posts: 460
|
|
Well I have messed around with both of these codes and i have found this to work thanks both of you guys and here it is for all to see:
Code:
Private Sub CmdSomeButton_Click()
'-----------------------------------------------------------------------------
'This is for all who don't know how to do this with this I hope this HELPS
'Thanks, vdraceil & Jg007 for this.
'-----------------------------------------------------------------------------
Dim fso As New Scripting.FileSystemObject
Dim fld As Folder
Set fld = fso.GetFolder("Copy From What Folder on Your Computer")
fld.Copy ("Copy To What Folder You Have Made Or Have Already")
MsgBox "Transfer Completed!"
End Sub
And If you Don't Know How To Create A folder Here This Is:
Code:
Private Sub CmdSomeButton_Click()
Dim ofilesys As New FileSystemObject
Dim ofolder As Folder
Set ofolder = ofilesys.CreateFolder("C:\You Folder name Goes Here")
MsgBox "Folder Has Been Created!"
End Sub
Well there you go.
lee123
|

September 2nd, 2008, 07:43 PM
|
 |
Familiar Sight
|
|
Join Date: Jul 2007
Location: tamil nadu, INDIA
Age: 20
Posts: 187
|
|
Thanx..i know the code i posted will work for sure..But fso.copyfolder doesnt work..If i had used 'dim fso as NEW filesystemobject' would it work? Let me try and then reply..
|

September 2nd, 2008, 07:43 PM
|
 |
Needs Regular Fix
|
|
Join Date: Feb 2007
Location: United States
Age: 37
Posts: 460
|
|
P.S,
Make sure you load the "Microsoft Scripting Runtime" to do this go into the Projects - Reference. and scroll down to it.
lee123
|

September 2nd, 2008, 07:52 PM
|
 |
Familiar Sight
|
|
Join Date: Jul 2007
Location: tamil nadu, INDIA
Age: 20
Posts: 187
|
|
Quote:
|
Originally Posted by lee123
P.S,
Make sure you load the "Microsoft Scripting Runtime" to do this go into the Projects - Reference. and scroll down to it.
lee123
|
well lee..the code u posted last is working.i want the code u posted at the begining is not working(but it is supposed to work).any idea of using filesystemobject.copyfolder or filesystemobject.copyfile?
Hope i'm clear enough.
|

September 2nd, 2008, 08:13 PM
|
 |
Needs Regular Fix
|
|
Join Date: Feb 2007
Location: United States
Age: 37
Posts: 460
|
|
No Idea Just learning what you people post..But I do have a question though, I want to add a form with a progressbar and an animated video how do i do this and put it inside of the code so when i press the button it will run.
lee123
|

September 2nd, 2008, 08:36 PM
|
 |
Familiar Sight
|
|
Join Date: Jul 2007
Location: tamil nadu, INDIA
Age: 20
Posts: 187
|
|
Quote:
|
Originally Posted by lee123
No Idea Just learning what you people post..But I do have a question though, I want to add a form with a progressbar and an animated video how do i do this and put it inside of the code so when i press the button it will run.
lee123
|
try using a windows media player control..it has a progress bar built in itself.it would make your task much easier.
|

September 2nd, 2008, 09:44 PM
|
 |
Needs Regular Fix
|
|
Join Date: Feb 2007
Location: United States
Age: 37
Posts: 460
|
|
You Kow now that i think about this i would rather have two textbox and a button to enter in the "Source" and the "Destination" How Can I do this? any suggestions -anyone-
lee123
|

September 3rd, 2008, 06:41 PM
|
 |
Familiar Sight
|
|
Join Date: Jul 2007
Location: tamil nadu, INDIA
Age: 20
Posts: 187
|
|
Quote:
|
Originally Posted by lee123
You Kow now that i think about this i would rather have two textbox and a button to enter in the "Source" and the "Destination" How Can I do this? any suggestions -anyone-
lee123
|
I dont get you lee..you mean you want the user to enter the source and destination and then perform a file or folder copy accordingly?
|

September 3rd, 2008, 06:50 PM
|
 |
Familiar Sight
|
|
Join Date: Jul 2007
Location: tamil nadu, INDIA
Age: 20
Posts: 187
|
|
Yes lee..i was right the last time.if i use the NEW keyword with filesystemobject then its filecopy and foldercopy methods are working..so you can finish copying in a single line.
|

September 3rd, 2008, 07:51 PM
|
 |
Needs Regular Fix
|
|
Join Date: Feb 2007
Location: United States
Age: 37
Posts: 460
|
|
you hit the nail on the head, i want to be able to type in a folder "Source" in a text box then type in a folder "destination" in second textbox then click on a button to make it all work that way you can control it the way you want to instead of it being hardwired in the code. i think i explained that right. did I?
lee123
|

September 3rd, 2008, 08:42 PM
|
|
Familiar Sight
|
|
Join Date: Mar 2008
Posts: 244
|
|
I'm not sure I see what the question is , if you want a textbox just use textbox1.text where I have placed the file name although I would probably be more inclined to use the built in directory / file selection boxes to save misstypes.
example for selecting folders -
Code:
Dim openfolderfialog1 = New FolderBrowserDialog()
openfolderfialog1ShowDialog()
foldertocopy = openfolderfialog1.SelectedPath
you can use ' System.Windows.Forms.OpenFileDialog ' for selecting a file
|

September 3rd, 2008, 10:51 PM
|
 |
Needs Regular Fix
|
|
Join Date: Feb 2007
Location: United States
Age: 37
Posts: 460
|
|
I'm Sorry But I tried to put the code the way you have it and it doesn't work. my question was if you were to make a form. you would put two textboxes on it one named "txtSourceofFolder" this would be for entering what folder you want to copy. the other textbox named "txtDestinationOfFolder" this would be where you want the folder to be copied to. now to make this all go place a button on the form named "Go" or "Begin" or something like that. the basis of this form would be, you could be incontrol of what folder you wanted to copy and where you would want that folder to be copied to. Because right now i have a form that copies the folder and places it in a folder i had to program in the code but that's no fun. I would rather have it the way i have discribed it. so if you could help me do this well that would be great!
lee123
|

September 4th, 2008, 10:08 AM
|
|
Familiar Sight
|
|
Join Date: Mar 2008
Posts: 244
|
|
If I underastand that would be pretty simple as above just replace folder names with the textbox values
I'm not too sure why the code I posted above will not work but it may be becase I am using vb.net , think I need to find a copy of vb 6 to save me confusing people !
Code:
Private Sub CmdSomeButton_Click()
Dim fso As New Scripting.FileSystemObject
Dim fld As Folder
Set fld = fso.GetFolder(txtSourceofFolder.text)
fld.Copy (txtDestinationOfFolder.text)
MsgBox "Transfer Completed!"
End Sub
|

September 4th, 2008, 10:09 AM
|
|
Familiar Sight
|
|
Join Date: Mar 2008
Posts: 244
|
|
just noticed that I missed out a ' . ' somehow :)
Code:
Dim openfolderfialog1 = New FolderBrowserDialog()
openfolderfialog1.ShowDialog()
foldertocopy = openfolderfialog1.SelectedPath
|

September 4th, 2008, 01:56 PM
|
 |
Familiar Sight
|
|
Join Date: Jul 2007
Location: tamil nadu, INDIA
Age: 20
Posts: 187
|
|
Quote:
|
Originally Posted by lee123
I'm Sorry But I tried to put the code the way you have it and it doesn't work. my question was if you were to make a form. you would put two textboxes on it one named "txtSourceofFolder" this would be for entering what folder you want to copy. the other textbox named "txtDestinationOfFolder" this would be where you want the folder to be copied to. now to make this all go place a button on the form named "Go" or "Begin" or something like that. the basis of this form would be, you could be incontrol of what folder you wanted to copy and where you would want that folder to be copied to. Because right now i have a form that copies the folder and places it in a folder i had to program in the code but that's no fun. I would rather have it the way i have discribed it. so if you could help me do this well that would be great!
lee123
|
Try this lee..(single line code)
fso.copyfolder text1.text,text2
.text
|

September 4th, 2008, 02:48 PM
|
 |
Needs Regular Fix
|
|
Join Date: Feb 2007
Location: United States
Age: 37
Posts: 460
|
|
Well vb.net or not JG007 The code you have posted works great thanks for it, Sometime the code that vb.net uses works why I don't know, But your right I should of let you both know what version i was using and it was Visual Basic 6.0 and for that I'm very sorry but thanks the both of you for you help.
this is the one I used:
Code:
Private Sub CmdSomeButton_Click()
Dim fso As New Scripting.FileSystemObject
Dim fld As Folder
Set fld = fso.GetFolder(txtSourceofFolder.text)
fld.Copy (txtDestinationOfFolder.text)
MsgBox "Transfer Completed!"
End Sub
lee123
|

September 5th, 2008, 01:18 AM
|
|
Newbie
|
|
Join Date: Sep 2008
Posts: 1
|
|
if you're using Visual Basic .NET, why not just use the My object? It's really easy to use.
Code:
My.Computer.FileSystem.CopyFile(sourceFileName, destinationFileName, overwriteBoolean)
I really use this a lot and it is very 'basic'.
Hope this helps,
Sam
_______________________________________________
Visit my site:
www.inno-tron.com
|

September 5th, 2008, 10:24 AM
|
|
Familiar Sight
|
|
Join Date: Mar 2008
Posts: 244
|
|
that is the fun of programming :), 50 different ways to achieve a goal . thanks for that I will 'try' to remember that one !
most of my programming is just pieced together from what I pick up when I need to do something so is not always the quickest or more efficient way and it is always great to see easier or better ways of doing something
|
 |
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|
What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over network members.
|