0

I'm trying to create a VBS that's supposed to iterate through a directory that has folders in it, these folders have files in them. The folders are named after users, I want to iterate through the user folders and copy the files in them to a another folder that is also named after the user. After copying the files to the destination, it should also delete the copied files from the first folder.

This is what I currently have:

Set fso = CreateObject("Scripting.FileSystemObject")
Set fldr = fso.GetFolder("C:\Users\user\Desktop\folder")
Set fldrsub = fldr.SubFolders
For Each file In fldrsub
    Set firstPath = file
    myPath = Mid(firstPath,34,25)
    RTrim(myPath)
    WScript.Echo(myPath)
    Set pathTwo = fso.GetFolder("C:\Users\user\Desktop\" & myPath & "\data\")
    For Each oneFile In file.Files
        WScript.Echo oneFile
        oneFile.Copy(pathTwo)   
    Next        
Next

It's supposed iterate through the folders, then trim the user name from the folder path, loop inside that folder and copy all the files to a destination based on the username.

I'm receiving an error "Permission Denied" (800A0046) that points to the oneFile.Copy(pathTwo) command, I did research on it and found that the it could be caused by a permission issue or missing a backslash on a folder destionation. After checking out my permissions and checking the folder paths I still couldn't get it to work, though.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • 1
    pathtwo should be a string. `pathTwo="C:\Users\user\Desktop\" & myPath & "\data\"`. –  Jul 19 '16 at 08:37
  • 1
    While the default property of a folder **object** is its path, the returned **string** has no trailing backslash. – Ekkehard.Horner Jul 19 '16 at 09:08
  • Thanks for the replies, sorry for the duplicate. I did read that other existing question, it just didn't resolve my problem so I decided to ask the question myself, even though it was very similar. Also got it working, thanks to the replies. – Teemu Rytsölä Jul 20 '16 at 06:00

0 Answers0