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
Solved

Move files from one location to another mirroring folder structure

Posted on 2008-06-11
10
1,172 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
Script is being strange 8 76
Pass through dll 2 91
fso.FolderExists("\\server\HiddenFolder$") 4 78
Excel - Creating a Visual Basic Macro to change the active cell color 9 52
In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
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…

860 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