tags:

views:

484

answers:

3

I am trying to copy the contents of certain folders to another folder using VBScript.

The goal is to enumerate a user's AD groups and then copy specific folder content based on those groups.

I have code, which is currently not working.

Dim Group,User,objFSO,objFolder,source,target,StrDomain

StrDomain = "domain.local"
FolderBase = "\\domain.local\netlogon\workgrps\icons"
Set net = CreateObject("wscript.network")
Struser = net.username
target = "\\fs1\users\"&net.username&"\Desktop\AppIcons\"

DispUserInWhichGroup()

Function DispUserInWhichGroup()

On Error Resume Next

Set objFSO=CreateObject("Scripting.FileSystemObject")
Set User = GetObject("WinNT://" & strDomain & "/" & strUser & ",user")

For Each Group In User.Groups

source = FolderBase & Group.name

Set objFolder = GetFolder(source)

For Each file in objFolder.Files
objFSO.CopyFile source &"\"& file.name, target&"\"&file.name
Next

Next

End Function

This has been cobbled together from various sources, and I'm sure most of it is right, I just can't get it working completely.

Any assistance would be great.

Cheers.

A: 

Try removing the second \ in the copy statement

For Each file in objFolder.Files
   objFSO.CopyFile source & "\" & file.name, target & file.name
Next

Also you could use the file objects copy method like this

For Each file in objFolder.Files
   file.Copy target & file.name
Next
Tester101
Thanks, but I can't get it to work. I tried both. I can't even get it to echo the file name within the loop using wscript.echo(file.name) which makes me think I have bigger issues. I wish I had some errors being thrown, at least then I'd have something to go on. There are definitely files in the folders, I'm really not sure what's going wrong.
LukeR
A: 

Remove the

On Error Resume Next

line from your function and then you will see any errors that are happening.

EDIT: I think you need to specify an object for GetFolder.

Set objFolder = objFSO.GetFolder(source)
aphoria
Great. Thankyou. Now I'm getting a type mismatch error for GetFolder. At least I can figure out waht's happening now.
LukeR
Thanks, I've got it working now.
LukeR
A: 

The destination folder does not have to exist, but it must be specified:

C:\Z to C:\A\Z will work, but C:\Z to C:\A will not work.

The folder that the new folder will be in must exist:

C:\A must exist, but C:\A\Z does not have to.

The trailing slashes should be omitted:

C:\Z to C:\A\Z will work but C:\Z\ to C:\A\Z\ will not work.

quokwok