?
Solved

PowerShell to compare files in folder B to a lookup for new names and then new name to files in folder A

Posted on 2014-03-20
2
Medium Priority
?
411 Views
Last Modified: 2014-03-27
Hi EEE,
 
I have two folders on my hard drive A and B. I want to compare all files in B to A. Any file thats in B and not in A, shall be copied into a third folder C. All this to be done in the PowerShell 2.0. I want to keep A , B and C as variables so set A=C:/A for example.

One little twist is that I can not compare all of B/*.* to A/*.* using the filenames as is. For that I need a lookup file.

So, under B, a file called 1.doc is the same as myfile1.doc in A.
B/1.doc = A/myfile1.doc
The lookup file has this information in the following format:

"Z:\B\1.doc","myfile1.doc",<some string>,<some string>

How can I do this? Part of the code is placed below but needs to be augmented for the lookup file and correct picking of filename in A.
0
Comment
Question by:LuckyLucks
[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
2 Comments
 
LVL 11

Assisted Solution

by:Joe Klimis
Joe Klimis earned 800 total points
ID: 39944085
Hi


I think this is a good starting point , please can you clarify if the  file from b is onlu in the corresponding folder in A or anywhere on drive A

Also when copying the files to C into 1 folder or copy structure  , and also the name structure for the file copied to c  eg 1.doc or myfile1.doc ?

$folderA = "c:\temp\1\a"
$Folderb = "c:\temp\1\b"
$folderc = "c:\temp\1\c"
# define the lookup prefix
$lookup  = "myfile"
# get a list of all the filenames for both folders
$a = get-childitem $foldera -recurse
$b = get-childitem $folderb -recurse

#Compare them and copy  files that dont match
foreach ($item in $b)
{
	$item.fullname
	if ( -not ($A.name.contains( "$lookup$($item.BaseName)$($item.Extension)")))
	{ 
	copy-item $item.fullname $folderc -verbose
	}
}

Open in new window

0
 
LVL 85

Accepted Solution

by:
oBdA earned 1200 total points
ID: 39945229
Try this:
$SourceA = "D:\Temp\A"
$SourceB = "D:\Temp\B"
$Target = "D:\Temp\C"
$LookupFile = "D:\Temp\lookup.csv"
## Set this to $True if the file names in the target folder should have the name from the lookup file (folder A) rather than folder B.
$CopyUsingLookupName = $False
$Lookup = @{}
## Read the lookup file and create the lookup hash table:
ForEach ($Line In (Get-Content -Path $LookupFile)) {
	$FileB = $Line.Split(",")[0].Trim('"')
	$FileB = Split-Path $FileB -Leaf
	$FileA = $Line.Split(",")[1].Trim('"')
	$Lookup[$FileB] = $FileA
}
## Get all files in B
ForEach ($FileItem In (Get-ChildItem -Path $SourceB -Filter "*.*" | Where {-Not $_.PSIsContainer})) {
	## Check if the file is in the lookup table, and set the name to search for accordingly
	If ($Lookup.ContainsKey($FileItem.Name)) {
		$LookFor = $Lookup[$FileItem.Name]
		"Processing '$($FileItem.Name)' (--> $($LookFor)) ..." | Write-Host -NoNewLine
	} Else {
		$LookFor = $FileItem.Name
		"Processing '$($FileItem.Name)' ..." | Write-Host -NoNewLine
	}
	## Check if the file is in folder A, and if not, copy it to C
	If (Get-Item -Path (Join-Path -Path $SourceA -ChildPath $LookFor) -ErrorAction SilentlyContinue) {
		" found in '$SourceA'." | Write-Host
	} Else {
		" copying to '$Target'." | Write-Host
		If ($CopyUsingLookupName) {
			$TargetFile = $LookFor
		} Else {
			$TargetFile = $FileItem.Name
		}
		Copy-Item -Path $FileItem.FullName -Destination (Join-Path -Path $Target -ChildPath $TargetFile) -Force
	}
}
"Done." | Write-Host

Open in new window

0

Featured Post

Four New Appliances. Same Industry-leading Speeds.

But don't take it from us.  The Firebox M370 is Miercom tested and Miercom approved, outperforming its competitors for stateless and stateful traffic throughput scenarios.  Learn more about the M370, M470, M570 and M670 and find the right solution for your organization today!

Question has a verified solution.

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

In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

800 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