简体   繁体   English

创建文本文件,其中包含特定类型的所有文件及其文件大小的列表

[英]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


Note that -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会在检索到对象后对其进行过滤。”

Could anyone edit one of these so I get the desired list? 任何人都可以编辑其中一个,所以我得到了所需的列表?

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

Further Reading 进一步阅读

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 option: 还有一个选项:

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.

相关问题 需要Powershell脚本来列出目录和子目录中所有文件名以及文件大小 - Need Powershell script to list all files name with filesize in a directory and subdirecties 在包含文件夹中所有文件的版本号的inPowesShell中创建.txt文件 - Create .txt file inPowesShell containing Version number for all files in folder 将大型目录树中的所有文件大小归零(删除文件内容,保留文件) - Zero all filesize in a large directory tree (delete file content, keep files) 使用文件大小列出给定结构中的所有文件夹和子文件夹 - List all folders and subfolders in a given structure with filesize PowerShell:列出除某些文件以外的所有项目 - PowerShell: List all items except for certain files 使用 PowerShell,读取多个已知文件名,附加所有文件的文本,创建并写入一个输出文件 - Using PowerShell, read multiple known file names, append text of all files, create and write to one output file 从Powershell递归归档某种类型的所有文件 - Archive all files of a certain type recursively from powershell Powershell脚本可在所有驱动器中创建文本文件 - Powershell script to create text files in all the drives 在一个巨大的文本文件中获取所有包含字符串的行 - 尽可能快? - Get all lines containing a string in a huge text file - as fast as possible? 将遵循某些模式的所有文件中的某些文本递归替换为另一种文本 - Recursively replace certain text in all files following certain pattern with another text
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM