Copy one file into two locations

Posted on 2008-06-17
Last Modified: 2012-05-05
I'm trying to write a simple VBScript to copy one file into two different folders.  The script works fine when broken into two separate scripts (one file one destination location) but throws an error message when combined (one file two destination locations).  The script I'm currently using is posted below.  
Thanks in advance.

Const OverwriteExisting = True


Set objFSO = CreateObject("Scripting.FileSystemObject")

objFSO.CopyFile "FileName", "C:\Destination1\", OverwriteExisting

objFSO.CopyFile "FileName","C:\Destination2\", OverwriteExisting

Open in new window

Question by:BalchBingham
  • 4
  • 3
LVL 24

Accepted Solution

purplepomegranite earned 250 total points
ID: 21804728
What is the error?  There shouldn't be anything wrong with that code as it stands, so long as you are not deleting the source file between copy statements (which you aren't in the above code).  CopyFile does exactly that.
LVL 24

Assisted Solution

purplepomegranite earned 250 total points
ID: 21804782
In fact I've just tested your code snippet, and it works fine - if there is an error it is caused by another issue - does the target file exist in one of the locations, and is it open (i.e. locked) by an application?  This is an example of what would cause an error.

Author Comment

ID: 21805074
The error given is a Runtime Error stating Permission Denied.  I am logged in as a Domain Admin so I should have adequate permission to run this.  I've also tried running it with the System Account; same error.  It runs without problem when it is only copying the file to one location, it's when it's copying to two locations that I'm having problems.
The target file does already exist in one location, but with the OverWrite command it shouldn't be an issue.  The file to be Over Written (a MS Word template file) is not in use.
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

LVL 24

Assisted Solution

purplepomegranite earned 250 total points
ID: 21805219
If you are getting the permission denied error for the location in which the file does exist, then the file is not intrinsically writable.  This could be for a number of reasons:

- You don't have permission.  Just because you are logged in as an administrator doesn't mean you by default have permission to all files.  You need to actually check the permissions on the file.
- The file is on a share and is open by another user.
- The locked status of the file has been left by a hung application (e.g. Word crashes).  A system restart normally fixes this.

Author Comment

ID: 21805379
I think you're on to something and I really appreciate your help.  I checked the security permission for not only the file but the folder itself and granted All Users Full Control.  I'm still getting the same error.  If I delete the file and then run the script it works great - once.  It will not OverWrite the file, gives me the same Permission Denied error.
I suppose I could have the script delete the pre-existing file and then copy in the new one, but in essence isn't that what Overwritting is doing?  And why would I have permission to delete the file but not copy over it?  
Being able to delete the file shows the file isn't in use (or hung).  The file is located in my local directory and is not being shared.  Good ideas though.

Author Comment

ID: 21805491
Figured it out!  Was trying the script on test boxes that had freshly been imaged.  The file I was attempting (and failing) to copy down is a MS Word Template file.  Once Word was opened for the first time, the files copied as would be expected.  Not entirely sure why Word had to complete it's RunOnce sequence, but as long as it works now I'm happy.
Thanks for all of your help!

Author Closing Comment

ID: 31468022
Thank you so much for taking the time to help me.  I had been banging my head against the wall, couldn't figure out why it wasn't working.  Turned out to be right in front of my face.  Thank you!!

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
base64 decode encode 12 98
Microsoft C++ code failing in executable that worked 9 56
recursion example 16 70
Hide vba in gp 7 43
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
A short article about problems I had with the new location API and permissions in Marshmallow
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now