Home > Back-end >  How to right align the output of the file size in a custom directory listing?
How to right align the output of the file size in a custom directory listing?

Time:08-21

I am trying to make a custom directory listing in batch using for loops.

This is the code:

for /F "tokens=* UseBackQ" %%g IN (`dir /b %2`) do (   
    echo  ^| %%~ag %%~tg %%~zg %%~nxg && echo  ^|
)

The output with %2 being E:\ is:

 | d---------- 2022-08-19 02:14 PM 0 AppTemp
 |
 | --a-------- 2022-08-06 11:40 PM 971 learned.py
 |
 | --a-------- 2022-08-19 05:00 PM 4269 main.cmd
 |

I would like the file size to be like this:

 | d---------- 2022-08-19 02:14 PM    0 AppTemp
 |
 | --a-------- 2022-08-06 11:40 PM  971 learned.py
 |
 | --a-------- 2022-08-19 05:00 PM 4269 main.cmd
 |

How can I make it like this?

CodePudding user response:

There could be used the following a little bit commented batch file code:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem Make the directory passed as second argument the current directory
rem checking if a second, non-empty argument string is passed at all to
rem the batch file and with checking if that is really a directory name.

set "RestoreCurrentDirectory="
if "%~2" == "" (
    echo No directory specified as second argument, listing the directory:
    echo "           
  • Related