Link to home
Start Free TrialLog in
Avatar of David T
David T

asked on

Export Folder, Sub-Folder(s), and File Contents to a CSV file

I am going to be detailed as possible.  I am looking for a CSV that can list the file contents of a directory of my choosing (with the ability to specify how many sub-directories to also parse)

Let's Pretend I have the following

Example1
C:\FolderA\FolderAA\filea1.txt
C:\FolderA\FolderAA\filea2.pdf

Example2
C:\FolderB\FolderBB\fileb1.txt
C:\FolderB\FolderBB\fileb2.pdf


I would need a CSV that has the following:

EXAMPLE 1
Row 1 Column A would only list the relative directory name for the file. In this example it would be FolderAA
Row 1 Column B would be the full target path without the filename.  In this example that would be C:\FolderA\FolderAA
Row 1 Column C would be the file name without extension. In this example it would be filea1
Row 1 Column D would be the file name with the extension.  In this example it would be filea1.txt
Row 1 Column E would be the file name and extension with the full target path.  In this example that would be C:\FolderA\FolderAA\filea1.txt

Row 2 Column A would only list the relative directory name for the file. In this example it would be FolderAA
Row 2 Column B would be the full target path without the filename.  In this example that would be C:\FolderA\FolderAA
Row 2 Column C would be the file name without extension. In this example it would be filea2
Row 2 Column D would be the file name with the extension.  In this example it would be filea2.pdf
Row 2 Column E would be the file name and extension with the full target path.  In this example that would be C:\FolderA\FolderAA\filea2.pdf

EXAMPLE 2
Row 3 Column A would only list the relative directory name for the file. In this example it would be FolderBB
Row 3 Column B would be the full target path without the filename.  In this example that would be C:\FolderB\FolderBB
Row 3 Column C would be the file name without extension. In this example it would be fileb1
Row 3 Column D would be the file name with the extension.  In this example it would be fileb1.txt
Row 3 Column E would be the file name and extension with the full target path.  In this example that would be C:\FolderB\FolderBB\fileb1.txt


Row 4 Column A would only list the relative directory name for the file. In this example it would be FolderBB
Row 4 Column B would be the full target path without the filename.  In this example that would be C:\FolderB\FolderBB
Row 4 Column C would be the file name without extension. In this example it would be fileb1
Row 4 Column D would be the file name with the extension.  In this example it would be fileb1.pdf
Row 4 Column E would be the file name and extension with the full target path.  In this example that would be C:\FolderB\FolderBB\fileb1.pdf



So the csv itself should start out with the following so that excel is able to read the CSV properly. I also populated row 1 of the CSV with what I'm looking for so it makes complete sense below.   (I also attached the CSV)

RelativeDirectory,AbsoluteDirectoryName,FileName,FileNameWithExtension,FileNameWithFullPath
FolderAA,C:\FolderA\FolderAA,filea1,filea1.txt,C:\FolderA\FolderAA\filea1.txt
Example.csv
Avatar of aikimark
aikimark
Flag of United States of America image

There is a -Depth parameter on the Get-ChildItem command.
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-childitem?view=powershell-6

I'll look at the file you uploaded and see if I can work up something
You have three rows listed in your problem description, but your uploaded CSV only has two rows.  Please explain.
Avatar of David T
David T

ASKER

The Example.csv only has 2 rows right now (the first row is the column names that are returned with a CSV). The 2nd row is just an example of what the data would look like if it returned the data for what is listed in Example 1.  (FolderA\FolderAA\filea1.txt)
How about that for starters.
It allows you to both export to the pipeline for further processing, or directly to a csv if you pass the -CsvPath argument with the path to the export file.
By default, it will start with the current directory.
Separate column with file extension added.
[CmdletBinding()]
Param(
	[Parameter(ValueFromPipeline=$true)]
	[string]$Path = (Get-Location -PSProvider FileSystem).Path,
	[string]$Filter,
	[UInt32]$Depth,
	[string]$CsvPath
)
#requires -Version 5.0
Begin {
	Function Get-ChildItemCustom([hashtable]$Splat) {
		Get-ChildItem @Splat -File -Recurse |
			Select-Object -Property `
				@{n='RelativeDirectory'; e={[IO.Path]::GetFileName($_.DirectoryName)}},
				@{n='AbsoluteDirectoryName'; e={$_.DirectoryName}},
				@{n='FileName'; e={$_.BaseName}},
				@{n='FileNameWithExtension'; e={$_.Name}},
				@{n='FileNameWithFullPath'; e={$_.FullName}},
				@{n='FileExtension'; e={$_.Extension}}
	}
}
Process {
	If ($PSBoundParameters.ContainsKey('CsvPath')) {
		[void]$PSBoundParameters.Remove('CsvPath')
		Get-ChildItemCustom -Splat $PSBoundParameters | Export-Csv -Path $CsvPath -NoTypeInformation
	} Else {
		Get-ChildItemCustom -Splat $PSBoundParameters
	}
}

Open in new window

Avatar of David T

ASKER

This already works exactly as needed.  Any way to add a couple things to this?  Thank you so much for the quick help!

1.  A filter that allows to only output information for specific file extensions (let's say I only need .txt and .jpg files and ignore the rest)
EDIT:  I see I can use the -filter Parameter but can't figure out how to filter for more than one type of extension.  If I only specify one type of extension with the -filter command it works (-filter *.txt)

2.  Capability to specify how many levels deep to parse records.
EDIT: I see I can use the -depth parameter to specify this
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial