Patrick Breuer
Patrick Breuer

Reputation: 11

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.

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 (e.g. .jpg)

gci -rec|?{!$_.PSIsContainer}|%{"$($_.Fullname) $($_.Length)"} >filelist.txt

This batch file lists all .jpg's, but without showing the 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?

Upvotes: 1

Views: 1708

Answers (6)

Esperento57
Esperento57

Reputation: 17462

try this

Get-ChildItem "yourdir" -File -Filter '*.jpg' -Recurse | 
    Select FullName, Length | 
        Export-Csv '.\FileList.csv' -NoType

Upvotes: 0

Theo
Theo

Reputation: 61058

Another option would be to not use the -Filter parameter, but the -Include instead where the wildcard pattern works as expected, like this:

PowerShell version 3.0 and up

Get-ChildItem 'z:\Filme' -File -Include '*.jpg' -Recurse | 
    Select FullName, Length | 
    Export-Csv '.\FileList.csv' -NoTypeInformation

PowerShell version below 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\*'. Also, -Filter works faster than -Include (or -Exclude) parameters. 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."

Upvotes: 1

beatcracker
beatcracker

Reputation: 6920

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:

Get-ChildItem -Path 'X:\PHOTO' -Filter '*.jp*g' -Recurse | 
    Where-Object {-not $_.PsIsContainer} | 
        Select-Object Name, Length | 
            Export-Csv -Path '.\FileList.csv' -NoTypeInformation

P.S. I've used *.jp*g wildcard that will also match *.jpeg files. Unfortunately, * wildcard matches zero or more symbols, so you can get files like zzz.jpXXXg in your list. 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$'}

Upvotes: 1

Compo
Compo

Reputation: 38604

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:

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.

In the examples above, I've used . to represent the current directory, you could of course change that to another relative path, e.g. ..\Pictures, or full path, e.g. C:\Users\Patrick\Pictures as necessary.


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 e.g. system and hidden files, will not include files with extensions other than .jpg and will not include an unrequested header with that listing.

Upvotes: 0

Stephan
Stephan

Reputation: 56180

You know about the %%~nxA modifier, so I'm a bit surprised you didn't notice the %%~zA modifier.

To simplify it even more, use a for /R loop and don't use a temp file:

(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

Upvotes: 1

DavidPostill
DavidPostill

Reputation: 7921

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).

You can also get rid of the temporary file by using a slightly different version of the for command.

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

Upvotes: 1

Related Questions