1

I have over 300 .csv files in a folder (folder A) containing computer information (machine name, HD size, memory, etc) all on separate lines. Headers are Item and Value.

I need to extract the first 25 lines from each file in the folder, and put it into one csv file.

Get-Content "C:\folder\A\*.csv" | select -First 25 | Out-file "C:\folder\B\.csv" 

This works fine but how do I get it to work for all the files in folder A?

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • use `Get-ChilIdtem` on the source dir & pipeline that to `Get-Content`. also, you likely want to skip the 1st line since that is the header line. – Lee_Dailey Feb 04 '19 at 20:42
  • How can I get the output file on desktop as I get access denied when using C. Get-ChildItem -Filter C:\test\A\*.csv | Import-Csv | Select-Object -First 25 | out-file C:\output.csv -Append – Becky Teeple-Binder Feb 04 '19 at 21:14
  • 1
    [1] don't use `Out-File` on a CSV imported collection. that will NOT work - use `Export-CSV` instead. [2] the current user desktop dir is _usually_ located @ `"$env:USERPROFILE\Desktop"`. that is not true for redirected folders, tho. if you need a predictably available location, the `$env:Temp` is usually a good one. the system temp dir is usually @ `"$env:windir\temp"`. – Lee_Dailey Feb 04 '19 at 21:20

2 Answers2

0

See this answer for a plain-text processing solution.


As for what you tried:

Get-Content "C:\folder\A*.csv" | select -First 25

This doesn't extract 25 lines from each file, it collects the lines from all files that match the wildcard expression (A*.csv) and then extracts a single 25-element slice from the beginning.

Additionally, in order to output to a single CSV output file:

  • You must ensure that only one header row (line with column names) is written to the output file.

  • This header row must then be followed with 25 data rows from each file, i.e., the lines with numbers 2 through 26.

mklement0
  • 382,024
  • 64
  • 607
  • 775
0

If your 30/300? csv files contain 10/25? name,value pairs,
combining them in single csv file is not of much use;
unless they are uniform and unique and can thus
be treated as a hash table and converted as input to a [PSCustomObject]

So provided your input csv look like:

|PCone.csv           |PCtwo.csv           |PChree.csv          |
+--------------------+--------------------+--------------------+
|item,value          |item,value          |item,value          |
|machine name,PCone  |machine name,PCtwo  |machine name,PCthree|
|HD size,250GB       |HD size,1TB         |HD size,1TB         |
|memory,8192         |memory,16384        |memory,16384        |

this script:

## Q:\Test\2019\02\05\SO_54523916.ps1
$Columns = @("machine name","HD size","memory")

$AllCsv = foreach($csv in Get-ChildItem pc*.csv){
    [pscustomobject](ConvertFrom-StringData -StringData (
                     (Get-Content $Csv -raw) -replace ',', "="))
}

$AllCsv | Select-Object $Columns

yields this output:

machine name HD size memory
------------ ------- ------
PCone        250GB   8192
PCthree      1TB     16384
PCtwo        1TB     16384