0

I'm trying to find a way to echo x number of dots based on the length of the file name.

In the Batch-script I'm making, there is a part where it ECHO's the file name and size of a file.

The problem is that none of the file names do have the same amount of characters.

45545.ext.................8.23 MB
12341231.ext..............5.87 MB
543646767676.ext..........6.23 MB
34563456345634563.ext.....3.87 MB

Let's say I want 5 dots after the longest file name, then I need an X amount of dots after the shorter file names to get the the size of the files on the same line below each other, as shown above.

I've tried with a script I found on StackOverflow that counts the length of the file name. I'm not sure if I can use a "counter" to to this or not.

Thanks in advance! :)

MokiTa
  • 3,840
  • 2
  • 13
  • 15
  • Just out of curiosity, where's the script you found that counts the length of file names? – rojo Dec 31 '14 at 02:10
  • Here :) http://stackoverflow.com/a/8566001/1013586 – MokiTa Dec 31 '14 at 02:13
  • Ah. Well, feel free to replace my `:length` function with your own. :) My answer should at least demonstrate the logic of determining how many dots is needed anyway. – rojo Dec 31 '14 at 02:15
  • Okay :) Thank you so much for the answer! I'm going to try it out :) – MokiTa Dec 31 '14 at 02:24
  • I just ran some tests, and for a dir containing 35 files my `for /l` loop is 150ms faster than the method you linked. jeb's method was about 40ms faster, but didn't work for some reason (although the logic looks sound). foxidrive's answer below was about 4x faster, but doesn't calculate any dot fill variation based on the longest filename length. – rojo Dec 31 '14 at 04:03

3 Answers3

2

This is a simple way to pad the right hand side of the filename with periods.
You do have to cater for the maximum length filename and so change the 80 below.

@echo off
setlocal enabledelayedexpansion
set "line="
for /L %%a in (1,1,1000) do set line=!line!.
for %%a in (*) do (
   set "var=%%a%line%"
   echo !var:~0,80!  %%~za
)
pause
foxidrive
  • 37,659
  • 8
  • 47
  • 67
  • Thank, this was nice too! But is there a way to use special characters like Æ,Ø,Å,æ,ø,å,è? EDIT: Nevermind, I just added `chcp 1252` :) – MokiTa Dec 31 '14 at 02:53
1

You can use variable substrings with a for /l loop to determine the length of a string. Keep looping until x number of characters of filename is equal to filename, then you've found your length.

Loop through all files in the directory to find the longest. Then add 5 to that to make sure the longest file still has 5 dots. Then for each file in the directory, that number minus length is the number of dots you need.

Easy-peasy lemon squeezey.

@echo off
setlocal enabledelayedexpansion

:: get longest filename in directory
set longest=0
for %%I in (*) do (
    call :length "%%~I" len
    if !len! gtr !longest! set longest=!len!
)

:: Dot fill each line
for %%I in (*) do (
    call :length "%%~I" len
    set /a dots=%longest% + 5 - len
    <NUL set /P "=%%~I"
    call :dots !dots!
    echo %%~zI bytes
)

:: end main script
goto :EOF

:length <filename> <var_to_set>
setlocal enabledelayedexpansion
set "tmpfile=%~1"
for /l %%I in (1,1,100) do (
    if "!tmpfile!"=="!tmpfile:~-%%I!" (
        endlocal && set "%~2=%%I"
        goto :EOF
    )
)

:dots <number_of_dots>
setlocal
for /l %%I in (1, 1, %~1) do <NUL set /P "=."
goto :EOF

Note: This script assumes you won't have any filenames longer than 100 characters. If you might, then increase the 100 in the for /l loop in the :length subroutine.

If you want a speed-optimized :dots subroutine, replace the last four lines of the script with the following:

:dots <number_of_dots>
setlocal enabledelayedexpansion
set "dots=...................................................................................................."
<NUL set /P "=!dots:~-%1!"
goto :EOF
rojo
  • 22,626
  • 5
  • 47
  • 93
  • I feel so stupid that didn't write this in my question, but is there a (simpler) way to do this in PowerShell too? I testet the script btw, awesome! – MokiTa Jan 13 '15 at 17:55
  • I'm sure there is a simpler way to write this in PowerShell, but I'd be doing you a disservice by attempting to discover it. You'd be better off posting it as a new question with the PowerShell tag. My PowerShell-fu is not strong. – rojo Jan 13 '15 at 23:29
0

As the user "Jeb" stated, there is no native way to get the length a string. You must write your own: How do you get the string length in a batch file?

Please also refer to this question for looping in a batch file: Batch script loop

Community
  • 1
  • 1
  • This one works fine for me. But that's not really the question. What I am wondering about is how I can Echo x number of dots after the file name, so I can get size of the files on the same line below eachother. `set "strvar=Count me 1234"` `ECHO %strvar%> tempfile.txt` `FOR %%? IN (tempfile.txt) DO ( SET /A strlength=%%~z? - 2 )` `echo %strlength%` – MokiTa Dec 31 '14 at 02:02