Solved

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

Posted on 2012-03-13
8
534 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
[X]
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
  • 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
Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

 
LVL 58

Author Closing Comment

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

Thanks a lot.
0
 
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

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

730 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