[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Explanation of variable with ~ syntax

Posted on 2011-02-25
6
Medium Priority
?
387 Views
Last Modified: 2013-11-05
Hi
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
c:
cd\
set /A Total=0, Cnt=0
for /F "tokens=*" %%A in ('dir %1 /S /B /A-D') do call :show "%%A"
goto :EOF
:show
set "Size= %~z1"
echo %~f1 %~t1 %Size:~-10% >> c:\test\files.txt
goto :eof
PAUSE

The part I don't understand is in the 3rd last line of the script:
%Size:~-10%

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

Thanks

0
Comment
Question by:Barry Cunney
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 11

Expert Comment

by:Patmac951
ID: 34984317
I think this link will give  you the answer  you are looking for:

http://stackoverflow.com/questions/112055/what-does-d0-mean-in-a-windows-batch-file
0
 
LVL 11

Expert Comment

by:Patmac951
ID: 34984328
This link is probably a little easier to understand, straight from Microsoft.

http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/percent.mspx?mfr=true
0
 
LVL 34

Assisted Solution

by:it_saige
it_saige earned 400 total points
ID: 34984458
%~ 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.

http://www.dostips.com/DtTipsStringManipulation.php

HTH,

-saige-
0
How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

 
LVL 17

Author Comment

by:Barry Cunney
ID: 34984557
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?
0
 
LVL 17

Author Comment

by:Barry Cunney
ID: 34984570
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
0
 
LVL 71

Accepted Solution

by:
Qlemo earned 600 total points
ID: 34984712
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.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
This is a fine trick which I've found useful many times, when you just don't want to accidentally run a batch script or the commands needs administrator rights.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question