none
hOW Copy File to another folder RRS feed

  • Question

  • Hi. I am new to vbscript and trying to learn to create script from scratch. I am trying to copy a file from one folder to another that has date equal to what I specify:

    dim myfile, copymyfile, myfile2
    set objfso=CreateObject("Scripting.FileSystemObject")
    targetfolder="C:\Users\Documents\820\accounts"
    targetdate="03/06/2016"
    set objfolder=objfso.getfolder(targetfolder)
    set colfiles=objfolder.files
    for each element in colfiles
    If formatdatetime(element.DateLastModified,vbShortdate) = targetdate then
    myfile=element.name&vbTab&formatdatetime(element.DateLastModified,vbShortdate)
    myfile2=element.name
    wscript.echo myfile
    wscript.echo myfile2
    ' How do I copy file in myfile2 to another folder?
    Else
    'WScript.Echo "sorry"
    End if
    next

    Thank you in advance for your time and help/

    Tuesday, June 14, 2016 10:54 PM

Answers

  • See how much harder and stranger this is:

    start_time = CDate("9/8/2013")
    end_time = Cdate("9/9/2013")
    targetFolder = "c:\test2"
    
    Set fso=CreateObject("Scripting.FileSystemObject")
    
    Set folder=fso.getfolder("C:\scripts")
    
    for Each file in folder.files
     	If file.DateLastModified >= start_time And file.DateLastModified < end_time then
     		WScript.Echo file.name
    		fso.CopyFile file.Name, targetFolder & "\" & file.ShortName ,True
     	Else
     		'WScript.Echo "sorry"
     	End If
    Next


    \_(ツ)_/



    Wednesday, June 15, 2016 12:13 AM

All replies

  • First you should be learning PowerShell.  VBScript is legacy and nearly obsolete.  You will be wasting your time.

    Get-ChildItem $sourcefolder |?{$_.LastWriteTIme.ToShortDateString() -eq '3/6/2016'}| Copy-Item $targetfolder


    \_(ツ)_/

    • Proposed as answer by Mike Laughlin Tuesday, June 14, 2016 11:58 PM
    Tuesday, June 14, 2016 11:52 PM
  • See how much harder and stranger this is:

    start_time = CDate("9/8/2013")
    end_time = Cdate("9/9/2013")
    targetFolder = "c:\test2"
    
    Set fso=CreateObject("Scripting.FileSystemObject")
    
    Set folder=fso.getfolder("C:\scripts")
    
    for Each file in folder.files
     	If file.DateLastModified >= start_time And file.DateLastModified < end_time then
     		WScript.Echo file.name
    		fso.CopyFile file.Name, targetFolder & "\" & file.ShortName ,True
     	Else
     		'WScript.Echo "sorry"
     	End If
    Next


    \_(ツ)_/



    Wednesday, June 15, 2016 12:13 AM
  • hi jrv. thank you for your help and advised. I will also try to learn PowerShell in the future but for now I have already started vbscript and getting a little bit of understanding of it. Both have pros and cons, but both are worth learning. Going back to my original code, I feel like I am almost there, and just need the proper code. Can I use CopyFile and how do I apply that? Can I apply that right before the Else statement? Or what vbscript command I can add to copy 'myfile2' to another folder?
    Wednesday, June 15, 2016 1:52 AM