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

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1179
  • Last Modified:

Move files from one location to another mirroring folder structure

Hi ,

I am looking for a VB Scrip that would move all files from the source to the target, including all subfolders, down as many levels as exist.

Also if the sub folders do not already exist in the target directory it should create them.

e.g.

source=c:\testing
target=c:\target

in source is
c:\testing\folder1\some files
c:\testing\folder1\anotherfolder\morefolders\ some other files
c:\testing\folder3

Then all the folder structure would be mirrored onto c:\target (creating any directories which do not already exist)
And then all files moved from the source

Any help greatly appreciated

Cheers
0
arundelr
Asked:
arundelr
  • 5
  • 4
1 Solution
 
SLafferty1983Commented:
I don't know vbscripting very well, but you can use Robocopy. It will do the same thing you want. You can find out more about it here http://technet.microsoft.com/en-us/magazine/cc160891(TechNet.10).aspx
0
 
RobSampsonCommented:
Yes, or you could just delete the target folder, then move the source folder.....

http://msdn.microsoft.com/en-us/library/465s5y8s(VS.85).aspx

Regards,

Rob.
strSource = "C:\Temp\Scripts\Source"
strDestination = "C:\Temp\Scripts\Destination"
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.DeleteFolder strDestination, True
objFSO.MoveFolder strSource, strDestination

Open in new window

0
 
arundelrAuthor Commented:
Hi Rob,

Thanks very much for the suggestion.

Unfortunately I cannot delete the target folder first because it may already contain some folders and files I do not want to loose.

Do you haveany other suggestions?

cheers

Rob
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
RobSampsonCommented:
Sure....we'll use XCopy to do the copying, then there's a line in the script that is this:
'objFSO.DeleteFolder strSource, True

which will then delete the Source folder, if you uncomment that line.

Regards,

Rob.
strSource = "C:\Temp\Temp\Test Script\Test"
strDestination = "C:\Temp\Temp\Test Script\Test2"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
strSource = objFSO.GetFolder(strSource).ShortPath
If objFSO.FolderExists(strDestination) = False Then CreateLocalDirectoryStructure(strDestination)
strDestination = objFSO.GetFolder(strDestination).ShortPath
strCommand = "cmd /c xcopy " & strSource & " " & strDestination & " /E /C /I /Q /H /R /K /Y /Z"
objShell.Run strCommand, 0, True
'objFSO.DeleteFolder strSource, True
MsgBox "Done"
 
Sub CreateLocalDirectoryStructure(strPath)
	If Right(strPath, 1) = "\" Then strPath = Left(strPath, Len(strPath) - 1)
	arrBits = Split(strPath, "\")
	strSubPath = arrBits(0)
	If UBound(arrBits) > 0 Then
		For intBit = 1 To UBound(arrBits)
			strSubPath = strSubPath & "\" & arrBits(intBit)
			If objFSO.FolderExists(strSubPath) = False Then objFSO.CreateFolder(strSubPath)
		Next
	End If
End Sub

Open in new window

0
 
arundelrAuthor Commented:
HI Rob,

Thats almost perfect but what I really want to do is after the successful copy delete the source "files" but leave the folder structure intact.
0
 
RobSampsonCommented:
OK, so we'll run
del /s /q C:\Folder\*.*
after the xcopy then....

Try this on a copy of the source folder just in case things go pear shaped.  It WILL delete files, so make sure you specify the right path.

Regards,

Rob.
strSource = "C:\Temp\Temp\Test Script\Test"
strDestination = "C:\Temp\Temp\Test Script\Test2"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
strSource = objFSO.GetFolder(strSource).ShortPath
If objFSO.FolderExists(strDestination) = False Then CreateLocalDirectoryStructure(strDestination)
strDestination = objFSO.GetFolder(strDestination).ShortPath
strCommand = "cmd /c xcopy " & strSource & " " & strDestination & " /E /C /I /Q /H /R /K /Y /Z"
objShell.Run strCommand, 0, True
strCommand = "cmd /c del /s /q " & strSource & "\*.*"
objShell.Run strCommand, 0, True
MsgBox "Done"
 
Sub CreateLocalDirectoryStructure(strPath)
	If Right(strPath, 1) = "\" Then strPath = Left(strPath, Len(strPath) - 1)
	arrBits = Split(strPath, "\")
	strSubPath = arrBits(0)
	If UBound(arrBits) > 0 Then
		For intBit = 1 To UBound(arrBits)
			strSubPath = strSubPath & "\" & arrBits(intBit)
			If objFSO.FolderExists(strSubPath) = False Then objFSO.CreateFolder(strSubPath)
		Next
	End If
End Sub

Open in new window

0
 
RobSampsonCommented:
Actually, we'll need to enclose the source folder in quotes....use this instead.

Rob.
strSource = "C:\Temp\Temp\Test Script\Test"
strDestination = "C:\Temp\Temp\Test Script\Test2"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
strSource = objFSO.GetFolder(strSource).ShortPath
If objFSO.FolderExists(strDestination) = False Then CreateLocalDirectoryStructure(strDestination)
strDestination = objFSO.GetFolder(strDestination).ShortPath
strCommand = "cmd /c xcopy """ & strSource & """ """ & strDestination & """ /E /C /I /Q /H /R /K /Y /Z"
objShell.Run strCommand, 0, True
strCommand = "cmd /c del /s /q """ & strSource & "\*.*"""
objShell.Run strCommand, 0, True
MsgBox "Done"
 
Sub CreateLocalDirectoryStructure(strPath)
	If Right(strPath, 1) = "\" Then strPath = Left(strPath, Len(strPath) - 1)
	arrBits = Split(strPath, "\")
	strSubPath = arrBits(0)
	If UBound(arrBits) > 0 Then
		For intBit = 1 To UBound(arrBits)
			strSubPath = strSubPath & "\" & arrBits(intBit)
			If objFSO.FolderExists(strSubPath) = False Then objFSO.CreateFolder(strSubPath)
		Next
	End If
End Sub

Open in new window

0
 
arundelrAuthor Commented:
Brilliant - Thanks Rob
0
 
arundelrAuthor Commented:
Hi Rob,

Thats great - thanks for all your help!

Rob
0
 
RobSampsonCommented:
No problem. Thanks for the grade.

Regards,

Rob.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now