qualityip
asked on
Unable to merge folders using objFSO.MoveFolder in VBS
Here is the script I've created:
I'm receiving error number 58 stating that the folder already exists.
Here's an example from my log file:
Basically what I want the code to do is merge the two folders if a folder with the same name exists in the second directory without overwriting existing files.
Any ideas?
ForReading = 1
ForAppending = 8
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("c:\MoveM\move.txt", ForReading)
Do Until objTextFile.AtEndOfStream
On Error Resume Next 'Enable Error Handling so script doesn't fail
strFileLine = objTextFile.Readline
arrUserList = Split(strFileLine, ",")
strUser = arrUserList(0)
strNewName = arrUserList(1)
logFile="C:\MoveM\mergelog.txt"
sFolder = "D:\Data\" & strNewName
dFolder = "D:\Clients\"
If objFSO.FolderExists(sFolder) Then
objFSO.movefolder sFolder , dFolder
Set objFile = objFSO.OpenTextFile(logFile,ForAppending)
objFile.WriteLine sFolder & " moved to: " & dfolder & strNewName & vbCrLf
objFile.Close
Else
Set objFile = objFSO.OpenTextFile(logFile,ForAppending)
objFile.WriteLine sFolder & " doesn't exist and wasn't moved" & vbCrLf
objFile.Close
End If
If Err.Number = 0 Then 'Log errors
Set objFile = objFSO.OpenTextFile(logFile,ForAppending)
objFile.WriteLine sFolder & " Copied Successfully" & vbCrLf
objFile.Close
Else
Set objFile = objFSO.OpenTextFile(logFile,ForAppending)
objFile.WriteLine "------" & vbCrLf
objFile.WriteLine "Moving the folder " & sFolder & " experienced an error" & vbCrLf
objFile.WriteLine Err.Number & vbCrLf
objFile.WriteLine Err.Source & vbCrLf
objFile.WriteLine Err.Description & vbCrLf
objFile.WriteLine "------" & vbCrLf
objFile.Close
End If
loop
MsgBox "COMPLETED!"
I'm receiving error number 58 stating that the folder already exists.
Here's an example from my log file:
------
Moving the folder D:\Data\FolderName experienced an error
58
Microsoft VBScript runtime error
File already exists
------
Basically what I want the code to do is merge the two folders if a folder with the same name exists in the second directory without overwriting existing files.
Any ideas?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
omgang is correct. MoveFolder doesn't have "overwrite" abilities. You need to do one of the following methods:
a) use CopyFolder with the True parameter (as shown) and then run DeleteFolder on the original
b) run DeleteFolder on the destination first (if it exists), then use MoveFolder
Regards,
Rob.
a) use CopyFolder with the True parameter (as shown) and then run DeleteFolder on the original
b) run DeleteFolder on the destination first (if it exists), then use MoveFolder
Regards,
Rob.
ASKER
I had to modify the logic a little to fit my needs but using CopyFolders did the trick. Thank you!
http://msdn.microsoft.com/en-us/library/xbfwysex(v=vs.84).aspx
OM Gang