{"id":380,"date":"2025-10-22T16:23:37","date_gmt":"2025-10-22T22:23:37","guid":{"rendered":"https:\/\/blog.d5.ca\/?p=380"},"modified":"2026-01-12T15:52:00","modified_gmt":"2026-01-12T22:52:00","slug":"export-the-list-of-files-in-a-folder-directory-to-csv","status":"publish","type":"post","link":"https:\/\/blog.d5.ca\/?p=380","title":{"rendered":"Export the list of files in a folder\/directory to CSV"},"content":{"rendered":"<p>Here are a couple of methods to export the list of files in a directory into a CSV for analysis.<!--more--><\/p>\n<h3>1. Powershell (Wrapped for use in CMD.exe)<\/h3>\n<p>The following command is a wrapped Powershell script that will run from a cmd.exe window.<\/p>\n<pre><!--ScriptorStartFragment-->powershell -Command \"Get-ChildItem -Path . -File | Select-Object Name,@{Name='SizeBytes';Expression={$_.Length}},@{Name='LastModified';Expression={$_.LastWriteTime}} | Export-Csv -Path '.\\file_list.csv' -NoTypeInformation\"<!--ScriptorEndFragment--><\/pre>\n<h4>Output:<\/h4>\n<ul>\n<li><strong>Name<\/strong>: File name<\/li>\n<li><strong>SizeBytes<\/strong>: File size in bytes<\/li>\n<li><strong>LastModified<\/strong>: Timestamp of the last modification<\/li>\n<\/ul>\n<h3>2. Native CMD.exe Prompt<\/h3>\n<p>The following is a native command line that can be run from the cmd.exe prompt.<\/p>\n<pre><!--ScriptorStartFragment-->(for \/R %f in (*) do @echo %~pf%~nxf) &gt; file_list.csv<!--ScriptorEndFragment--><\/pre>\n<p>Or to export recursively through subfolders as well:<\/p>\n<pre><!--ScriptorStartFragment-->(for \/R %f in (*) do @echo \"%~nxf\",%~zf,\"%~tf\") &gt; file_list.csv<!--ScriptorEndFragment--><\/pre>\n<h4>Explanation:<\/h4>\n<ul>\n<li><code>%~nxf<\/code> \u2014 filename with extension<\/li>\n<li><code>%~zf<\/code> \u2014 file size in bytes<\/li>\n<li><code>%~tf<\/code> \u2014 last modified date\/time<\/li>\n<li><code>for \/R<\/code> \u2014 recursively processes all files in subdirectories<\/li>\n<li>Output is saved to <code>file_list.csv<\/code> in the current directory<\/li>\n<\/ul>\n<h3>4. Explorer Context Item<\/h3>\n<p>The following registry script creates a context item for folders in Windows Explorer titled &#8220;Export File List to CSV&#8221;. It will prompt whether to include subfolders and will ask for the save path of the resulting CSV.<\/p>\n<pre>Windows Registry Editor Version 5.00\r\n\r\n[HKEY_CLASSES_ROOT\\Directory\\shell\\ExportFileListToCSV]\r\n@=\"Export File List to CSV\"\r\n\r\n[HKEY_CLASSES_ROOT\\Directory\\shell\\ExportFileListToCSV\\command]\r\n@=\"powershell -NoProfile -ExecutionPolicy Bypass -Command \\\"&amp; { $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} }\\\"\"<\/pre>\n<p>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.<\/p>\n<h4>Powershell Script: <em>ExportFolderToCsv.ps1<\/em><\/h4>\n<pre># Script to create a CSV listing the contents of a folder\r\n# Prompts whether to include subfolders\r\n# Asks for destination .csv path.\r\nparam(\r\n[string]$Folder\r\n)\r\n\r\n$includeSub = Read-Host 'Include subfolders? (Y\/N)'\r\n$recurse = $includeSub -match '^[Yy]'\r\n$defaultPath = Join-Path (Split-Path $Folder -Parent) ((Split-Path $Folder -Leaf)+'.csv')\r\n\r\ndo {\r\n$savePath = Read-Host ('Enter full path for CSV file (default: '+$defaultPath+')')\r\nif ([string]::IsNullOrWhiteSpace($savePath)) { $savePath = $defaultPath }\r\n$exists = Test-Path $savePath\r\nif ($exists) { Write-Host 'File already exists. Please enter a different path.' }\r\n} while ($exists)\r\n\r\nif ($recurse) {\r\nGet-ChildItem -Path $Folder -Recurse | Select-Object `\r\n@{Name='RelativePath';Expression={ if ($_.PSIsContainer) { ($_.FullName).Substring($Folder.Length+1) + '\\' } else { ($_.FullName).Substring($Folder.Length+1) } }},\r\n@{Name='FileName';Expression={ if ($_.PSIsContainer) { $_.Name + '\\' } else { $_.Name } }},\r\n@{Name='Size';Expression={$_.Length}},\r\n@{Name='DateModified';Expression={$_.LastWriteTime}} |\r\nExport-Csv -Path $savePath -NoTypeInformation -Encoding UTF8\r\n} else {\r\nGet-ChildItem -Path $Folder | Select-Object `\r\n@{Name='FileName';Expression={ if ($_.PSIsContainer) { $_.Name + '\\' } else { $_.Name } }},\r\n@{Name='Size';Expression={$_.Length}},\r\n@{Name='DateModified';Expression={$_.LastWriteTime}} |\r\nExport-Csv -Path $savePath -NoTypeInformation -Encoding UTF8\r\n}<\/pre>\n<h4>Registry Script to Install Context Menu Item:<\/h4>\n<pre>Windows Registry Editor Version 5.00\r\n\r\n[HKEY_CLASSES_ROOT\\Directory\\shell\\ExportFileListToCSV]\r\n@=\"Export File List to CSV\"\r\n\r\n[HKEY_CLASSES_ROOT\\Directory\\shell\\ExportFileListToCSV\\command]\r\n@=\"powershell -NoProfile -ExecutionPolicy Bypass -File \\\"C:\\\\Path\\\\To\\\\Script\\\\ExportFolderToCsv.ps1\\\" \\\"%1\\\"\"<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here are a couple of methods to export the list of files in a directory into a CSV for analysis.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_links_to":"","_links_to_target":""},"categories":[25],"tags":[55,68,66,63,64,42,67,37],"class_list":["post-380","post","type-post","status-publish","format-standard","hentry","category-reference","tag-command-line","tag-context-menu","tag-explorer","tag-folders","tag-powershell","tag-registry","tag-shell-extensions","tag-windows"],"_links":{"self":[{"href":"https:\/\/blog.d5.ca\/index.php?rest_route=\/wp\/v2\/posts\/380","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.d5.ca\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.d5.ca\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.d5.ca\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.d5.ca\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=380"}],"version-history":[{"count":5,"href":"https:\/\/blog.d5.ca\/index.php?rest_route=\/wp\/v2\/posts\/380\/revisions"}],"predecessor-version":[{"id":394,"href":"https:\/\/blog.d5.ca\/index.php?rest_route=\/wp\/v2\/posts\/380\/revisions\/394"}],"wp:attachment":[{"href":"https:\/\/blog.d5.ca\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=380"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.d5.ca\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=380"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.d5.ca\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=380"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}