例如,假设我想查看*.txt
树中所有文件的文件大小。目前,我可以这样做:
dir *.txt /s
但该列表的格式不太好,难以使用。
我想知道是否可以使用 CMD(或必要时使用 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\www.bing.com\cache.sqlite
11/12/2015 10:31 AM 98,304 C:\files\www.infoworld.com\cache.sqlite
12/13/2016 11:34 AM 65,536 C:\files\www.oracle.com\cache.sqlite
01/14/2017 12:37 PM 98,304 C:\files\www.popularmechanics.com\cache.sqlite
02/15/2018 13:40 PM 327,680 C:\files\www.sitepoint.com\cache.sqlite
03/16/2019 14:43 PM 65,536 C:\files\www.yahoo.com\cache.sqlite
04/17/2020 15:46 PM 98,304 C:\files\www.zdnet.com\cache.sqlite
Total Files Listed:
24 File(s) 4,882,432 bytes
16 Dir(s) 208,862,339,072 bytes free
答案1
您可以使用获取子项并选择文件属性值,使用或不使用计算属性以协助获取所需的输出值。
使用文件全名和计算属性“MB Size”进行递归
Get-ChildItem -Path "C:\files\*.txt" -Recurse |
Select @{Name="MB Size";Expression={ "{0:N1}" -f ($_.Length / 1MB) }}, Fullname, LastWriteTime;
输出
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
具有文件名和计算属性“MB Size”的非递归
Get-ChildItem -Path "C:\files\*.txt" |
Select @{Name="MB Size";Expression={ "{0:N1}" -f ($_.Length / 1MB) }}, Name, LastWriteTime;
输出示例
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
递归删除父文件夹
$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
支持资源
- 获取子项
- 选择对象
Select-Object 的标准别名:
select
- 在 PowerShell 中使用 Select-Object 添加计算属性
- 代替()
答案2
powershell 将为您提供最佳结果。运行此程序并在 excel 中打开结果。根据需要编辑路径。
Get-ChildItem -recurse -path c:\files\*.txt | export-csv c:\files\here.csv
答案3
我是新来的,也许这可以帮助你 对于当前目录
for %F in (*.txt) do @echo %~zF %F
对于子目录
for /f %F in ('dir /s /r /b *.txt') do @echo %~zF %F
输出
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
答案4
感谢这里一些人的指导,我能够把所有东西拼凑成(几乎)我想要的东西:
# 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("\") }}
最后没有总数,不过没关系。
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
唯一的问题是,如果您指定一个没有文件规范的目录(例如,c:\temp
与相反c:\temp\*.*
),并且我还希望列之间有更多的间距。