1

I'm using the code referenced here: with the examples etc but I'm running into an error with this line:

$remotefilehash =  ($remotefiles -replace '^[a-f0-9]{32}(  )', '$0=  ' -join "`n") | ConvertFrom-StringData

https://stackoverflow.com/a/69782527/800592

here's the full error:

ConvertFrom-StringData : Data item '4826367e20469078693dced6dc642b96' in line '4826367e20469078693dced6dc642b96  =  My Videos/Drone/DJI-FPV/DJI_0003.MP4' is already defined. 
At P:\scripts\code\pcloud_sync.ps1:66 char:86
+ ... ace '^[a-f0-9]{32}(  )', '$0=  ' -join "`n") | ConvertFrom-StringData
+                                                    ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [ConvertFrom-StringData], PSInvalidOperationException
    + FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.ConvertFromStringDataCommand

Update2 I think my desire is to deduplicate my remoteFiles object which is simply an list/arry? of md5sum file path. since it seems like there are times where there is the same file in two different paths I want to de duplicate the remoteFiles obj on the md5sum

Tony
  • 8,681
  • 7
  • 36
  • 55

1 Answers1

2

ConvertFrom-StringData parses each input string in the form of one or more <name>=<value> lines into a hashtable.

In doing so it doesn't tolerate duplicates; that is, if a previous <name> value is encountered again in a given input string, the statement-terminating error you saw occurs.

A simple repro:

# !! Error: 
# !!  "ConvertFrom-StringData: Data item 'Key' in line 'Key=b' is already defined"
ConvertFrom-StringData "Key=a`nKey=b"

Thus, the solution is to eliminate duplicates from your input string first - assuming that the <value> part among the lines with duplicate <name>s is also the same.

If not, you'll need a different data structure to represent your input data, which ConvertFrom-StringData cannot provide.

If you only care about the <name> parts - ignoring duplicates, irrespective of their <value>s - you can parse them into a [System.Collections.Generic.HashSet[T] instance; in your case:

$remotefileHashSet =
  [System.Collections.Generic.HashSet[string]] $remotefiles.ForEach({ (-split $_)[0] })

Note that hash sets are case-sensitive by default with respect to lookups; more work is needed if you want case-insensitivity: see this answer.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • interesting - the sum's are the same but the paths are different which I don't really care about - how do I dedup by just the sum? – Tony Aug 26 '22 at 22:28
  • @Tony, please see my update. – mklement0 Aug 26 '22 at 23:02
  • updated original post, as I don't think what your proposing gets me what I need ? – Tony Aug 27 '22 at 00:28
  • if the above wasn't clear I need the sum and the path so removing just the dupes from the sum's leaves me with an issue matching up the paths ... – Tony Aug 27 '22 at 00:44
  • @Tony, your first comment on this question contradicts your most recent one. As stated in the answer, if you have `=` pairs with duplicate ``s but different ``s and you want to _retain_ these duplicates, `ConvertFrom-StringData` as shown won't work. _Swapping_ keys and values may be an option, with the file paths acting as the keys, and the corresponding hashes as the values. It sounds like it may be time for a _new_ question post yet again. – mklement0 Aug 27 '22 at 02:13