Here are a couple of methods to export the list of files in a directory into a CSV for analysis.
1. Powershell (Wrapped for use in CMD.exe)
The following command is a wrapped Powershell script that will run from a cmd.exe window.
powershell -Command "Get-ChildItem -Path . -File | Select-Object Name,@{Name='SizeBytes';Expression={$_.Length}},@{Name='LastModified';Expression={$_.LastWriteTime}} | Export-Csv -Path '.\file_list.csv' -NoTypeInformation"
Output:
- Name: File name
- SizeBytes: File size in bytes
- LastModified: Timestamp of the last modification
2. Native CMD.exe Prompt
The following is a native command line that can be run from the cmd.exe prompt.
(for /R %f in (*) do @echo %~pf%~nxf) > file_list.csv
Or to export recursively through subfolders as well:
(for /R %f in (*) do @echo "%~nxf",%~zf,"%~tf") > file_list.csv
Explanation:
%~nxf— filename with extension%~zf— file size in bytes%~tf— last modified date/timefor /R— recursively processes all files in subdirectories- Output is saved to
file_list.csvin the current directory
4. Explorer Context Item
The following registry script creates a context item for folders in Windows Explorer titled “Export File List to CSV”. It will prompt whether to include subfolders and will ask for the save path of the resulting CSV.
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Directory\shell\ExportFileListToCSV]
@="Export File List to CSV"
[HKEY_CLASSES_ROOT\Directory\shell\ExportFileListToCSV\command]
@="powershell -NoProfile -ExecutionPolicy Bypass -Command \"& { $folder='%1'; $includeSub=Read-Host 'Include subfolders? (Y/N)'; $recurse=$includeSub -match '^[Yy]'; $defaultPath=Join-Path (Split-Path $folder -Parent) ((Split-Path $folder -Leaf)+'.csv'); do { $savePath=Read-Host ('Enter full path for CSV file (default: '+$defaultPath+')'); if([string]::IsNullOrWhiteSpace($savePath)){$savePath=$defaultPath}; $exists=Test-Path $savePath; if($exists){Write-Host 'File already exists. Please enter a different path.'} } while ($exists); if($recurse){Get-ChildItem -Path $folder -Recurse | Select-Object @{Name='RelativePath';Expression={ if ($_.PSIsContainer) { ($_.FullName).Substring($folder.Length+1) + '\\' } else { ($_.FullName).Substring($folder.Length+1) } }}, @{Name='FileName';Expression={ if ($_.PSIsContainer) { $_.Name + '\\' } else { $_.Name } }}, @{Name='Size';Expression={$_.Length}}, @{Name='DateModified';Expression={$_.LastWriteTime}} | Export-Csv -Path $savePath -NoTypeInformation -Encoding UTF8}else{Get-ChildItem -Path $folder | Select-Object @{Name='FileName';Expression={ if ($_.PSIsContainer) { $_.Name + '\\' } else { $_.Name } }}, @{Name='Size';Expression={$_.Length}}, @{Name='DateModified';Expression={$_.LastWriteTime}} | Export-Csv -Path $savePath -NoTypeInformation -Encoding UTF8} }\""
The script uses embedded Powershell to enumerate the folder and create the CSV. This is pretty complicated. A better way is to save the Powershell Script separately and reference that in the context menu item.
Powershell Script: ExportFolderToCsv.ps1
# Script to create a CSV listing the contents of a folder
# Prompts whether to include subfolders
# Asks for destination .csv path.
param(
[string]$Folder
)
$includeSub = Read-Host 'Include subfolders? (Y/N)'
$recurse = $includeSub -match '^[Yy]'
$defaultPath = Join-Path (Split-Path $Folder -Parent) ((Split-Path $Folder -Leaf)+'.csv')
do {
$savePath = Read-Host ('Enter full path for CSV file (default: '+$defaultPath+')')
if ([string]::IsNullOrWhiteSpace($savePath)) { $savePath = $defaultPath }
$exists = Test-Path $savePath
if ($exists) { Write-Host 'File already exists. Please enter a different path.' }
} while ($exists)
if ($recurse) {
Get-ChildItem -Path $Folder -Recurse | Select-Object `
@{Name='RelativePath';Expression={ if ($_.PSIsContainer) { ($_.FullName).Substring($Folder.Length+1) + '\' } else { ($_.FullName).Substring($Folder.Length+1) } }},
@{Name='FileName';Expression={ if ($_.PSIsContainer) { $_.Name + '\' } else { $_.Name } }},
@{Name='Size';Expression={$_.Length}},
@{Name='DateModified';Expression={$_.LastWriteTime}} |
Export-Csv -Path $savePath -NoTypeInformation -Encoding UTF8
} else {
Get-ChildItem -Path $Folder | Select-Object `
@{Name='FileName';Expression={ if ($_.PSIsContainer) { $_.Name + '\' } else { $_.Name } }},
@{Name='Size';Expression={$_.Length}},
@{Name='DateModified';Expression={$_.LastWriteTime}} |
Export-Csv -Path $savePath -NoTypeInformation -Encoding UTF8
}
Registry Script to Install Context Menu Item:
Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\Directory\shell\ExportFileListToCSV] @="Export File List to CSV" [HKEY_CLASSES_ROOT\Directory\shell\ExportFileListToCSV\command] @="powershell -NoProfile -ExecutionPolicy Bypass -File \"C:\\Path\\To\\Script\\ExportFolderToCsv.ps1\" \"%1\""