Computing.Net > Forums > Programming > Script to copy and delete existing files

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Script to copy and delete existing files

Reply to Message Icon

Name: baidwan
Date: October 1, 2009 at 15:04:21 Pacific
OS: Windows XP
CPU/Ram: 4gb
Subcategory: Batch
Tags: script, vb, DELETE, Copy, overwrite
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: baidwan
Date: October 1, 2009 at 15:15:57 Pacific
Reply:

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


0

Response Number 2
Name: Razor2.3
Date: October 2, 2009 at 05:37:29 Pacific
Reply:

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?


0

Response Number 3
Name: baidwan
Date: October 2, 2009 at 06:17:55 Pacific
Reply:

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,


0

Response Number 4
Name: Razor2.3
Date: October 2, 2009 at 07:00:17 Pacific
Reply:

So if the file exists in both folders, delete both copies, otherwise copy from the first folder to the second?


0

Response Number 5
Name: baidwan
Date: October 2, 2009 at 07:03:12 Pacific
Reply:

Yes. Spot on


0

Related Posts

See More



Response Number 6
Name: Razor2.3
Date: October 2, 2009 at 08:32:12 Pacific
Reply:

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


0

Response Number 7
Name: baidwan
Date: October 2, 2009 at 09:27:33 Pacific
Reply:

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,


0

Response Number 8
Name: Razor2.3
Date: October 2, 2009 at 09:42:17 Pacific
Reply:

*sigh*

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


0

Response Number 9
Name: baidwan
Date: October 2, 2009 at 10:03:00 Pacific
Reply:

Thank you very much. Once again you are a life saver


0

Sponsored Link
Ads by Google
Reply to Message Icon





Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: Script to copy and delete existing files

script to copy specify date file to www.computing.net/answers/programming/script-to-copy-specify-date-file-to/17662.html

VB Script needed to Copy files www.computing.net/answers/programming/vb-script-needed-to-copy-files/14942.html

batch script to copy current date f www.computing.net/answers/programming/batch-script-to-copy-current-date-f/14097.html