Copy file to parent folder and all subfolders

On my Windows 2003 server I have a parent folder with 200 subfolders. I want to be able to copy a file to the parent folder and have the same copy replicated to all of the subfolders. Does anyone know of a utility or script to accomplish this?
solidrumorAsked:
Who is Participating?
 
netsmithcentralCommented:
That error means your passing in an invalid file or folder name to the script.  It will be looking for either a relative path from the directory in which the script executes, or an absolute path on the system.

EXAMPLE: Script saved on the Desktop (C:\Documents and Settings\User\Desktop).  File to copy on Desktop, named "myfile.txt".  Destination folder on the desktop "myfolder".

   File Path: myfile.txt    OR    .\myfile.txt
   Folder Path: myfolder    OR     .\myfolder


EXAMPLE 2: Script saved on the Desktop (C:\Documents and Settings\User\Desktop).  File to copy C:\mydata\myfile.txt.  Destination folder C:\myotherdata.

   File Path: \mydata\myfile.txt
   Folder Path: \myotherdata
0
 
Brian PiercePhotographerCommented:
I have to ask - why?
It seems an inefficient use of disk space.
0
 
netsmithcentralCommented:
Copy the text below into a text file called "subcopy.vbs".  Run the command as follows: subcopy [file] [destination].  So if you wanted to copy the file "myData.txt" to every folder in the path "\myFolder", you would use subcopy myData.txt \myFolder.



Option Explicit

If WScript.Arguments.Count <> 2 Then
    WScript.Echo("You must supply the destination folder tree and filename to copy.")
    WScript.Quit(0)
End If

Dim fname, dest
fname = WScript.Arguments.Item(0)
dest = WScript.Arguments.Item(1)

Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")

Dim root, subfolder
Set root = objFSO.GetFolder(dest)

If objFSO.FileExists(root.Path & "\" & fname) Then
    objFSO.CopyFile ".\" & fname, root.Path
End If

For Each subfolder In root.SubFolders
    If Not objFSO.FileExists(subfolder.Path & "\" & fname)
        objFSO.CopyFile ".\" & fname, subfolder.Path
    End If
Next

Set root = Nothing
Set objFSO = Nothing
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Stephen MandersonSoftware EngineerCommented:
Why do you need to duplicate these folders? Are they going onto another drive or for backup purposes?

You would more than likely want to be using xcopy or robocopy in order to copy the effective permissions.
And run a scheduled task in order to do the copy incl folder & file permissions.

See the robocopy link below as it has a GUI to allow you to create a copy script easily.

XCopy
http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/xcopy.mspx?mfr=true

RoboCopy
http://www.microsoft.com/technet/technetmag/issues/2006/11/UtilitySpotlight/

0
 
solidrumorAuthor Commented:
The script is giving me an error on line:22 char:9 permission denied code:800A0046. Do you know what might be causing the error? For testing I simply created a root folder with 3 sub folders. I ran subcopy mydata.txt c:\rootfolder when the error occured.

My process calls for each sub folder to contain the same file as the parent folder.
0
 
netsmithcentralCommented:
Sorry!  I forgot to append a trailing slash on the destination path name (its a stupid MS requirement).   Here's an updated script:

Option Explicit

If WScript.Arguments.Count <> 2 Then
    WScript.Echo("You must supply the destination folder tree and filename to copy.")
    WScript.Quit(0)
End If

Dim fname, dest
fname = WScript.Arguments.Item(0)
dest = WScript.Arguments.Item(1)

Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")

Dim root, subfolder
Set root = objFSO.GetFolder(dest)

If Not objFSO.FileExists(root.Path & "\" & fname) Then
    objFSO.CopyFile ".\" & fname, root.Path & "\"
End If

For Each subfolder In root.SubFolders
    If Not objFSO.FileExists(subfolder.Path & "\" & fname) Then
        objFSO.CopyFile ".\" & fname, subfolder.Path & "\"
    End If
Next

Set root = Nothing
Set objFSO = Nothing
0
 
solidrumorAuthor Commented:
Great it works now copies files as needed. How difficult would it be to create a simple GUI front end? Something basic where one field would allow me to browse for the source file and another field where I could browse for the destination folder. Finally a submit button to execute the subcopy.vbs script. Please let me know if the script could be easily modified to give the added functionality.
0
 
netsmithcentralCommented:
Not with VB Script...  It's a very limited language.  I mean, I could create a dialog box to prompt for the source/destination, but it would be as a text, not a "Browse Files..." window.  Doing that would require writing and compiling an actual program.  Let me know if you want the dialog boxes.
0
 
solidrumorAuthor Commented:
Thanks for the reply if you would please add the extra lines of script I would like to give it a try. What of the reverse effect meaning after I execute the script and copy a file to the subfolders. Can the script be modified to erase a file from each of the folders?
0
 
netsmithcentralCommented:
Alright, here's a version that will accept command line parameters OR, if they're not supplied prompt the user for input.  I've also included a "subdelete.vbs" file after this first one.

'subcopy.vbs
Option Explicit

Dim fname, dest
If WScript.Arguments.Count <> 2 Then
    fname = InputBox("Please enter the path to the file to copy.", "File Path")
    dest = InputBox("Please enter the path to copy the file to.", "Destination")
Else
    fname = WScript.Arguments.Item(0)
    dest = WScript.Arguments.Item(1)
End If

Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")

If Not objFSO.FileExists(fname) Or Not objFSO.FolderExists(dest) Then
    WScript.Echo("Invalid file name or destination path!")
    WScript.Quit(1)
End If

Dim root, subfolder
Set root = objFSO.GetFolder(dest)

If Not objFSO.FileExists(root.Path & "\" & fname) Then
    objFSO.CopyFile ".\" & fname, root.Path & "\"
End If

For Each subfolder In root.SubFolders
    If Not objFSO.FileExists(subfolder.Path & "\" & fname) Then
        objFSO.CopyFile ".\" & fname, subfolder.Path & "\"
    End If
Next

Set root = Nothing
Set objFSO = Nothing



'subdelete.vbs
Option Explicit

Dim fname, dest
If WScript.Arguments.Count <> 2 Then
    fname = InputBox("Please enter the name of the file to delete.", "File Name")
    dest = InputBox("Please enter the path to delete the file from.", "Destination")
Else
    fname = WScript.Arguments.Item(0)
    dest = WScript.Arguments.Item(1)
End If

Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")

If Not objFSO.FolderExists(dest) Then
    WScript.Echo("Invalid deletion path!")
    WScript.Quit(1)
End If

Dim root, subfolder
Set root = objFSO.GetFolder(dest)

If objFSO.FileExists(root.Path & "\" & fname) Then
    objFSO.DeleteFile root.Path & "\" & fname, True
End If

For Each subfolder In root.SubFolders
    If objFSO.FileExists(subfolder.Path & "\" & fname) Then
        objFSO.DeleteFile subfolder.Path & "\" & fname, True
    End If
Next

Set root = Nothing
Set objFSO = Nothing
0
 
solidrumorAuthor Commented:
Sorry for the delay in my response. I ran the revised subcopy program and I'm receiving an error. Line:22 Char:5  Error:bad file name or number code: 800a0034. Can you help with this?
0
 
solidrumorAuthor Commented:
Okay I followed your direction but still receive the error line:22 char:5 error:path not found code:800a004c
I have a test file at the root of c:\ I want to copy the test file to all sub folders at c:\test. When executing subcopy from the desktop my source file is \testfile.txt my destination path is \test
 I still receive the error can you assist further please?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.