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
403 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
2 Comments
 
LVL 10

Assisted Solution

by:JoeKlimis
JoeKlimis earned 200 total points
Comment Utility
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 82

Accepted Solution

by:
oBdA earned 300 total points
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
Set OWA language and time zone in Exchange for individuals, all users or per database.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now