Explanation of variable with ~ syntax

I got this batch script on the web for listing files with their sizes and there is one part I do not fully understand, so I was hoping someone could explain it to me
 @echo off
set /A Total=0, Cnt=0
for /F "tokens=*" %%A in ('dir %1 /S /B /A-D') do call :show "%%A"
goto :EOF
set "Size= %~z1"
echo %~f1 %~t1 %Size:~-10% >> c:\test\files.txt
goto :eof

The part I don't understand is in the 3rd last line of the script:

Is the whole variable everything between the two % characters?
I understand that the %Size is the Size variable which is set to the file size in the previous line, but I don't understand what the :~10% bit is doing - so please advise


LVL 17
Barry CunneyAsked:
Who is Participating?
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Correction to it_saige's post:

   %Size:~-10% adds 10 charactors to the front of the Size variable.  Like an align right if you will.

That is not correct. %Size:~-10% just starts ten positions from the right of %Size%.
%A:~pos,len%    results in the substring starting at (null-based) position pos, having length len. Negative positions will count from the right instead of the left.
set A=1234567890
echo %A:~1,1%    echoes  2
echo %A:~-2%     echoes   90

To get a explanation, you don't need a link, just call
  set /?
   for /?

in a DOS prompt and you will see all "substitution" variants.
I think this link will give  you the answer  you are looking for:

This link is probably a little easier to understand, straight from Microsoft.

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

%~ is an expanding charactor.

%~n expands or add's too something, i.e.

%~1 - Expands %1 removing any surrounding quotes
%~f1 - Expands %1 to a fully qualified path name

In the above, your script uses

%~z1 which Expands %1 to the size of the file.
%~f1 which Expands %1 to a fully qualified path name.
%~t1 which Expands %1 to the date/time of the file.
%Size:~-10% adds 10 charactors to the front of the Size variable.  Like an align right if you will.



Barry CunneyAuthor Commented:
I was looking at these links earlier but I could not see any explicit explanation for the :~-10%
 syntax or even for ~- - maybe you can point it out to me in case I am missing something?
Barry CunneyAuthor Commented:
Thanks it_saige - my previous cpmment was for patmacs links not yours - I am looking in to more details what you posetdd now but it looks good
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.