How to save filename and size in text file?

I am not sure if it’s possible, i have kind of zero idea to create powershell script. it will be really helpful for me if someone can help me.

I have multiple sub folders, lets say

C:\test\2
C:\test\3

inside the folder there is multiple image files. i want to list the sub folder file name and size and save text files inside the folders.

C:\test\1\filelist.txt
C:\test\2\filelist.txt
C:\test\3\filelist.txt

filelist.txt will have something like this

logo.png  30kb
Header.png 4MB

here one sample script i found, wondering if some can help

$Folder = 'C:\pic'
$Output = 'C:\output.txt'
$Files = Get-ChildItem -Path $Folder -Filter *.mp4 -File
$objShell = New-Object -ComObject Shell.Application 
$objFolder = $objShell.Namespace($Folder)
foreach( $File in $Files ) {
    $objFile = $objFolder.ParseName($File)
    $Name = $objFolder.GetDetailsOf($objFile, 0)
    $Size = $objFolder.GetDetailsOf($objFile, 1)
    $Length = $objFolder.GetDetailsOf($objFile, 27)
    $Tab = [char]9
    "$Name$Tab$Size$Tab$Length" | Out-File -Append -FilePath $Output
}
1 Like

sam2020,
Welcome back to the forum.

What’s wrong with the script? If you found it somewhere why don’t you ask the author of the script to help you? Have you tried to modify/fix the script to make it do what you want?

thank you Olaf.

i found it on stackoverflow.com which is very old post. i doubt if the author still active. so i thought it might be better if someone can help me here. i also asked this problem in stackoverlfow as well, from there no reply yet though.

===

there is an ugly way i can do it. i really don’t know powershell language.

  1. make batch script using mediainfo which will create separate text file for each media file. if there is 3 media file it will create 3 text file which will include file name - size
    2.next join those file using type *.txt > file.txt
    however there is another issue if you use type for joining text file in same directory it will create double line. so i made mediainfo output file extension .csv which works fine with concatenation without adding info twice.
  2. next you can use another batch script to remove file with .csv extension.

3 step actually. it’s bit ugly but output gives fine. however i still prefering powershell script if anyone can make one for me :smiley:

============

another way is possible using cmd but cmd giving file size in byte format which i don’t like.

When you crosspost the same question at the same time to different forums you should at least post links to the other forums along with your question to avoid people willing to help you making their work twice or more.
Thanks in advance.

We PowerSheller do not like something like this. :wink: … and since this is a PowerShell forum we will not create a crappy batch solution. :wink:

Since most of us do earn their money with such services we will not offer something like this here for free … sorry. But we will be happy to assist you with your own code.

If that’s not the best chance to start learning PowerShell … :wink: :slightly_smiling_face:

If you don’t mind we could “develop” your first PowerShell script together.

What I think you will need for your task is a nested loop. In the first step - the outer loop - you enumerate the subfolders you want to treat. In PowerShell you can do this with the cmdlet

Please always read the complete help for the cmdlets including the examples to learn how to use them.
In this case we only need the folders - not the files.

The next step would be to iterate over the subfolders outputted by the first step. We can do this with the cmdlet

Inside this loop we would need to enumerate the files contained in the single subfolder. We do this with Get-ChildItem again. But this time we need the files - not folders. :point_up_2:t4:

Try to write the code for these steps and then we will continue. :+1:t4: :wink: :slightly_smiling_face:

2 Likes

i rather would skip to learn :sweat_smile: even if i learn, i will hardly use this knowledge and probably will forget. it’s like one~two times script need but you took some time to write everything and find the links, i guess i should give a try. but i don’t think i will i have enough knowledge to create any script…later you might get mad at me or something :grinning_face_with_smiling_eyes:

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_filesystem_provider?view=powershell-7.1#attributes-flagsexpression

Directory <System.Management.Automation.SwitchParameter>

$Folder we set the parent directory path. i think we can use something like to read directory:

Get-ChildItem -path $Folder -Directory or -Attributes

and i have to use -Recurse -Force

-Recurse is for sub directory but i don’t know what is for -Force using.

sorry i am not sure here is something i think:

=====================

$Folder=set path
$Fileextension=“*.png”
Get-ChildItem -path $Folder -Directory -Recurse -Force | ForEach-Object {

i am not sure there is something i can use to get sub directory files

Get-ChildItem -File -Path $_.FullName -Filter $Fileextension

sorry :pensive:

Who knows? :wink: May be you like it and you become an expert and start a new career!? :wink:

I’d be disappointed probably. :stuck_out_tongue_winking_eye:

Don’t worry. You’re half way there.

I added a lot of actually unnecessary and superfluous comments to explain what the code does. Usually we don’t do that because PowerShell code is actually pretty desciptive in itself. And when you remove the comments the code gets actuyll easier to read. :+1:t4:

$RootFolder = 'C:\samplePath'  
$FileExtension = '*.png'
Get-ChildItem -Path $RootFolder -Directory -Recurse |  # return only the folder objects from all subfolders
ForEach-Object {                                       # for every single folder returned do the following ... 
    $CurrentFolder = $_.FullName                       # save the folderpath for later use
    $FileList = Get-ChildItem -Path $CurrentFolder -Filter $FileExtension # return all png files from the currnet 
                                                                          # folder and save this list in a variable
    if ($FileList) {                                   # if there is a t least one png file do the following ... 
        $CsvFileName = Join-Path -Path $CurrentFolder -ChildPath 'Inventory.csv' # create the output file name
        $FileList |                                    # output what's saved in the variable
            Select-Object -Property Name,@{Name = 'SizeKB';Expression={[Math]::Round($_.Length/1KB,2)}} | # 
                                                       # output only the file name and the size in KB rounded to 2 decimal points
                Export-Csv -Path $CsvFileName -NoTypeInformation  # output the list of png files to a file "inventory.csv" in the current folder
    }
}

I decided t o output the inventory as CSV file. Of course you can change this if you want. Instead you could use

to output the list of png files in a plain text file. Of course you should adjust the filename then accordingly. :wink:

1 Like

thank you so much :slight_smile: just a question can we use size like MB/GB/TB?
like if a file is in kb=show in kb size but if it’s more than 1mb=it will show size in MB?

 Select-Object -Property Name,@{Name = 'SizeKB';Expression={[Math]::Round($_.Length/1KB,2)}}

Let me ask you something first. I don’t know what kind of file type you’re looking for with this project but do you really expect to see files with a size of more than 1 TB?? :wink:

That’s possible. But I’m about to leave for a day trip to enjoy life and nature. So you have all day to search for a solution by yourself. :stuck_out_tongue_winking_eye:

Have a really nice day! :wink:

2 Likes