Powershell / VB Script to copy files from A to B with conditions

Hi EXPERTS!

Does anyone have a script that will take:

1. source (root) folder  [e.g. C:\temp\template-update]
2. destination (root) folder  [e.g. C:\temp\project1]
3. file mask  [e.g. "*common*.cs]

And for each file in source, recursively, that match the file mask,
Will copy it into the destination folder, preserving the path information?

So if a file, std.common.net.cs was found in C:\temp\template-update\std\net, it will be copied into C:\temp\project1\std\net\std.common.net.cs with extreme prejudice (overwrite)?

Thank you
LVL 58
cyberkiwiAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RobSampsonCommented:
Hi, this won't be the fastest thing in the world since it's VBScript, but it should work.

Regards,

Rob.

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")

strSourceFolder = "C:\Temp\Scripts"
strTargetFolder = "C:\Temp\New"
strMask = "*.vbs"

If Right(strSourceFolder, 1) <> "\" Then strSourceFolder = strSourceFolder & "\"
If Right(strTargetFolder, 1) <> "\" Then strTargetFolder = strTargetFolder & "\"

strParentFolder = objFSO.GetFolder(objFSO.GetFile(WScript.ScriptFullName).ParentFolder).ShortPath
If Right(strParentFolder, 1) <> "\" Then strParentFolder = strParentFolder & "\"

strTempFile = strParentFolder & "TempOutput.dat"

objShell.Run "cmd /c dir /s /b """ & strSourceFolder & strMask & """ > " & strTempFile, 0, True

Set objTemp = objFSO.OpenTextFile(strTempFile, 1, False)
While Not objTemp.AtEndOfStream
	strLine = Trim(objTemp.ReadLine)
	If strLine <> "" Then
		strOriginalFolder = objFSO.GetFile(strLine).ParentFolder
		If Right(strOriginalFolder, 1) <> "\" Then strOriginalFolder = strOriginalFolder & "\"
		strNewFolder = Replace(strOriginalFolder, strSourceFolder, strTargetFolder, vbTextCompare)
		If objFSO.FolderExists(strNewFolder) = False Then
			WScript.Echo "Creating folder " & strNewFolder
			CreateLocalDirectoryStructure strNewFolder
		End If
		If Right(strNewFolder, 1) <> "\" Then strNewFolder = strNewFolder & "\"
		WScript.Echo "Copying " & strLine & " to " & strNewFolder & objFSO.GetFileName(strLine)
		objFSO.CopyFile strLine, strNewFolder & objFSO.GetFileName(strLine), True
	End If
Wend
objTemp.Close
objFSO.DeleteFile strTempFile, True

WScript.Quit

Sub CreateLocalDirectoryStructure(strPath)
	' Check if the path is a local file path
	'WScript.Echo "Argument passed for local creation: " & strPath
	If Mid(strPath, 2, 2) = ":\" Then
		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)
				'WScript.Echo strSubPath & " is being checked..."
				If objFSO.FolderExists(strSubPath) = False Then
					objFSO.CreateFolder(strSubPath)
					'WScript.Echo strSubPath & " created."
				Else
					'WScript.Echo strSubPath & " exists."
				End If
			Next
		End If
	Else
		WScript.Echo "A local directory path was not passed to the CreateLocalDirectoryStructure procedure."
	End If
End Sub

Open in new window

0
cyberkiwiAuthor Commented:
Thanks Rob for the quick turnaround.
There is at least one bug though - and it could be the only one.
The target folders are never created if they didn't already exist.  It would work for my current purposes, but would be better if fixed.

Note: check the output of
            WScript.echo "Check: " & strNewFolder

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")

strSourceFolder = "C:\temp\2010"
strTargetFolder = "C:\temp\2012"
strMask = "*.xls"

If Right(strSourceFolder, 1) <> "\" Then strSourceFolder = strSourceFolder & "\"
If Right(strTargetFolder, 1) <> "\" Then strTargetFolder = strTargetFolder & "\"

strParentFolder = objFSO.GetFolder(objFSO.GetFile(WScript.ScriptFullName).ParentFolder).ShortPath
If Right(strParentFolder, 1) <> "\" Then strParentFolder = strParentFolder & "\"

strTempFile = strParentFolder & "TempOutput.dat"

objShell.Run "cmd /c dir /s /b """ & strSourceFolder & strMask & """ > " & strTempFile, 0, True

Set objTemp = objFSO.OpenTextFile(strTempFile, 1, False)
While Not objTemp.AtEndOfStream
	strLine = Trim(objTemp.ReadLine)
	If strLine <> "" Then
		strOriginalFolder = objFSO.GetFile(strLine).ParentFolder
		If Right(strOriginalFolder, 1) <> "\" Then strOriginalFolder = strOriginalFolder & "\"
		strNewFolder = Replace(strOriginalFolder, strSourceFolder, strTargetFolder, vbTextCompare)
		WScript.echo "Check: " & strNewFolder
		If objFSO.FolderExists(strNewFolder) = False Then
			WScript.Echo "Creating folder " & strNewFolder
			CreateLocalDirectoryStructure strNewFolder
		End If
		If Right(strNewFolder, 1) <> "\" Then strNewFolder = strNewFolder & "\"
		WScript.Echo "Copying " & strLine & " to " & strNewFolder & objFSO.GetFileName(strLine)
		'objFSO.CopyFile strLine, strNewFolder & objFSO.GetFileName(strLine), True
	End If
Wend
objTemp.Close
objFSO.DeleteFile strTempFile, True

WScript.Quit

Sub CreateLocalDirectoryStructure(strPath)
	' Check if the path is a local file path
	WScript.Echo "Argument passed for local creation: " & strPath
	If Mid(strPath, 2, 2) = ":\" Then
		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)
				WScript.Echo strSubPath & " is being checked..."
				If objFSO.FolderExists(strSubPath) = False Then
					objFSO.CreateFolder(strSubPath)
					WScript.Echo strSubPath & " created."
				Else
					WScript.Echo strSubPath & " exists."
				End If
			Next
		End If
	Else
		WScript.Echo "A local directory path was not passed to the CreateLocalDirectoryStructure procedure."
	End If
End Sub

Open in new window

0
RobSampsonCommented:
Ooops, sorry.  I made a mistake on this line:
            strNewFolder = Replace(strOriginalFolder, strSourceFolder, strTargetFolder, vbTextCompare)


it should be
            strNewFolder = Replace(strOriginalFolder, strSourceFolder, strTargetFolder,1,-1,vbTextCompare)

Regards,

Rob.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Challenges in Government Cyber Security

Has cyber security been a challenge in your government organization? Are you looking to improve your government's network security? Learn more about how to improve your government organization's security by viewing our on-demand webinar!

cyberkiwiAuthor Commented:
Perfect.
You're a good man!

Thanks a lot.
0
RobSampsonCommented:
No problem. Thanks for the grade.

Regards,

Rob.
0
RobSampsonCommented:
Hi, this is a little emabarrassing, but once again I forgot about Robocopy.  In it's simplest form, this command with Robocopy will do the same thing as the script above:

robocopy "c:\temp\2010" "c:\temp\2012" "*.xls" /E /Y

There are plenty of other switches for Robocopy, depending on your needs too.

Regards,

Rob.
0
cyberkiwiAuthor Commented:
> this is a little emabarrassing

Nah. It's all good. I like vbscripts (and jscripts) that I can paste into any machine and run. Not so easy getting a copy of Robocopy to some servers via rdp/teamviewer etc.

Everything has its place, and you have an "Expert" seat in my mind.
0
RobSampsonCommented:
OK cool.  At least you've got another option now, too!

Rob.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.