7 Replies - 14135 Views - Last Post: 18 November 2010 - 05:59 AM Rate Topic: -----

#1 koffer08  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 09-November 10

Copy/move file to different directory

Posted 09 November 2010 - 09:05 PM

Hi There,

I'm working on a project for work and I'm having some issue trying to distribute to program the smoothes as possible, on idea I had was using VB, Iím kind of new at this so ill explain to you what Iím hoping to do. Basically to have commandbuttons when you click on them it would get a file from lets say G:\path\path1\path2\file.hta to C:\path\path1\folder So it would take the file and copy past it in another folder from a different directory. I look everywhere for a code that could help me. If a popup message could popup saying completed as well that would be great.

Is This A Good Question/Topic? 0
  • +

Replies To: Copy/move file to different directory

#2 guyfromri  Icon User is offline

  • D.I.C Addict

Reputation: 46
  • View blog
  • Posts: 817
  • Joined: 16-September 09

Re: Copy/move file to different directory

Posted 09 November 2010 - 10:17 PM

Look into FileCopy

That will copy a file from a location to a location

Then look into Kill

That will delete the source file

Then look into msgbox

This will give a popup message box when it's done to let you know...


One question, is the destination location already specified or do you want the user to select it before the program runs? If you want the user to select it, then you'll have to create a string(strFileLoc) and use FileDialog 'or' CommonDialog to let the user select the new location.

Hope this helps! Come back with some code/errors and we'll go from there :)
Was This Post Helpful? 0
  • +
  • -

#3 maj3091  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 300
  • View blog
  • Posts: 1,780
  • Joined: 26-March 09

Re: Copy/move file to different directory

Posted 10 November 2010 - 05:56 AM

guyfromri has given you some great pointers there. You could also look into the filesystem object, which has lots of file and folder manipulation functions.
Was This Post Helpful? 0
  • +
  • -

#4 koffer08  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 09-November 10

Re: Copy/move file to different directory

Posted 10 November 2010 - 08:46 PM

thanks for the heads up, like i said I dont usely use VB6 so i have no idea how to start, if you guys could show my a code i could start from there, thanks
Was This Post Helpful? 0
  • +
  • -

#5 guyfromri  Icon User is offline

  • D.I.C Addict

Reputation: 46
  • View blog
  • Posts: 817
  • Joined: 16-September 09

Re: Copy/move file to different directory

Posted 10 November 2010 - 11:13 PM

We're not really in the habbit of creating code for people here but since this is relatively easy, try this out.


Sub test()

Dim strSelFile As String
Dim strNewLoc As String

MsgBox ("Select file to copy")
With Application.FileDialog(msoFileDialogFilePicker)
    If .Show = False Then End
    strSelFile = .SelectedItems(1)
    .AllowMultiSelect = False
    .Title = "Location to output new file to"
End With

MsgBox ("Select location to output file to")
With Application.FileDialog(msoFileDialogFolderPicker)
    If .Show = False Then End
    strNewLoc = .SelectedItems(1)
    .AllowMultiSelect = False
    .Title = "Location to output new file to"
End With

FileCopy strSelFile, strNewLoc

If MsgBox("Do you want to delete the source file?", vbYesNo) = vbNo Then Kill (strSelFile)

MsgBox ("Done!")

End Sub


Was This Post Helpful? 0
  • +
  • -

#6 koffer08  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 09-November 10

Re: Copy/move file to different directory

Posted 17 November 2010 - 09:02 PM

View Postguyfromri, on 10 November 2010 - 10:13 PM, said:

We're not really in the habbit of creating code for people here but since this is relatively easy, try this out.


Sub test()

Dim strSelFile As String
Dim strNewLoc As String

MsgBox ("Select file to copy")
With Application.FileDialog(msoFileDialogFilePicker)
    If .Show = False Then End
    strSelFile = .SelectedItems(1)
    .AllowMultiSelect = False
    .Title = "Location to output new file to"
End With

MsgBox ("Select location to output file to")
With Application.FileDialog(msoFileDialogFolderPicker)
    If .Show = False Then End
    strNewLoc = .SelectedItems(1)
    .AllowMultiSelect = False
    .Title = "Location to output new file to"
End With

FileCopy strSelFile, strNewLoc

If MsgBox("Do you want to delete the source file?", vbYesNo) = vbNo Then Kill (strSelFile)

MsgBox ("Done!")

End Sub



hey thanks for the code,

so i tried using it, looks like its asking the user to choose the file to copy and its location, do you think you might be able to show me how to get a specific file and move it to a specific folder in a different directory, for G:/ to C:/ and no option to delete. it would be very helpful thanks.
Was This Post Helpful? 0
  • +
  • -

#7 maj3091  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 300
  • View blog
  • Posts: 1,780
  • Joined: 26-March 09

Re: Copy/move file to different directory

Posted 18 November 2010 - 05:51 AM

Just try coding your from and to paths in the two variables strSelFile and strNewLoc, then call the FileCopy function.

guyfromri has given you everything you need to do what you asked, he even wrote the code for you, which isn't the norm here.

Have a go at making the changes yourself and if you have problems post back your code and the issues you have.

This post has been edited by maj3091: 18 November 2010 - 05:52 AM

Was This Post Helpful? 0
  • +
  • -

#8 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 464
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: Copy/move file to different directory

Posted 18 November 2010 - 05:59 AM

you can also check our tutorial section for some more information
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1