Solved

Unable to merge folders using objFSO.MoveFolder in VBS

Posted on 2013-02-05
4
859 Views
Last Modified: 2013-02-26
Here is the script I've created:

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!"

Open in new window


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

------

Open in new window


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?
0
Comment
Question by:qualityip
  • 2
4 Comments
 
LVL 28

Accepted Solution

by:
omgang earned 500 total points
ID: 38856008
It seems the MoveFolder method will always generate an error if the destination already exists.  You may have to test for the existence of the destination folder and use the CopyFolder method if the destination forlder already exists.  The CopyFolder method includes an optional property/switch for Overwrite.

Something like

    If objFSO.FolderExists(sFolder) Then
        Select Case objFSO.FolderExists(dFolder & strNewName)
           Case True
                objFSO.CopyFolder(sFolder, dFolder, True)

           Case False
                objFSO.MoveFolder(sFolder, dFolder)

        End Select

          'objFSO.movefolder sFolder , dFolder
          Set objFile = objFSO.OpenTextFile(logFile,ForAppending)
          objFile.WriteLine sFolder & " moved to: " & dfolder & strNewName & vbCrLf
          objFile.Close
    Else
0
 
LVL 28

Expert Comment

by:omgang
ID: 38856014
see here for more on CopyFolder method.
http://msdn.microsoft.com/en-us/library/xbfwysex(v=vs.84).aspx

OM Gang
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 38857285
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.
0
 

Author Closing Comment

by:qualityip
ID: 38930906
I had to modify the logic a little to fit my needs but using CopyFolders did the trick. Thank you!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question