Copying File

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lee123
    Contributor
    • Feb 2007
    • 556

    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
  • vdraceil
    New Member
    • Jul 2007
    • 236

    #2
    For filesystemobjec t to come into action you have to add microsoft scripting runtime lib to your project references..did you do that lee?

    Comment

    • lee123
      Contributor
      • Feb 2007
      • 556

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

      lee123

      Comment

      • lee123
        Contributor
        • Feb 2007
        • 556

        #4
        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

        Comment

        • vdraceil
          New Member
          • Jul 2007
          • 236

          #5
          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 filesystemobjec t
          dim fld as folder

          set fld = getfolder(sourc e)
          fld.copy(destin ation)
          [\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.

          Comment

          • jg007
            Contributor
            • Mar 2008
            • 283

            #6
            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")

            Comment

            • lee123
              Contributor
              • Feb 2007
              • 556

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

              Comment

              • vdraceil
                New Member
                • Jul 2007
                • 236

                #8
                Thanx..i know the code i posted will work for sure..But fso.copyfolder doesnt work..If i had used 'dim fso as NEW filesystemobjec t' would it work? Let me try and then reply..

                Comment

                • lee123
                  Contributor
                  • Feb 2007
                  • 556

                  #9
                  P.S,

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

                  lee123

                  Comment

                  • vdraceil
                    New Member
                    • Jul 2007
                    • 236

                    #10
                    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 filesystemobjec t.copyfolder or filesystemobjec t.copyfile?
                    Hope i'm clear enough.

                    Comment

                    • lee123
                      Contributor
                      • Feb 2007
                      • 556

                      #11
                      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

                      Comment

                      • vdraceil
                        New Member
                        • Jul 2007
                        • 236

                        #12
                        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.

                        Comment

                        • lee123
                          Contributor
                          • Feb 2007
                          • 556

                          #13
                          You Kow now that i think about this i would rather have two textbox and a button to enter in the "Source" and the "Destinatio n" How Can I do this? any suggestions -anyone-

                          lee123

                          Comment

                          • vdraceil
                            New Member
                            • Jul 2007
                            • 236

                            #14
                            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 "Destinatio n" 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?

                            Comment

                            • vdraceil
                              New Member
                              • Jul 2007
                              • 236

                              #15
                              Yes lee..i was right the last time.if i use the NEW keyword with filesystemobjec t then its filecopy and foldercopy methods are working..so you can finish copying in a single line.

                              Comment

                              Working...