none
Enclose Path Variable in Double Quotes at Run Time RRS feed

  • Question

  • If I want to get the content of a file where the path has spaces I just enclose it in double quotes.  But I cannot for the life of me figure out how to do this at run time when the path is a variable!

    How can I call

    $a = Get-ChildItem -Path C:\SomeFolder -Recurse
    Foreach ($child in $a) {
         Get-Content -Path $child.FullName
    }

    when $child.FullName has spaces in it?!!!


    • Edited by spickles Friday, April 1, 2016 8:35 PM
    Friday, April 1, 2016 8:32 PM

Answers

  • You cannot use Get-Content on a folder.

    The issue is that you are not giving us the information.  You are insisting on a fix for something that is not what you think it is,

    Don't pass folder objects an you don't need a loop.

    Get-ChildItem C:\SomeFolder -Recurse -File | Get-Content
    
    Or the harder way:
    $files = Get-ChildItem C:\test  -File -Recurse
    Foreach ($f in $files) {
         Get-Content $f.Fullname
    }
    Some files will give errors because they cannot be opened as text files.


    \_(ツ)_/

    • Marked as answer by spickles Wednesday, April 6, 2016 5:04 PM
    Wednesday, April 6, 2016 3:58 PM

All replies

  • You don't need to use the FullName property. Get-Content works just fine with a file object, and it accepts pipeline input, so you don't need foreach either.


    PS C:\> Get-ChildItem C:\SomeFolder -Recurse | Get-Content


    -- Bill Stewart [Bill_Stewart]

    Friday, April 1, 2016 9:13 PM
    Moderator
  • Bill -

    Thanks for the reply, I'll give that a try.  Can you tell me how to add quotes at run time so I learn that as well?  It has really frustrated me.

    Wednesday, April 6, 2016 2:41 PM
  • You don't need to add quotes.

    -- Bill Stewart [Bill_Stewart]

    Wednesday, April 6, 2016 2:54 PM
    Moderator
  • For which one?  My original post or your version?  When I use my variant, $child.FullName with spaces causes the script to fail.
    Wednesday, April 6, 2016 3:00 PM
  • You don't need to surround a variable with quotes when simply using a variable in PowerShell.

    Please provide a succinct example of why you think you need to do that.


    -- Bill Stewart [Bill_Stewart]

    Wednesday, April 6, 2016 3:04 PM
    Moderator
  • You cannot use Get-Content on a folder.

    The issue is that you are not giving us the information.  You are insisting on a fix for something that is not what you think it is,

    Don't pass folder objects an you don't need a loop.

    Get-ChildItem C:\SomeFolder -Recurse -File | Get-Content
    
    Or the harder way:
    $files = Get-ChildItem C:\test  -File -Recurse
    Foreach ($f in $files) {
         Get-Content $f.Fullname
    }
    Some files will give errors because they cannot be opened as text files.


    \_(ツ)_/

    • Marked as answer by spickles Wednesday, April 6, 2016 5:04 PM
    Wednesday, April 6, 2016 3:58 PM
  • Gents -

    Thanks for the replies.  I was in the process of producing the error output when @jrv posted.  I had come to the same conclusion, that it was the folder paths that was causing the errors.  Thanks for showing me how to exclude folders.  I'm building it as a Foreach loop because I'm going to perform multiple operations with each file.  Otherwise I would just pipe.

    Wednesday, April 6, 2016 5:04 PM