none
Problems with FileSystemObject CopyFile

    General discussion

  • Why does the first CopyFile statement fail when uncommented?  The error says Path not found. code 8004A004C
    The second copyfile statement executes without error.

    set fso = CreateObject("Scripting.FileSystemObject")

    '==fso.CopyFile "G:\SoftGrocer\*.DAT", "G:\SoftGrocer\2255.DAT"

    fso.CopyFile "G:\SoftGrocer\009998-894090206131939002-4-NEX.DAT","G:\SoftGrocer\9998.DAT"


    According to the FSO documentation on MSDN the commented line of code should work.


    Old COBOL programmer
    • Changed type Xingwei Hu Monday, February 23, 2009 6:35 AM VBS question
    Wednesday, February 18, 2009 3:45 PM

All replies

  • I think you have to check your code.

    You never can do this :
           source : *.dat
           destination : 2255.dat

    What do you want to do Jay? Explain it briefly.
    If you want to do copy all files from one directory to another you can use the code below :

    For Each f In Directory.GetFiles(sourcefolderpath)  
       IO.File.Copy(f,destination path)  
    Next  

    =) Good luck.
    Wednesday, February 18, 2009 3:51 PM
  • set fso = CreateObject("Scripting.FileSystemObject")  
    set FileOfFiles = fso.CreateTextFile("G:\SoftGrocer\FileNames.txt",True)  
    set f = fso.GetFolder("G:\SoftGrocer")  
    set fc = f.Files  
    for each f1 in fc  
        fn = "G:\SoftGrocer\" & f1.Name       
        ext = fso.GetExtensionName(fn)  
        if ext = "DAT" then  
            FileOfFiles.WriteLine f1.name  
        end if  
    Next 
    FileOfFiles.close  
     
    Set objConnection = CreateObject("ADODB.Connection")  
    Set objRecordSet = CreateObject("ADODB.Recordset")  
    strPathtoTextFile = "G:\SoftGrocer\" 
     
    objConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _  
              "Data Source=" & strPathtoTextFile & ";" & _  
              "Extended Properties=""text;HDR=NO;FMT=Delimited""" 
     
    objRecordset.Open "SELECT distinct * FROM EDI894Stores.txt",objConnection         
    do until objRecordset.eof  
     
        fso.CopyFile strPathToTextFile & ObjRecordset(0) & "*.DAT" &","& strPathToTextFile & ObjRecordset(0) &".DAT" 
          
    objRecordset.MoveNext  
    loop  
     
    objConnection.close  
     
    set fso = nothing  
    set cn = nothing  
    set rs = nothing  
          
     
     
    I have to combine many similar files into a single file.  The groups of files grouped by the first 6 characters of the file name.  My program correctly gathers the distinct first 6 characters of all the files in that directory into a file which I then read and try to execute this copyfile statement:
    fso.CopyFile strPathToTextFile & ObjRecordset(0) & "*.DAT" &","& strPathToTextFile & ObjRecordset(0) &".DAT"  ObjRecordset(0) contains the first 6 characters of those files to be concatenated. 

    I have attached the complete program
    Old COBOL programmer
    Wednesday, February 18, 2009 4:05 PM
  • Please disregard the code that captures the file names at the beginning of the program.  I abandonded that code and opted to use a file that has the first 6 characters of each file name that has a .DAT extension from a preceeding program.  I just haven't cleaned up the code.
    Old COBOL programmer
    Wednesday, February 18, 2009 4:09 PM
  • The amended code attached.
    Set fso = CreateObject(“Scripting.FileSystemObject”)  
    Set objConnection = CreateObject("ADODB.Connection")  
    Set objRecordSet = CreateObject("ADODB.Recordset")  
    strPathtoTextFile = "G:\SoftGrocer\" 
     
    objConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _  
              "Data Source=" & strPathtoTextFile & ";" & _  
              "Extended Properties=""text;HDR=NO;FMT=Delimited""" 
     
    objRecordset.Open "SELECT distinct * FROM EDI894Stores.txt",objConnection         
    do until objRecordset.eof  
     
        fso.CopyFile strPathToTextFile & ObjRecordset(0) & "*.DAT" &","& strPathToTextFile & ObjRecordset(0) &".DAT" 
          
    objRecordset.MoveNext  
    loop  
     
    objConnection.close  
     
    set fso = nothing  
    set cn = nothing  
    set rs = nothing  
          
     
     

    Old COBOL programmer
    Wednesday, February 18, 2009 4:11 PM
  • Cant you run dos commands??

     Dim p As New Process
            p.StartInfo.FileName = "cmd.exe"
            p.StartInfo.Arguments = "/c copy " & "G:\SoftGrocer\*.DAT" & " " & "G:\SoftGrocer\9998.DAT"
            p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
            p.Start()


    Arjun Paudel
    Wednesday, February 18, 2009 4:32 PM
  • Arjun Paudel said:

    Cant you run dos commands??


     Dim p As New Process
            p.StartInfo.FileName = "cmd.exe"
            p.StartInfo.Arguments = "/c copy " & "G:\SoftGrocer\*.DAT" & " " & "G:\SoftGrocer\9998.DAT"
            p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
            p.Start()


    Arjun Paudel



    I'm running in VBScript.  I'll try a shell process, though.  Thanks for the tip.
    Old COBOL programmer
    Wednesday, February 18, 2009 4:56 PM
  • But this is not that kind of forum. It a within date forum for .net.

    Renee
    Wednesday, February 18, 2009 8:48 PM
  • Hi  Jay Nichols

    Welcome to Visual Basic General forum,
    This forum is for VB.NET questions only (e.g. Visual Basic 2003, Visual Basic 2005, Visual Basic 2008). 

    For VBScript questions, search for a variety of resources or try:
    VBScript Forums
    http://www.visualbasicscript.com/ (A Non-Microsoft Site)

    Best Regards
    Xingwei Hu


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Monday, February 23, 2009 6:35 AM