• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 539
  • Last Modified:

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
0
cyberkiwi
Asked:
cyberkiwi
  • 5
  • 3
1 Solution
 
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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
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

Featured Post

Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now