[英]Create text file containing a list of all files of a certain type with their filesize
I want to create a text file with all filenames of a certain filetype plus the filesize, recursively from a specified directory and all subdirectories. 我想创建一个文本文件,其中包含某个文件类型的所有文件名加上文件大小,从指定目录和所有子目录递归。
For example: Listing all .jpg
files plus their sizes from a huge photo-collection. 例如:从巨大的照片集中列出所有.jpg
文件及其大小。
I have found several similar questions, but not this specific listing. 我发现了几个类似的问题,但不是这个特定的列表。
One did this with the full path name, but I don't need this and it would become very long. 一个用完整的路径名做了这个,但我不需要这个,它会变得很长。 Another lists all files, but without size. 另一个列出所有文件,但没有大小。 Another lists all filenames with size, but I can't specify a filetype. 另一个列出了所有具有大小的文件名,但我无法指定文件类型。
This PowerShell
command creates the desired list, but I don't know how to limit it to a certain filetype (eg .jpg
) 此PowerShell
命令创建所需的列表,但我不知道如何将其限制为某种文件类型(例如.jpg
)
gci -rec|?{!$_.PSIsContainer}|%{"$($_.Fullname) $($_.Length)"} >filelist.txt
This batch file
lists all .jpg
's, but without showing the filesize. 此batch file
列出了所有.jpg
,但未显示filesize。
dir /b /s z:\Filme\*.jpg > list1.txt
for /f "tokens=*" %%A in (list1.txt) do echo %%~nxA >> list.txt
del list1.txt
Could anyone edit one of these? 谁能编辑其中一个? so I get the desired list, or come up with a different solution? 所以我得到了所需的清单,或者提出了不同的解决方案?
You know about the %%~nxA
modifier, so I'm a bit surprised you didn't notice the %%~zA
modifier. 你知道%%~nxA
修饰符,所以我有点惊讶你没注意到%%~zA
修饰符。
To simplify it even more, use a for /R
loop and don't use a temp file: 要进一步简化它,请使用for /R
循环,不要使用临时文件:
(for /R %%A in (*.jpg) do echo %%~nxA %%~zA)>list.txt
or if you need the full path\\name, use %%~fA
(explicite) or even just %%A
或者如果您需要完整路径\\名称,请使用%%~fA
(明确)或甚至只使用%%A
Text output: 文字输出:
Get-ChildItem -Path 'X:\PHOTO' -Filter '*.jp*g' -Recurse |
Where-Object {-not $_.PsIsContainer} |
Select-Object Name, Length |
Out-File -FilePath '.\FileList.txt'
CSV output: CSV输出:
Get-ChildItem -Path 'X:\PHOTO' -Filter '*.jp*g' -Recurse |
Where-Object {-not $_.PsIsContainer} |
Select-Object Name, Length |
Export-Csv -Path '.\FileList.csv' -NoTypeInformation
PS I've used *.jp*g
wildcard that will also match *.jpeg
files. PS我使用了*.jp*g
通配符,它也匹配*.jpeg
文件。 Unfortunately, *
wildcard matches zero or more symbols, so you can get files like zzz.jpXXXg
in your list. 不幸的是, *
通配符匹配零个或多个符号,因此您可以在列表中获取zzz.jpXXXg
文件。 There are other ways to filter Get-ChildItem
output that don't suffer from this issue, such as filtering with pipeline and regex but they're slower: Where-Object {$_.Extension -match '^\\.jp[e]{1}g$'}
还有其他方法可以过滤Get-ChildItem
输出而不会遇到此问题,例如使用管道和正则表达式进行过滤但速度较慢: Where-Object {$_.Extension -match '^\\.jp[e]{1}g$'}
Another option would be to not use the -Filter
parameter, but the -Include
instead where the wildcard pattern works as expected, like this: 另一种选择是不使用-Filter
参数,而是使用-Include
而不是通配符模式按预期工作,如下所示:
PowerShell version 3.0 and up PowerShell 3.0及更高版本
Get-ChildItem 'z:\Filme' -File -Include '*.jpg' -Recurse |
Select FullName, Length |
Export-Csv '.\FileList.csv' -NoTypeInformation
PowerShell version below 3.0 PowerShell版本低于3.0
Get-ChildItem 'z:\Filme' -Include '*.jpg' -Recurse |
Where-Object { !$_.PsIsContainer} |
Select FullName, Length |
Export-Csv '.\FileList.csv' -NoTypeInformation
-Include
only works if you also specify -Recurse
or if you have the path end in \\*
like in Get-Childitem 'z:\\Filme\\*'
.
请注意, -Include
仅在您指定-Recurse
或者您的路径以\\*
Get-Childitem 'z:\\Filme\\*'
\\*
结尾时才有效,例如Get-Childitem 'z:\\Filme\\*'
。
Also, -Filter
works faster than -Include
(or -Exclude
) parameters.
此外, -Filter
作品比快-Include
(或-Exclude
)参数。
As stated in the docs :
如文档中所述 :
"Filters are more efficient than other parameters, because the provider applies them when the cmdlet gets the objects. Otherwise, PowerShell filters the objects after they are retrieved." “过滤器比其他参数更有效,因为提供程序在cmdlet获取对象时应用它们。否则,PowerShell会在检索到对象后对其进行过滤。”
You are almost there with the batch script. 你几乎就在那里使用批处理脚本。
%~z1
will display the file size (in bytes). %~z1
将显示文件大小(以字节为单位)。
You can also get rid of the temporary file by using a slightly different version of the for
command. 您还可以使用稍微不同的for
命令版本来删除临时文件。
Use the following batch file: 使用以下批处理文件:
@echo off
setlocal
for /f "tokens=*" %%A in ('dir /b /s z:\Filme*.jpg') do (
if /i "%%~xf" equ ".jpg" echo %%~nxf %%~zf
) > list.txt
endlocal
try this 尝试这个
Get-ChildItem "yourdir" -File -Filter '*.jpg' -Recurse |
Select FullName, Length |
Export-Csv '.\FileList.csv' -NoType
I have never looked into the layout from the Where
command, but if it does not alter between languages/locales, or technically if your layout is not too dissimilar to that of my test system , you could do it on your machine like this: 我从来没有从Where
命令查看布局,但是如果它没有在语言/语言环境之间改变, 或者技术上如果你的布局与我的测试系统的布局没有太大不同 ,你可以在你的机器上这样做:
From the Command Prompt: 从命令提示符:
(For /F "Tokens=1,3*" %A In ('Where/T /R . *.jpg 2^>Nul')Do @Echo("%C","%A")>"list.txt"
From a batch file: 从批处理文件:
@(For /F "Tokens=1,3*" %%A In ('Where/T /R . *.jpg 2^>Nul')Do @Echo("%%C","%%A")>"list.txt"
Obviously if the layout from your Where
command output differs there's still a possibility to adjust the Tokens
and/or include delimiters to suit your target system. 显然,如果Where
命令输出的布局不同,则仍有可能调整Tokens
和/或包含分隔符以适合您的目标系统。
In the examples above, I've used .
在上面的例子中,我使用过.
to represent the current directory, you could of course change that to another relative path, eg ..\\Pictures
, or full path, eg C:\\Users\\Patrick\\Pictures
as necessary. 为了表示当前目录,您当然可以将其更改为另一个相对路径,例如..\\Pictures
或完整路径,例如C:\\Users\\Patrick\\Pictures
。
And a powershell option: 还有一个powershell选项:
Ls -Filt '*.jpg' -Fo -Rec -EA SilentlyContinue|?{!$_.PSIsContainer -And $_.Extension -Eq '.jpg'}|Select FullName,Length|ConvertTo-CSV -NoT|Select -Skip 1|SC '.\list.txt'
This will also include eg system and hidden files, will not include files with extensions other than .jpg
and will not include an unrequested header with that listing. 这还包括例如系统和隐藏文件,不包括扩展名不是.jpg
文件,并且不包含带有该列表的未请求标题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.