Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
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
?
413 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

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

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

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.
A walk-through example of how to obtain and apply new DID phone numbers to your cloud PBX enabled users that are configured in Office 365. Whether you have 1, 10 or 100+ users in your tenant, it's quite easy to get them phone-enabled and making/rece…
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

604 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