How to save filename and size in text file?

Gon

Active Member
475
138
43
2014
4,050
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

Code:
C:\test\1
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.

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

filelist.txt will have something like this

Code:
logo.png  30kb
Header.png 4MB

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


Code:
$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
}
 
4 comments

illuminated

Active Member
51
2
8
2011
90
Just put it C:\test\1\ in your browser URL (chrome, firefox) and you get all listed size, format

will do for you?
Post automatically merged:

Or do it like this:
Post automatically merged:

search youtube How to save directory listing to a text file in MS-DOS
 
Last edited:
  • Like
Reactions: Gon

Hyperz

Active Member
2,360
401
83
2009
3,755
Why mess with the garbage that is Powershell when you can do it in Python and run it anywhere:

Python:
#!/usr/bin/env python3

import os


ROOT_FOLDER = r'C:\target_folder'
INDEX_FILENAME = 'filelist.txt'


def get_file_name_and_size(filename, column_width):
    file_size = float(os.path.getsize(filename))
    units = ('B', 'KB', 'MB', 'GB', 'TB', 'PB')
    order, limit = 0, len(units) - 1
    while file_size >= 1024 and order < limit:
        order += 1
        file_size /= 1024
    name = os.path.basename(filename).ljust(column_width)
    return f'{name} {round(file_size)} {units[order]}'


def create_index_text(path):
    folder, sub_folders, sub_files = next(os.walk(path))
    column_width = max(len(fn) for fn in sub_files) if sub_files else 0
    return '\n'.join([
        get_file_name_and_size(os.path.join(path, filename), column_width)
        for filename in sub_files
        if filename != INDEX_FILENAME
    ])


def main():
    print(f'Scanning folder: {ROOT_FOLDER}')
    for sub_folder_name in os.listdir(ROOT_FOLDER):
        path = os.path.join(ROOT_FOLDER, sub_folder_name)
        if os.path.isdir(path):
            print(f'Processing folder: {path}')
            index_txt = create_index_text(path)
            if index_txt:
                filename = os.path.join(path, INDEX_FILENAME)
                with open(filename, 'w') as stream:
                    stream.write(index_txt)
                print(f'Saved file: {filename}')
    print('All done.')


if __name__ == 'main':
    main()

This will list it in a column as well instead of separating file name and size with a single tab.
 
Last edited:
  • Like
Reactions: Gon

Gon

Active Member
475
138
43
2014
4,050
Just put it C:\test\1\ in your browser URL (chrome, firefox) and you get all listed size, format

will do for you?
Post automatically merged:

Or do it like this:
Post automatically merged:

search youtube How to save directory listing to a text file in MS-DOS
windows give file size in bytes which is not good for human eyes
 
Top Bottom