Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Unable to merge folders using objFSO.MoveFolder in VBS

Posted on 2013-02-05
4
Medium Priority
?
911 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 2000 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
When you discover the power of the R programming language, you are going to wonder how you ever lived without it! Learn why the language merits a place in your programming arsenal.
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 a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

926 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