Solved

Move files from one location to another mirroring folder structure

Posted on 2008-06-11
10
1,174 Views
Last Modified: 2012-06-21
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
Comment
Question by:arundelr
[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
  • 5
  • 4
10 Comments
 
LVL 7

Expert Comment

by:SLafferty1983
ID: 21759756
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
 
LVL 65

Expert Comment

by:RobSampson
ID: 21764479
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
 

Author Comment

by:arundelr
ID: 21764572
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
Independent Software Vendors: 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!

 
LVL 65

Expert Comment

by:RobSampson
ID: 21765270
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
 

Author Comment

by:arundelr
ID: 21766982
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
 
LVL 65

Expert Comment

by:RobSampson
ID: 21767019
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
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 21767183
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
 

Author Closing Comment

by:arundelr
ID: 31466142
Brilliant - Thanks Rob
0
 

Author Comment

by:arundelr
ID: 21780555
Hi Rob,

Thats great - thanks for all your help!

Rob
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 21790041
No problem. Thanks for the grade.

Regards,

Rob.
0

Featured Post

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!

Question has a verified solution.

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

This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

689 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