Copy folder contents using VBScript

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

The goal is to list custom user groups and then copy the contents of a specific folder based on those groups.

I have some code that 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 was cobbled together from various sources and I'm sure most of them are correct, I just can't get it to work completely.

Any help would be great.

Greetings.

+2


source to share


3 answers


Delete

On Error Resume Next

      

from your function and then you will see the errors that happen.



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

Set objFolder = objFSO.GetFolder(source)

      

0


source


Try to remove the second \ in the copy statement

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

      



Also you can use file object copy method like this

For Each file in objFolder.Files
   file.Copy target & file.name
Next

      

0


source


The target folder must not exist, but it must be specified:

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

The folder where the new folder will be located must exist:

C: \ A must exist, but C: \ A \ Z is optional.

Trailing slashes must be omitted:

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

0


source







All Articles