Link to home
Start Free TrialLog in
Avatar of MilesLogan
MilesLoganFlag for United States of America

asked on

Copy file from multiple shares to one location v2

Hi EE

I had this question answered by oBdA below but I still need a bit of help .
https://www.experts-exchange.com/questions/29071695/Copy-file-from-multiple-shares-to-one-location.html

Both of those scripts work but the files are being overridden if they have a similar path .

example:
\\Server1\folder\folder2\folder3\SamAccountname1
\\Server1\folder\folder2\folder3\SamAccountname2

In the example above only the file for SamAccountname2 is being saved or overriding the file for SAmaccountName1
Why I am thinking it would be easier of the file names can just be saved as the entire path or just the last 7 characters of the path.

$File = 'logfile.log'
$Destination = 'C:\Temp\Location'
$CopyLog = 'C:\Temp\Location\_LogfileCopy.log'
Get-Content -Path .\shares.txt | ForEach-Object {
	$Server, $Share, $SamAccountName = $_.Split('\', [StringSplitOptions]::RemoveEmptyEntries)[0..2]
	$Result = $_ | Select-Object -Property @{n='SamAccountName'; e={$SamAccountName}}, @{n='Path'; e={$_}}, Error
	Try {
		Copy-Item -Path "$($_)\$($File)" -Destination "$($Destination)\$($SamAccountName)_$($File)" -ErrorAction Stop
	} Catch {
		$Result.Error = $_.Exception.Message
	}
	$Result
} | Export-Csv -NoTypeInformation -Path $CopyLog

Open in new window

Avatar of oBdA
oBdA

Because you said the folder structure would be "\\Server\Share\SamAccountName"; now it's "\\Server1\folder\folder2\folder3\SamAccountname1".
* What is the exact folder structure? Is "SamAccountName" always the folder containing "logfile.log"?
* Is SamAccountName unique over all servers and shares?
* Is the source server/source share relevant, or are you just interested in the contents?
Avatar of MilesLogan

ASKER

HI 0BdA

that was my mistake thinking the folder structure was the same for all but its not ...

What is the exact folder structure? Is "SamAccountName" always the folder containing "logfile.log"?
      The structure is different for all unfortunately , the only unique item is the samaccountname at the end of the path,
 Is SamAccountName unique over all servers and shares?
      Yes, this is the only unique item across for all the shares that I need the file from .

Is the source server/source share relevant, or are you just interested in the contents?
    I only care about the file I need to copy from the source .
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
insane ! you are just amazing .. I so appreciate this .