1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

VBScript to copy file from 1 path to another path

Discussion in 'Visual Basic ( VB )' started by tyme, Jul 19, 2012.

  1. tyme

    tyme New Member

    Joined:
    Jul 17, 2012
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Dear all,

    I want to copy files from a folder. Basically i i want to copy files if the folder exist. If it does not exist then check another folder. I have 3 folders to look at. The folder name is in date format yymmdd. But i just want to look at for 1st, 2nd or 3rd day of the month only. Basically the flow is like this but i don't know how to create a complete vbscript.

    If folder yymm01 exist at path: \\servername\sample\ then copy files in it to \\servername\prod\ then exit.
    else
    check if folder yymm02 exist at path: \\servername\sample\ then copy files in it to \\servername\prod\ then exit.
    else
    check if folder yymm03 exist at path: \\servername\sample\ then copy files in it to \\servername\prod\ then exit.


    Thanks in advance for any help.
     
  2. Scripting

    Scripting John Hoder

    Joined:
    Jun 29, 2010
    Messages:
    421
    Likes Received:
    57
    Trophy Points:
    0
    Occupation:
    School for life
    Location:
    /root
    Maybe you can try using CopyFile function. So something like this My.Computer.FileSystem.CopyFile(sourceFileName ,destinationFileName)
    Im not VB expert, but I hope you will find some more on google :)
     
  3. tyme

    tyme New Member

    Joined:
    Jul 17, 2012
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Dear all,

    I manage to create the script to copy the files but i got error: Object required:" at the highlighted line, char 6. Does anyone know what is wrong with my script?

    Code:
     
    Const ForReading = 1, ForWriting = 2 , ForAppending = 8
    Dim objFSO
    Dim strSortDate
    Dim strSortDate2
    Dim fso
    Dim file
    Dim year
    DIm month
    strSortDate = DatePart("yyyy",Date)
    strSortDate2 = DatePart("m",Date)
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set fso = CreateObject("Scripting.FileSystemObject")
    strSortDate = DatePart("yyyy",Date)
    year = Right(strSortDate,2)
    strSortDate2 = DatePart("m",Date)
     
    If objFSO.FolderExists("\\servername\samplefile\" & year & "0" & strSortDate2 & "01") Then
    MsgBox("Hello!")
    objFSO.copyfile file.path,[COLOR=black]\\servername\prod\[/COLOR] & strSortDate & "" & strSortDate2 & "01\" & file.name
    Else
    If objFSO.FolderExists("\\servername\samplefile\" & year & "0" & strSortDate2 & "02") Then
    MsgBox("gotcha!") 
    objFSO.copyfile file.path, \\servername\prod\ & strSortDate & "" & strSortDate2 & "02\" & file.name
    Else 
    If objFSO.FolderExists[COLOR=black]("\[/COLOR]\servername\samplefile\ & year & "0" & strSortDate2 & "03") Then
    [COLOR=darkorchid]objFSO.copyfile file.path,[/COLOR][COLOR=plum] [/COLOR][COLOR=darkorchid]\\servername\prod\[/COLOR][COLOR=purple][COLOR=darkorchid]& strSortDate & "" & strSortDate2 & "03\" & file.name[/COLOR]
    
    [/COLOR]End if
    set objFSO = nothing 
    set fSO = nothing 
    End if
     
    Last edited by a moderator: Jul 20, 2012

Share This Page