Solved

Unable to merge folders using objFSO.MoveFolder in VBS

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

758 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

21 Experts available now in Live!

Get 1:1 Help Now