Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old September 2nd, 2008, 02:06 PM
lee123's Avatar
Needs Regular Fix
 
Join Date: Feb 2007
Location: United States
Age: 37
Posts: 460
Default 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
Reply
  #2  
Old September 2nd, 2008, 03:12 PM
vdraceil's Avatar
Familiar Sight
 
Join Date: Jul 2007
Location: tamil nadu, INDIA
Age: 20
Posts: 187
Default

For filesystemobject to come into action you have to add microsoft scripting runtime lib to your project references..did you do that lee?
Reply
  #3  
Old September 2nd, 2008, 03:38 PM
lee123's Avatar
Needs Regular Fix
 
Join Date: Feb 2007
Location: United States
Age: 37
Posts: 460
Default

yes, i have that on. did i do the code right?

lee123
Reply
  #4  
Old September 2nd, 2008, 04:04 PM
lee123's Avatar
Needs Regular Fix
 
Join Date: Feb 2007
Location: United States
Age: 37
Posts: 460
Default

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:

Code:
"C:\Tesla"
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
Reply
  #5  
Old September 2nd, 2008, 06:02 PM
vdraceil's Avatar
Familiar Sight
 
Join Date: Jul 2007
Location: tamil nadu, INDIA
Age: 20
Posts: 187
Default

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.
Reply
  #6  
Old September 2nd, 2008, 06:08 PM
Familiar Sight
 
Join Date: Mar 2008
Posts: 244
Default

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")
Reply
  #7  
Old September 2nd, 2008, 07:40 PM
lee123's Avatar
Needs Regular Fix
 
Join Date: Feb 2007
Location: United States
Age: 37
Posts: 460
Default

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
Reply
  #8  
Old September 2nd, 2008, 07:43 PM
vdraceil's Avatar
Familiar Sight
 
Join Date: Jul 2007
Location: tamil nadu, INDIA
Age: 20
Posts: 187
Default

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..
Reply
  #9  
Old September 2nd, 2008, 07:43 PM
lee123's Avatar
Needs Regular Fix
 
Join Date: Feb 2007
Location: United States
Age: 37
Posts: 460
Default

P.S,

Make sure you load the "Microsoft Scripting Runtime" to do this go into the Projects - Reference. and scroll down to it.

lee123
Reply
  #10  
Old September 2nd, 2008, 07:52 PM
vdraceil's Avatar
Familiar Sight
 
Join Date: Jul 2007
Location: tamil nadu, INDIA
Age: 20
Posts: 187
Default

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.
Reply
  #11  
Old September 2nd, 2008, 08:13 PM
lee123's Avatar
Needs Regular Fix
 
Join Date: Feb 2007
Location: United States
Age: 37
Posts: 460
Default

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
Reply
  #12  
Old September 2nd, 2008, 08:36 PM
vdraceil's Avatar
Familiar Sight
 
Join Date: Jul 2007
Location: tamil nadu, INDIA
Age: 20
Posts: 187
Default

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.
Reply
  #13  
Old September 2nd, 2008, 09:44 PM
lee123's Avatar
Needs Regular Fix
 
Join Date: Feb 2007
Location: United States
Age: 37
Posts: 460
Default

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
Reply
  #14  
Old September 3rd, 2008, 06:41 PM
vdraceil's Avatar
Familiar Sight
 
Join Date: Jul 2007
Location: tamil nadu, INDIA
Age: 20
Posts: 187
Default

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?
Reply
  #15  
Old September 3rd, 2008, 06:50 PM
vdraceil's Avatar
Familiar Sight
 
Join Date: Jul 2007
Location: tamil nadu, INDIA
Age: 20
Posts: 187
Default

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.
Reply
  #16  
Old September 3rd, 2008, 07:51 PM
lee123's Avatar
Needs Regular Fix
 
Join Date: Feb 2007
Location: United States
Age: 37
Posts: 460
Default

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
Reply
  #17  
Old September 3rd, 2008, 08:42 PM
Familiar Sight
 
Join Date: Mar 2008
Posts: 244
Default

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
Reply
  #18  
Old September 3rd, 2008, 10:51 PM
lee123's Avatar
Needs Regular Fix
 
Join Date: Feb 2007
Location: United States
Age: 37
Posts: 460
Default

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
Reply
  #19  
Old September 4th, 2008, 10:08 AM
Familiar Sight
 
Join Date: Mar 2008
Posts: 244
Default

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
Reply
  #20  
Old September 4th, 2008, 10:09 AM
Familiar Sight
 
Join Date: Mar 2008
Posts: 244
Default

just noticed that I missed out a ' . ' somehow :)

Code:
Dim openfolderfialog1 = New FolderBrowserDialog()
openfolderfialog1.ShowDialog()
foldertocopy = openfolderfialog1.SelectedPath
Reply
  #21  
Old September 4th, 2008, 01:56 PM
vdraceil's Avatar
Familiar Sight
 
Join Date: Jul 2007
Location: tamil nadu, INDIA
Age: 20
Posts: 187
Default

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
Reply
  #22  
Old September 4th, 2008, 02:48 PM
lee123's Avatar
Needs Regular Fix
 
Join Date: Feb 2007
Location: United States
Age: 37
Posts: 460
Default

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
Reply
  #23  
Old September 5th, 2008, 01:18 AM
Newbie
 
Join Date: Sep 2008
Posts: 1
Default

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
Reply
  #24  
Old September 5th, 2008, 10:24 AM
Familiar Sight
 
Join Date: Mar 2008
Posts: 244
Default

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
Reply
Reply

Bookmarks


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

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 Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

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.
Post your question now . . .
It's fast and it's free

Popular Articles