change variable outcome form 1C201234 to IC201234 1 to I

Welcome Forums General PowerShell Q&A change variable outcome form 1C201234 to IC201234 1 to I

Viewing 3 reply threads
  • Author
    Posts
    • #264167
      Participant
      Topics: 2
      Replies: 2
      Points: 17
      Rank: Member

      information out variable change to other variable

      in the program below we read a PDF file   if the variable is 1C###### (# di digits) i want to change it to IC#######

      the PDF makes sometimes fault an read a 1 instead of an I

      variable is $logistixid

      if $logistixid = 1C201630 then change to IC201630

      the numbers change every time 1C combination is  the same

      I don’t program in powershell only VBA  bu i hope it is a simpel question

      thanks for your help

       

      <!–more–>

      # prepare the pdf
      $reader = New-Object iTextSharp.text.pdf.pdfreader -ArgumentList $sourcefile

      $logistixid = $null

      # for each page
      for($page = 1; $page -le $reader.NumberOfPages; $page++) {

      # set the page text
      $pageText = [iTextSharp.text.pdf.parser.PdfTextExtractor]::GetTextFromPage($reader,$page).Split([char]0x000A)
      # if the page text contains any of the keywords we’re evaluating

      $match = [regex]::Match($pageText, $idpattern ).captures
      if($match)
      {
      foreach ($m in $match)
      {
      $logistixid = $m.value.ToUpper()
      break;
      }
      }
      }
      $reader.Close()

      return $logistixid<!–more–>

    • #264173
      Participant
      Topics: 1
      Replies: 326
      Points: 257
      Helping Hand
      Rank: Contributor

      Yes, it’s straightforward:

       

       

    • #264272
      Participant
      Topics: 2
      Replies: 2
      Points: 17
      Rank: Member

      thank you for the solution it works

      now have solved several scans but i have a new challenge, i can change the first letters and a lot of document can be place in the right map

      but the read in PDF gives more problems in the in the 6 digits after IC  the PDF will give a O for a 0 (zero) and the 1 is also gives I how can i change that in the same if statement?

      So when the PDF give back 1C2OI234 it mist be IC201234

    • #264299
      Participant
      Topics: 1
      Replies: 326
      Points: 257
      Helping Hand
      Rank: Contributor

      Well the code example I gave won’t trigger if there are letters in the last six digits as the regular expression I gave ^1C\d+” will only match 1C followed by 1 or more numbers.

      I would suggest you just use -replace to replace all the O’s or I’s, then change the first two characters in the string to IC.

      Note that by default the -replace operator is not case sensitive.

       

       

      • This reply was modified 2 days, 13 hours ago by Matt Bloomfield. Reason: Formatting. Again
Viewing 3 reply threads
  • You must be logged in to reply to this topic.