Glam Prestige Journal

Bright entertainment trends with youth appeal.

For example, suppose I want to see the file sizes of all the *.txt files in a tree. Currently, I can do:

dir *.txt /s

But the list is not formatted well and is hard to use.

I would like to know if the following example is possible using CMD (or if necessary, PowerShell):

C:\files>dir cache.sqlite /s Volume in drive C is HDD Volume Serial Number is 6789-ABCD
01/02/2005 00:01 AM 65,536 C:\files\about+addons\cache.sqlite
02/03/2006 01:04 AM 98,304 C:\files\app.asana.com\cache.sqlite
03/04/2007 02:07 AM 2,162,688 C:\files\app.slack.com\cache.sqlite
04/05/2008 03:10 AM 98,304 C:\files\bootstrap-vue.org\cache.sqlite
05/06/2009 04:13 AM 196,608 C:\files\dev.to\cache.sqlite
06/07/2010 05:16 AM 65,536 C:\files\developer.mozilla.org\cache.sqlite
07/08/2011 06:19 AM 98,304 C:\files\phys.org\cache.sqlite
08/09/2012 07:22 AM 786,432 C:\files\stackdev.io\cache.sqlite
09/10/2013 08:25 AM 458,752 C:\files\todo.zenkit.com\cache.sqlite
10/11/2014 09:28 AM 98,304 C:\files\
11/12/2015 10:31 AM 98,304 C:\files\
12/13/2016 11:34 AM 65,536 C:\files\
01/14/2017 12:37 PM 98,304 C:\files\
02/15/2018 13:40 PM 327,680 C:\files\
03/16/2019 14:43 PM 65,536 C:\files\
04/17/2020 15:46 PM 98,304 C:\files\ Total Files Listed: 24 File(s) 4,882,432 bytes 16 Dir(s) 208,862,339,072 bytes free
5

4 Answers

You can use get-childitem and select the file property values with or without using calculated properties to assist in getting the desired output values.

Recursive with file fullname and a calculated property "MB Size"

Get-ChildItem -Path "C:\files\*.txt" -Recurse |
Select @{Name="MB Size";Expression={ "{0:N1}" -f ($_.Length / 1MB) }}, Fullname, LastWriteTime;

Output

MB Size FullName LastWriteTime
------- -------- -------------
21.2 C:\Files\Cell356.txt 2/24/2021 8:33:58 AM
0.9 C:\Files\Test123\best.txt 2/24/2021 8:16:29 AM
5.1 C:\Files\Test123\Test321\test.txt 2/24/2021 6:19:08 AM
0.5 C:\Files\Zeta\Cool123.txt 2/24/2021 6:13:05 AM
4.0 C:\Files\ZZettaa\zest.txt 2/24/2021 8:57:58 PM

Non-Recursive with file name and a calculated property "MB Size"

Get-ChildItem -Path "C:\files\*.txt" |
Select @{Name="MB Size";Expression={ "{0:N1}" -f ($_.Length / 1MB) }}, Name, LastWriteTime;

Output Example

MB Size Name LastWriteTime
------- ---- -------------
21.2 Cell356.txt 2/24/2021 8:33:58 AM
0.9 best.txt 2/24/2021 8:16:29 AM
5.1 test.txt 2/24/2021 6:19:08 AM
0.5 Cool123.txt 2/24/2021 6:13:05 AM
4.0 zest.txt 2/24/2021 8:57:58 PM

Recursive with parent folder removal

$sdir = "c:\temp\stuff\";
Get-ChildItem -Path $sdir -Filter "*.txt" -File -Recurse |
Select @{Name = "MB Size"; Expression = { "{0:N1}" -f ($_.Length / 1MB) }}, @{Name = "sDir"; Expression = { $_.FullName.Replace($sdir,"") }}, Fullname, LastWriteTime

Supporting Resources

3

powershell will give you the best results. Run this and open the results in excel. Edit the paths as needed.

Get-ChildItem -recurse -path c:\files\*.txt | export-csv c:\files\here.csv
3

i am new in batch maybe this can help youfor current directory

for %F in (*.txt) do @echo %~zF %F

for subdirectories

for /f %F in ('dir /s /r /b *.txt') do @echo %~zF %F

Output

163197 O:\170801072959IMG-20170622-WA0006.txt
729213 O:\170805042647DSC033881.txt
679900 O:\170805045024crop.txt
723650 O:\170805071247temp.txt
66632 O:\170817035334DSC03625_comp.txt
89448 O:\170817035545DSC03569_comp.txt
84730 O:\pics\151104051533IMG_20151103_111229.txt
1

Thanks to the direction from a few people here, I was able to cobble everything together into (almost) exactly what I wanted:

# check for arguments
if (!$args) { echo "Please specify [path\]filespec. i.e., c:\temp\*.txt or *.txt"; return }
# get the path only
$path = Split-Path $Args | Where-Object {$_ -ne ''}| Convert-Path
# if no path was specified, use the current directory
$path = IF ([string]::IsNullOrWhitespace($path)){ $pwd.path } else { $path }
# get the filespec (*.txt)
$filespec = Split-Path $Args -Leaf
write-output "`nShowing all $filespec contained under $path"
Get-ChildItem -Path $path -Filter $filespec -File -Recurse |
Format-Table -Wrap @{ Name="Date"; Expression = { $_.LastWriteTime.ToString("MM/dd/yyyy hh:mm:ss ") }}, @{ Name="Size"; Expression = { "{0,15:N0}" -f $_.Length }; Alignment="right"; }, @{ Name="File"; Expression = { ($_.FullName -ireplace [regex]::Escape($path), "").TrimStart("\") }}

There are no totals at the end, but that's fine.

PS C:\> C:\utils\sdir.ps1 \users\default\documents\*.txt
Showing all *.txt contained under C:\users\default\documents
Date Size File
---- ----- ----
11/14/2013 01:34:29 970 in\text\people.txt
08/27/2013 11:43:48 498 in\text\review.txt
08/14/2020 05:55:20 176 store\Documentation\Temporary0.txt
08/14/2020 05:55:20 52 store\Documentation\Temporary1.txt
08/14/2020 05:55:21 559 store\Documentation\Logs\Debug log.txt
01/20/2015 04:09:15 5,610 Letters\Letter.txt
12/30/2008 01:43:40 2,212 Misc\Poster.txt
07/30/2011 11:46:58 918 Misc\comments.txt
08/14/2010 04:13:04 523 Misc\Daily.txt
08/31/2010 08:51:24 2,840 Misc\review\overview.txt
01/05/2009 08:04:52 1,768 Misc\review\other.txt

The only issue is if you specify a directory without a filespec (e.g., c:\temp as opposed to c:\temp\*.*) and I would also like more spacing between the columns.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy