Solved

Move files from one location to another mirroring folder structure

Posted on 2008-06-11
10
1,169 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

759 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

20 Experts available now in Live!

Get 1:1 Help Now