Solved

Move files from one location to another mirroring folder structure

Posted on 2008-06-11
10
1,170 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

914 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now