Solved

Move files from one location to another mirroring folder structure

Posted on 2008-06-11
10
1,173 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
  • 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
Technology Partners: 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

Script to copy or move mouse-selected collection of files plus targets referenced by shortcuts (.lnk) The purpose of this article is to help illuminate the real challenges and options available (where they may exist) for utilizing simple scriptin…
When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

697 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