Write and search if a value already exist.

This topic contains 2 replies, has 2 voices, and was last updated by  Cristhian Vic. 4 months ago.

  • Author
    Posts
  • #73211

    Cristhian Vic.
    Participant

    Hi.

    I'm extracting some fields from an XML...

    $date
    $name
    $amount
    $UUID

    The $UUID is unique, so, i want to save that field in a TXT, and then, search in that same TXT if the value already exist.

    If the value exist, just ignore it and process the next XML.

    $filepath = "C:\UUIDs.txt"
    $uuids = Get-Content C:\UUIDs.txt
    
    ForEach ($uuid in $uuids) {
      If ($uuid -eq $uuids) {
        Write-Output 'Already exist.'
      }
      Else
      {
       #Do the XML creation...
       write-output ($uuid) | out-file -filepath $filepath -encoding ascii -append
          
      }
    }
    
  • #73214

    Olaf Soyk
    Participant

    If you created an array of UUIDs from your file you can check with

    $UUID -contains $UUIDArray

    if the new UUID is already there.

  • #73318

    Cristhian Vic.
    Participant

    Thanks.

    I think i got it.

    What do you think?

    $Saved = Get-Content C:\CC\Saved.txt
    $LogRepeated = "C:\CC\LogRepeated.txt"
    $LogSaved = "C:\CC\Saved.txt"
    $uuid = "1234"
    
    
      If ($Saved -contains $uuid)
      {
        Write-Output "File Already exist."
        Write-Output ($uuid) | Out-File -FilePath $LogRepeated -Encoding ASCII -Append
        Break;    
      }
      
      Else
      {
       #XML Generator Code
       Write-Output "File added."
       Write-Output ($uuid) | Out-File -FilePath $LogSaved -Encoding ASCII -Append      
      }
    

You must be logged in to reply to this topic.