Search code examples
windowsdir

Showing only filename and file size windows dos


This may sound easy to you all, I'm just trying to list the list of the files in a directory and their size only. I tried bunch of options dir / help, but don't seem to be able to get it right. Can you help please? I also looked around but all I find are reference to diretory size.

       I want the out put to look like this

       a.txt  21
       b.txt  72
       3.txt  73

Thanking you in advance


Solution

  • From the command line:

    for %F in (*) do @echo %F     %~zF
    

    Double up the percents if used within a batch file.

    @echo off
    for %%F in (*) do echo %%F     %%~zF
    

    For fixed width, it probably is better to list the size first, since that width does not vary so much. Twelve digits is almost enough for 1 terabyte. File name length can vary a lot.

    Here is a batch script listing size in bytes (up to 12 digits), followed by file name:

    @echo off
    setlocal disableDelayedExpansion
    
    for %%F in (*) do (
      set "name=%%F"
      set "size=            %%~ZF"
      setlocal enableDelayedExpansion
      echo !size:~-12! !name!
      endlocal
    )
    

    The toggling of delayed expansion is to prevent corruption of the file name if it contains the ! character. Any FOR variable value is corrupted if delayed expansion is enabled and the value contains !.