r/vbscript • u/mschepac • Jan 23 '23
A simple script to copy files modified today to several folders. I keep getting 'Permission Denied' 800A0046 errors even though I am the administrator. Any thoughts?
Set objFSO = CreateObject("Scripting.FileSystemObject")
strFolderToCopyFrom = "C:\Users\Mark2\Desktop\Test"
strCopyTo1 = "C:\Users\Mark2\Desktop\Test\1"
strCopyTo2 = "C:\Users\Mark2\Desktop\Test\2"
strCopyTo3 = "C:\Users\Mark2\Desktop\Test\3"
Set objFolder = objFSO.GetFolder(strFolderToCopyFrom)
For Each objFile In objFolder.Files
If DateDiff("d", objFile.DateLastModified, Now) = 0 Then
objFSO.CopyFile objFile.Path, strCopyTo1
objFSO.CopyFile objFile.Path, strCopyTo2
objFSO.CopyFile objFile.Path, strCopyTo3
End If
Next
2
Upvotes
1
u/jcunews1 Jan 24 '23
CopyFileby default, will not overwrite any existing file in destination folder. If an existing file is already exist andCopyFile's 3rd argument is nottrue, it will throw an exception error.1
u/hackoofr Feb 03 '23
This is the modified code :
First checks if the source folder specified in "strFolderToCopyFrom" exists using "objFSO.FolderExists".
If it exists, it continues with the loop to copy the files. If it doesn't exist, a message box is displayed to inform the user.
The code also checks if the 3 destination folders specified in "strCopyTo1", "strCopyTo2", and "strCopyTo3" exist using "objFSO.FolderExists".
If any of the folders don't exist, it creates them using "objFSO.CreateFolder".