Script to copy and delete existing files

October 1, 2009 at 15:04:21
Specs: Windows XP, 4gb
Hey Fellas,

I am Stuck at another corner. I have pdf files in one folder and same files in second folder. When i add new files to folder one and run a script to copy all the files over to second folder. it should overwrite the existing files in second folder and copy the newly added files to the third folder. Any idea how to do that. Thankyou in advance for your help.

This is what i have so far..

Const OverwriteExisting = TRUE
Set objFSO = CreateObject("Scripting.FileSystemObject")

With CreateObject("Scripting.FileSystemObject")
For Each file In .GetFolder("C:\Documents and Settings\gbaidwan\Desktop\new\").Files

objFSO.CopyFile "C:\Documents and Settings\gbaidwan\Desktop\new\*.pdf" , "C:\Documents and Settings\gbaidwan\Desktop\new1\", TRUE

name = Left(.GetBaseName(file),7) & "." & .GetExtensionName(file)
If name <> file.Name Then
If Not .FileExists("C:\Documents and Settings\gbaidwan\Desktop\new\" & name) Then
file.Delete
Else
file.Name = name
End If
End If
Next 'file
End With


See More: Script to copy and delete existing files

Report •


#1
October 1, 2009 at 15:15:57
Const DeleteExisting = TRUE


Set objFSO = CreateObject("Scripting.FileSystemObject")

objFSO.CopyFile "C:\Documents and Settings\gbaidwan\Desktop\new\*.pdf" , "C:\Documents and Settings\gbaidwan\Desktop\new1\", TRUE


would this work with some modification? its giving me error right now


Report •

#2
October 2, 2009 at 05:37:29
I'm not sure what you want here. You want to take all the PDF's in one folder, copy to a second folder, and make another copy in a third folder, while disregarding whatever was in the second and third folder?

Report •

#3
October 2, 2009 at 06:17:55
No

I wanna copy files from one folder to second and if the files already exist in the second folder then delete both files.

For example:

Folder one has: 4 files
a.pdf
b.pdf
c.pdf
d.pdf

and Second folder has: 3 files
a.pdf
b.pdf
c.pdf

when the copy and delete script is run

Second Folder should be left with only one file:

d.pdf

because it didn't copy the existing files to second folder. Does that help you understand it better?

Thanks,


Report •

Related Solutions

#4
October 2, 2009 at 07:00:17
So if the file exists in both folders, delete both copies, otherwise copy from the first folder to the second?

Report •

#5
October 2, 2009 at 07:03:12
Yes. Spot on

Report •

#6
October 2, 2009 at 08:32:12
Given the choice, this is something I'd do in a batch file; file structure manipulation is one thing it does well.

Untested VBScript:

Const sourceDir = "c:\folder one\"
Const destDir = "C:\folder two\"

With CreateObject("Scripting.FileSystemObject")
  For Each file In .GetFolder(sourceDir).Files
    If LCase(.GetExtensionName(file)) = "pdf" Then 
      If .FileExists(destDir & file.Name) Then
        .DeleteFile destDir & file.Name
        file.Delete
      Else
        file.Copy destDir
      End If
    End If
  Next 'file
End With


Report •

#7
October 2, 2009 at 09:27:33
As always i really appreciate your help. The script is giving me an error though

Error: Object doesn't support this property or method: 'Copy'

in this line

.Copy destDir

Thanks,


Report •

#8
October 2, 2009 at 09:42:17
*sigh*

Well, i did say it was untested. That line should read file.Copy destDir


Report •

#9
October 2, 2009 at 10:03:00
Thank you very much. Once again you are a life saver

Report •


Ask Question