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
409 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 200 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 300 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

690 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