Link to home
Start Free TrialLog in
Avatar of cyberkiwi
cyberkiwiFlag for New Zealand

asked on

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
Avatar of RobSampson
RobSampson
Flag of Australia image

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

Avatar of cyberkiwi

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Perfect.
You're a good man!

Thanks a lot.
No problem. Thanks for the grade.

Regards,

Rob.
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.
> 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.
OK cool.  At least you've got another option now, too!

Rob.