Solved

Unable to merge folders using objFSO.MoveFolder in VBS

Posted on 2013-02-05
4
866 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

752 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