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
407 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
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 84

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

809 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