Solved

Unable to merge folders using objFSO.MoveFolder in VBS

Posted on 2013-02-05
4
849 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

863 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now