locked
Select-String / Substrings RRS feed

  • Question

  • I have some XML files I'm filtering through to pull state abbreviations out of them.  The line from the datafile looks like this:     

    <add key="DefaultState" value="AL"/>

    Here's the code:

    $filelist = (Get-ChildItem -Path "C:\blahblah\filenames*.xml")
    
    foreach ($file in $filelist)
        {
        $filename = $file.Name
        $fullfilename = $file.FullName
        
        $statestring = (select-string -InputObject $file -Pattern 'DefaultState" value=').Line
        $state = ($statestring -split '"')[3]
        $newname = $state.$filename
        Write-Host "$newname"
        }
    

    What I'm finding is that the response from the split includes a line feed - so I can't pipe the two letter abbreviation to a variable.

    My ultimate aim is to append the two-letter state abbreviation to the front of the filenames.

    Any assistance would be greatly appreciated.


    Thursday, May 17, 2018 7:00 PM

Answers

  • You need to use XML methods to edit XML files.

    [xml]$xml = Get-Content $file
    $xml.SelectSingleNode('//add[@key="DefaultState"]').Value


    \_(ツ)_/

    • Marked as answer by BAWrites Thursday, May 17, 2018 7:29 PM
    Thursday, May 17, 2018 7:12 PM

All replies

  • You need to use XML methods to edit XML files.

    [xml]$xml = Get-Content $file
    $xml.SelectSingleNode('//add[@key="DefaultState"]').Value


    \_(ツ)_/

    • Marked as answer by BAWrites Thursday, May 17, 2018 7:29 PM
    Thursday, May 17, 2018 7:12 PM
  • Run this to see how it works:

    $filelist = (Get-ChildItem -Path "C:\blahblah\filenames*.xml")
    
    foreach ($file in $filelist){
        [xml]$xml = Get-Content $file
        $xml.SelectSingleNode('//add[@key="DefaultState"]').Value
    }


    \_(ツ)_/

    Thursday, May 17, 2018 7:15 PM
  • A little code engineering and that did the trick - I used the iif statement to ignore the empty line.  THanks!

    $filelist = (Get-ChildItem -Path "C:\blahblah\*.xml")
    
    foreach ($file in $filelist)
        {
        $filename = $file.Name
        $fullfilename = $file.FullName
    
        [xml]$xml = Get-Content $file
        $statedat = ($xml.SelectSingleNode('//add[@key="DefaultState"]').Value)
        Foreach ($line in $statedat)
            {
            if ($line -ne $null)
                {
                $state=$line
                Write-Host $state
                $newfilename = "$state$.filename"
                Write-Host "$newfilename"
                Rename-Item -Path $fullfilename -NewName "C:\blahblah\$newfilename" -Force
                }
            }
    
        }
    


    Thursday, May 17, 2018 7:29 PM
  • Not even close to what is happening.

    $filelist =Get-ChildItem C:\blahblah\*.xml
    foreach ($file in $filelist) {
        [xml]$xml = Get-Content $file
        if($state = $xml.SelectSingleNode('//add[@key="DefaultState"]').Value){
                $newfilename = "$state$.filename"
                Write-Host $newfilename
                Rename-Item -Path $file.FullName -NewName "C:\blahblah\$newfilename" -Force
        }
    }
    

    Don't create variables that you don't need.
    Don't add parens around everything.
    Don't add double quotes where they are no needed.


    \_(ツ)_/

    Thursday, May 17, 2018 7:40 PM
  • I appreciate the advice!  Thanks!


    Sunday, May 20, 2018 3:07 PM
  • For guidance see the following article.  It is a very good write on how to use PowerShell styles and the rules of variable creation and naming conventions.

    It is pretty much a PowerShell version of the defacto industry standards for programmers.

    PowerShell Style Guidelines


    \_(ツ)_/

    Sunday, May 20, 2018 3:59 PM