locked
Poweshell text file content read error RRS feed

  • Question

  • hello,

    I've been trying to read contents of few files in a directory where the file names are unusual. The file names contains multiple periods, curve braces and even hyphens. I tried via a script but received and error.

    Say I got 3 files in a directory C:\Test

    1. a.b.c.txt

    2. this is my.file.new.txt

    3 this is my.file.new.txt (2)

    SAMPLE SCRIPT

    --------------------------------------------------------------

    $MyFileList = (Get-Content C:\Test).Name

    foreach($file in $MyfileList) {Get-Content -path "C:\Test\$MyFileList"}

    --------------------------------------------------------------

    The above throws error

    get-content : The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.
    At line:1 char:1
    + get-content -Path C:\Test\$file
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ReadError: (C:\Test:String) [Get-Content], PathTooLongException
        + FullyQualifiedErrorId : DirIOError,Microsoft.PowerShell.Commands.GetContentCommand

    Any help on this will be highly appreciated.

    Thanks in advance.

    Thursday, November 1, 2018 7:10 PM

All replies

  • It would help if you took time to learn PowerShell.

    $files = Get-Content C:\Test\* 
    foreach($file in $files){
        Get-Content $file
    }

    or

    Get-Content C:\Test\*  | Get-Content


    \_(ツ)_/


    Thursday, November 1, 2018 7:38 PM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Just do it.

    Friday, November 9, 2018 8:14 AM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Just do it.

    Tuesday, November 27, 2018 1:58 AM