Question: I use the below script to move all the specific files to destination folder. I want to copy the files to destination folder without duplicating.

As I am going to schedule to run the VBS for every 10 minutes, it should not again copy the same files which are already present in the folder.

```
Set fso = CreateObject("Scripting.FileSystemObject")

testfolder = "D:\env"
'fso.CreateFolder(testfolder)

MoveFiles fso.GetFolder("D:\Movie Keys\License\2013_Apr_19")

Sub MoveFiles(fldr)
    For Each f In fldr.Files
        basename  = fso.GetBaseName(f)
        extension = fso.GetExtensionName(f)

        If LCase(extension) = "env" Then
            dest  = fso.BuildPath(testfolder, f.Name)
            count = 0
            Do While fso.FileExists(dest)
                count = count + 1
                dest  = fso.BuildPath(testfolder, basename & "_" & count & "." _
                        & extension)
            Loop
            f.Move dest
        End If
    Next

    For Each sf In fldr.SubFolders
        MoveFiles sf
    Next
End Sub

```

Answer: 
The problem here is that you want to copy all source files (including those in sub folders) into 1 destination folder. At this point there are chances that files will have the same file name and you may have files overwritten during the copy or files will be skipped as it will see it as a duplicate.

You're trying to achieve this with the following code

```
        count = 0
        Do While fso.FileExists(dest)
            count = count + 1
            dest  = fso.BuildPath(testfolder, basename & "_" & count & "." & extension)
        Loop

```

The problem is, you'll not know which file corresponds to which source file.

I suggest you rename the file as the full path. So, let's assume your source folder looks like:

![enter image description here](https://i.stack.imgur.com/WgjKv.png)

In both the FolderExample and sub01 there is a file called image01.jpg.

I've used the underscore here to separate each folder (which may or may not be realistic for you). But by creating this new file name it means you cannot overwrite any other file with the same name!

![enter image description here](https://i.stack.imgur.com/HmCK2.png)

Now, if you need to use the file again, the problem is the file name has changed. The good thing about using the underscore here is you can program another application to copy the file, split by underscore and then create the full path, get the file name and copy files accordingly.

This means, you can replace

```
    If LCase(extension) = "env" Then
        dest  = fso.BuildPath(testfolder, f.Name)
        count = 0
        Do While fso.FileExists(dest)
            count = count + 1
            dest  = fso.BuildPath(testfolder, basename & "_" & count & "." _
                    & extension)
        Loop
        f.Move dest
    End If

```

with

```
    If LCase(extension) = "env" Then
        dest  = fso.BuildPath(testfolder, f.Name)
        if not (fso.FileExists(dest)) then
            f.Move dest
        end if
    End If

```
If you want to synchronize folders without creating duplicates and without copying files that already exist in the destination, simply use `robocopy`:

```
robocopy "D:\Movie Keys\License\2013_Apr_19" "D:\env" *.env

```

Or in a [batch](/questions/tagged/batch "show questions tagged 'batch'") script:

```
@echo off

set dst=D:\env
set src=D:\Movie Keys\License\2013_Apr_19

robocopy "%src%" "%dst%" *.env

```

---

**Edit:** Since you want to mangle the source tree into the destination folder, `robocopy` won't work in your case. Your existing code is close to what you want, but since you don't want to overwrite existing files and also don't want to create "versioned" copies you need to change this:

```
count = 0
Do While fso.FileExists(dest)
    count = count + 1
    dest  = fso.BuildPath(testfolder, basename & "_" & count & "." _
            & extension)
Loop
f.Move dest

```

into this:

```
If Not fso.FileExists(dest) Then f.Move dest

```

---

For using various destination folders depending on the extension you could use a dictonary to store the destination paths:

```
Set testfolder = CreateObject("Scripting.Dictionary")
testfolder.CompareMode = vbTextCompare  'case-insensitive
testfolder.Add "env", "D:\env"
testfolder.Add "key", "D:\key"

```

and change your code like this:

```
If testfolder.Exists(extension) Then
  dest  = fso.BuildPath(testfolder(extension), f.Name)
  If Not fso.FileExists(dest) Then f.Move dest
End If

```