1

I am trying to copy file using following script.

Option Explicit

Const ForWriting = 2
Dim objFSO
Dim desfolder
Dim oShell
dim s

desfolder = "D:\Databases"
Set objFSO = CreateObject("Scripting.FileSystemObject")

Recurse objFSO.GetFolder("D:\Databases\Images")

Sub Recurse(objFolder)
    Dim objFile, objSubFolder

    For Each objFile In objFolder.Files
        If LCase(objFSO.GetExtensionName(objFile.Name)) = "tif" Then
            s = Right(objFile.Name, 10)
            S = Left(s, 1)
            If Left(s, 1) = "C" Then
                Set oShell = WScript.CreateObject("WScript.Shell")
                oShell.Run "xcopy.exe " & objFile & " " & desfolder & " /R /Y", _
                    0, True
            End If
        End If
    Next
    For Each objSubFolder In objFolder.SubFolders
        Recurse objSubFolder
    Next
End Sub

What I am trying to do is checking files in folders and subfolders. If file is tif then checking weather it contains required letter "C" at specific position. and using xcopy to copy the file.

It works fine but is very slow. Is there any faster method to do this?

Edit: What I want exactly is to find c*.tif in a folder and its subfolders.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Rahul
  • 10,830
  • 4
  • 53
  • 88

1 Answers1

2

Don't use xcopy then. Doing it your way spawns a new process each time you copy a file. Just use the file object's Copy method and be done with it.

Sub Recurse(objFolder)
    Dim objFile, objSubFolder

    For Each objFile In objFolder.Files
        If LCase(objFSO.GetExtensionName(objFile)) = "tif" Then
            If LCase(Left(objFile.Name, 1) = "c" Then
                objFile.Copy desfolder & "\"
            End If
        End If
    Next
    For Each objSubFolder In objFolder.SubFolders
        Recurse objSubFolder
    Next
End Sub

Note that your destination path requires a trailing backslash because it's a folder (you'd get a "permission denied" error without it). Otherwise you need to specify the destination path including the filename, e.g. like this:

objFile.Copy objFSO.BuildPath(desfolder, objFile.Name)
Community
  • 1
  • 1
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328