Code to Copy Files

esskaykay

Registered User.
Local time
Today, 17:29
Joined
Mar 8, 2003
Messages
267
I have a batch file I shell to which copies files. Rather than shelling out, I’d like to code the "copy" operation directly in my VBA routine. I’m aware of the FileCopy command but was wondering if it’s possible to copy only newer files similar to the /d/y switch in my batch code? Also, does it work with wildcards (...\*.*)?

Following is a small sample of the batch file.

c:
cd \cad_files\map
xcopy z:\map\base.dgn /d /y
xcopy z:\map\parcels.dgn /d /y
xcopy z:\plans\*.* c:\cad_files\plans\*.* /s /e /d /y

Is this possible?
Thanks,
SKK
 
Try "/M" on the xcopy command. That checks the archive flag (which is set when a file is "touched") and resets the archive flag. The first time, I imagine all the files will have the archive flag set. After that, only files that have changed will have it set.
 
George,

I'm not trying to recode the batch file but rather what would the code be if I was to include it in the module?

FileCopy "z:\map\base.dgn", "c:\cad_files\*.*" /M

SKK
 
I doubt that would work.

Since I always do this type of stuff in batch files, I'm probably not the best to answer exactly how to do it in VB (it doesn't look like Dir() has an Archive flag attribute...wrong, I found an example...see below). But generally, you'll check the Archive flag, if it is set, copy the file and reset the Archive flag.

Here is some code I got from help to clear the archive flag. You should be able to adapt this. You'll notice that the archive "bit" is 32.
Code:
Sub SetClearArchiveBit(filespec)
    Dim fs, f, r
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFile(fs.GetFileName(filespec))
    If [B]f.attributes[/B] and 32 Then
        r = MsgBox("The Archive bit is set, do you want to clear it?", vbYesNo, "Set/Clear Archive Bit")
        If r = vbYes Then 
            [B]f.attributes[/B] = [B]f.attributes[/B] - 32
            MsgBox "Archive bit is cleared."
        Else
            MsgBox "Archive bit remains set."
        End If
    Else
        r = MsgBox("The Archive bit is not set. Do you want to set it?", vbYesNo, "Set/Clear Archive Bit")
        If r = vbYes Then 
            [B]f.attributes[/B] = [B]f.attributes[/B] + 32
            MsgBox "Archive bit is set."
        Else
            MsgBox "Archive bit remains clear."
        End If
    End If
End Sub

Does that help?
 
Thanks George - I'll try and let you know how it worked out. I don't need to change the attribute - that happens via our CAD program. All I have to do is copy the files that have been modified since the last update.

These files are being copied to a laptop so our inspectors in the field have the latest and greatest drawings.


SKK
 
Simple Software Solutions

If you are attempting to evaluate if a file needs copying or not you first need something to compare it with. Lets say you copied a file yesterday to a known location, then today you come accross the same file in the source location, now you have the two elements of the equasion. If you use the FileSystemObject to compare the DateLastModified you will be able to detect if the file needs copying or not.

Here is a snippet of code I use to populate a custom list box with the contents of a folder. Look at the part where it retrieves the date last modified

Code:
Function FSOFindFiles(sFol As String, sFile As String)

Dim tFld As Folder, tFil As File, FileName As String
Dim DLM As String, tPath As String
Dim fld As Folder, fType As String


Dim FSO As New FileSystemObject, FileInfo As File
   
   On Error GoTo Catch
   If FSO.FolderExists(sFol) Then
   Set fld = FSO.GetFolder(sFol)
   FileName = Dir(FSO.BuildPath(fld.Path, sFile), vbNormal Or vbReadOnly)
    
   While Len(FileName) <> 0
      FindFile = FindFile + FileLen(FSO.BuildPath(fld.Path, FileName))
      tPath = FSO.BuildPath(fld.Path, FileName)
        Set FileInfo = FSO.GetFile(tPath)
        [B]DLM = CStr(FileInfo.DateLastModified)[/B]        
        fType = CStr(FileInfo.Type)
        
    If IsLoaded("FrmWzdMainDetails") Then
        If Right(FileName, 3) <> "tmp" Then
            FrmWzdMainDetails.ctList1.AddItem FileName + ";" + DLM + ";" + fType
        End If
    End If
      FileName = Dir()
      DoEvents
   Wend
   End If
   Exit Function
Catch:  FileName = ""
       Resume Next
End Function

Hope this helps

CodeMaster::cool:
 

Users who are viewing this thread

Back
Top Bottom