Solved

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

Posted on 2012-03-13
8
529 Views
Last Modified: 2012-03-14
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
0
Comment
Question by:cyberkiwi
  • 5
  • 3
8 Comments
 
LVL 65

Expert Comment

by:RobSampson
ID: 37718179
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
 
LVL 58

Author Comment

by:cyberkiwi
ID: 37718225
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
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 37718306
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
 
LVL 58

Author Closing Comment

by:cyberkiwi
ID: 37718322
Perfect.
You're a good man!

Thanks a lot.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 65

Expert Comment

by:RobSampson
ID: 37718378
No problem. Thanks for the grade.

Regards,

Rob.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 37722734
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
 
LVL 58

Author Comment

by:cyberkiwi
ID: 37722746
> 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
 
LVL 65

Expert Comment

by:RobSampson
ID: 37722758
OK cool.  At least you've got another option now, too!

Rob.
0

Featured Post

What Is Threat Intelligence?

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

Join & Write a Comment

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

708 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