Results 1 to 7 of 7
  1. #1
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,434
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts

    Unhappy PowerShell Replace Code

    I'm working my way through the "Windows PowerShell in Action" book and I was thinking I understood it but that was a stretch.

    Here's the code:
    Code:
    foreach-object ($f in dir *.xml) {$f = $f -replace 'Bruces-PC','Inspiron15-i5'}
    Now as I understand this, obviously wrong, according to page 144 of the book, this should find all the .xml files in the current directory then replace all instances of Bruces-PC with Inspiron15-i5 and then write it back to the same filename!

    I've also tried the variant of {${$f} = ${$f} -replace ...} and still no love.
    I've also tried it with a full path on the dir command w/o luck.

    Ideas?
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  2. #2
    Administrator
    Join Date
    Mar 2001
    Location
    St Louis, Missouri, USA
    Posts
    23,577
    Thanks
    5
    Thanked 1,057 Times in 926 Posts
    Have you tried quotation marks as the string delimiter?

    Joe

  3. #3
    WS Lounge VIP
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    8,179
    Thanks
    47
    Thanked 983 Times in 913 Posts
    My guess is that you haven't specified the dir command properly. Try this: ($f in 'dir *.xml')

    cheers, Paul

  4. #4
    5 Star Lounger
    Join Date
    Jan 2010
    Location
    Los Angeles, CA
    Posts
    828
    Thanks
    4
    Thanked 38 Times in 34 Posts
    Also, you cannot use the ${..} syntax with a variable. That is, ${$f} won't work. Instead use "get-content $f". Similaryl, use set-content to write to a file:

    foreach ($f in dir *.xml) {(get-content $f) -replace 'Bruces-PC','Inspiron15-15' | set-content $f}


    BTW, are you looking at the first or second edition of PowerShell in Action? I assume first edition based on the page reference.

  5. The Following User Says Thank You to cafed00d For This Useful Post:

    RetiredGeek (2011-07-14)

  6. #5
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,434
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    Cafed00d,

    You da' MAN!

    My book is copyrighted 2007 and makes no mention of being a 2nd ed., so I assume it is the 1st ed.

    Thanks so much I now have a better handle on thing especially, the not using a variable in the ${} construct . I've still got a ways to go to get through the book.

    Thanks again.
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  7. #6
    5 Star Lounger
    Join Date
    Jan 2010
    Location
    Los Angeles, CA
    Posts
    828
    Thanks
    4
    Thanked 38 Times in 34 Posts
    The second edition just came out - it's almost twice as thick as the first! Can't wait to work my way through it.

    I do have a suggestion: never try a long command unless you have first tried the parts. For example, I found out the ${$f} doesn't work, while ${some.xml} does by at the command line assigning $f to a file, and then entering just ${$f} in the command line. I also played with an empty foreach command to make sure that it found the files I was looking for:

    foreach ($f in dir *.xml) {$f}

    And then I tried various individual command to see what it would take to read a file:

    get-content $f

    Only once I was sure the individual pieces worked did I string the full command together.

    All the best in your PowerShell studies!

  8. #7
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,434
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    Cafed00d,

    I was doing exactly as you stated but had not got all the way there yet. I also did not yet know about the get-content cmdlet.

    Thanks Again.
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •