PowerShell - how to create list of files in directory

Discussion in 'Webmastering & Programming' started by Thuleman, Oct 19, 2010.

  1. Thuleman

    Thuleman [H]ardness Supreme

    Messages:
    5,842
    Joined:
    Apr 13, 2004
    In the words of the famous Liz Lemon: What the what?!

    I am getting reasonably frustrated by being unable to do what seems to be a simple thing, all I want is a script that goes through a directory and creates an output file which has the names of all files with a certain extension, one file per line, and no other information. How hard can that be?

    Here's what I have:
    Code:
    $File = "E:\temp\file-list.txt"
    get-childitem "E:\temp" -recurse -include *.dat |
    ForEach-Object { $_ | format-table name -hideTableHeaders | Out-File $file -append }
    
    This creates output that looks like this:

    [CR][LF]
    filename.dat
    [CR][LF]
    [CR][LF]
    [CR][LF]
    filename.dat
    [CR][LF]
    [CR][LF]
    [CR][LF]
    filename.dat
    etc...

    What the what?!?!!!?!
    Where do all those new-lines come from?

    format-list output isn't too much different.

    I guess I am just approaching this the wrong way but googling around for it just results in information overload ( Los Links! )

    So, is there a way to do this in PowerShell? Thanks in advance for any insight you may be able to provide.
     
  2. Pwyl_The_Destroyer

    Pwyl_The_Destroyer Limp Gawd

    Messages:
    139
    Joined:
    Aug 22, 2005
    I don't know about powershell but this might help:

    dir c:\windows\*.log /b > c:test.txt
     
  3. Thuleman

    Thuleman [H]ardness Supreme

    Messages:
    5,842
    Joined:
    Apr 13, 2004
    It certainly does! Thanks. I knew of dir I was just so focused on wanting to do this in PS that I didn't even think about it. ;)

    It would still be nice to figure out how to do just that in PS.
     
  4. mmtom

    mmtom Limp Gawd

    Messages:
    320
    Joined:
    Jan 9, 2003
    Try this:

    Code:
    get-childitem -include *.dat -recurse | format-table Name -hidetableheaders | out-file $file
    
    There's no need for the foreach {} in there since format-table will take a collection as pipeline input.
     
  5. mikeblas

    mikeblas [H]ard|DCer of the Month - May 2006

    Messages:
    12,858
    Joined:
    Jun 26, 2004
    Doesn't seem to work.

    Code:
    Out-File : Cannot bind argument to parameter 'FilePath' because it is null.
    At line:1 char:87
    + get-childitem -include *.pdf -recurse | format-table Name -hidetableheaders | out-file <<<<  $file
        + CategoryInfo          : InvalidData: (:) [Out-File], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.OutFileComm
       and
    
    
     
  6. Wiseguy2001

    Wiseguy2001 2[H]4U

    Messages:
    3,579
    Joined:
    Nov 28, 2001
    $file wasn't defined/ null

    Try the code below, that should dump a text file with a list of pdf's in the root of the current directory.

    Code:
    > Get-ChildItem -Recurse -Include *.pdf| Format-Table name -hidetableheaders | Out-File 'pdf list.txt'
     
    Last edited: Oct 22, 2010
  7. mmtom

    mmtom Limp Gawd

    Messages:
    320
    Joined:
    Jan 9, 2003
    Oh, yeah. Sorry, I left $file on the end assuming you'd use that variable you defined in your original script.

    Like Wiseguy said, change $file to a path and it'll work.