#1 08 May 2019 21:39

kteague
Member
Registered: 07 May 2019
Posts: 13

%varname:~3,4%

I found this piece of code ... somewhere... and, it works for me after I tinkered and modified it to suit my needs, but I'll be honest and say I don't quite understand the syntax.

for %%S in ("*.pdf") do (set fname=%%S) & call :storelist
goto :print

:storelist
echo %fname:~3,4% >>stores.txt
goto :eof

~3, as it was stated in the forum post, strips out the first 3 letters from the PDF filename.
,4 wasn't documented and I found some other posts about this %var:~x,y% syntax and, after tinkering with it a bit, got it to output the data in the format I needed.  It appears the ,4 keeps the next 4 characters in the filename contained in %fname%, then strips out the remainder.

I'm just not familiar with calling a variable with a colon followed by numbers.  What does the tilde represent?  Also, other forum posts I saw that had other examples had many more numbers after the colon.  If someone wouldn't mind explaining that to me, I'll be very grateful.  Maybe include a couple/few examples.   Many thanks for your time.

Offline

#2 08 May 2019 21:55

kteague
Member
Registered: 07 May 2019
Posts: 13

Re: %varname:~3,4%

Please disregard this request.  I found the answer here which I will also quote in this post just in case the web page ever disappears.



31 January 2008
Cmd.exe Environment Variables with Colon and Tilde
Some commands in Windows cmd.exe batch files have a leading tilde (~) character or a trailing colon-tilde (:~) pair of characters attached to environment variable names. What's the purpose of these characters?

Trailing Colon-Tilde Pair
You can find more about colon-tilde in the help using set /?. Briefly, you can …

Slice a string in a variable: %NAME:~s,n where s is the start position (zero-offset) and n is the number of characters. If s is negative, then the offset starts from the right side minus 1 (i.e. -1 refers to the rightmost character). If n is negative, then length - n characters, are extracted.
Replace a substring with another string in a variable: %NAME:s1=s2% where s1 is substring to be replaced and s2 is the replacement.
Leading Tilde
The leading tilde is used to decompose elements in a batch file parameter formatted as a path, such as the parent directory or file extension. The best reference is Frequently Asked Questions Regarding The Windows 2000 Command Processor, "How do I parse a file name parameter into its' constituent parts?" (sic). Note that you can only use a leading tilde for batch file parameters, not environment variables (!).

4-Mar-08. Another version of the reference is Frequently Asked Questions Regarding The Windows 2000 Command Processor. 09-Sep-02.


And here is an excerpt from the document referenced at the end:

How do I parse a file name parameter into its’ constituent parts?   


Answer: When you invoke a batch file with a parameter (%1), you are able to parse it to extract meaningful information.

Note Command Extensions ( /E:ON ), enabled by default, must be on.

PARAMETER
    DESCRIPTION

%1

The normal parameter.

%~f1

expands %1 to a fully qualified path name.

%~d1

expands %1 to a drive letter only.

%~p1

expands %1 to a path only.

%~n1

expands %1 to a file name only (prefix)

%~x1

expands %1 to a file extension only.

%~s1

changes the meaning of n and x options to reference the short name.

You can use these modifiers in combination:

PARAMETER
    DESCRIPTION

%~dp1

expands %1 to a drive letter and path only.

%~nx1

expands %1 to a file name and extension only.

To determine where a batch file was run from, use %~dp0

I have scripted demo.bat to display the various parsing of a file/folder name parameter. Demo.bat contains:

@echo off
@echo Batch file: %~dp0
If {%1}=={} @echo No parameter specified&goto :EOF
:loop
If {%1}=={} goto :EOF
@echo.
@echo ^%%1=%1
@echo ^%%~f1=%~f1
@echo ^%%~d1=%~d1
@echo ^%%~p1=%~p1
@echo ^%%~n1=%~n1
@echo ^%%~x1=%~x1
@echo ^%%~s1=%~s1
@echo ^%%~dp1=%~dp1
@echo ^%%~nx1=%~nx1
::Shift parameter string
shift
goto :loop


If I type:

demo "C:\Documents and Settings\Jerry\My Documents\My Pictures\Jerold.jpg"
%SystemRoot%\Notepad.exe "%userprofile%"


demo.bat will display:

Batch file: C:\Util\

%1="C:\Documents and Settings\Jerry\My Documents\My Pictures\Jerold.jpg"
%~f1=C:\Documents and Settings\Jerry\My Documents\My Pictures\Jerold.jpg
%~d1=C:
%~p1=\Documents and Settings\Jerry\My Documents\My Pictures\
%~n1=Jerold
%~x1=.jpg
%~s1=C:\DOCUME~1\Jerry\MYDOCU~1\MYPICT~1\Jerold.jpg
%~dp1=C:\Documents and Settings\Jerry\My Documents\My Pictures\
%~nx1=Jerold.jpg

%1=C:\WINNT\Notepad.exe
%~f1=C:\WINNT\notepad.exe
%~d1=C:
%~p1=\WINNT\
%~n1=notepad
%~x1=.exe
%~s1=C:\WINNT\notepad.exe
%~dp1=C:\WINNT\
%~nx1=notepad.exe

%1="C:\Documents and Settings\Jerry"
%~f1=C:\Documents and Settings\Jerry
%~d1=C:
%~p1=\Documents and Settings\
%~n1=Jerry
%~x1=
%~s1=C:\DOCUME~1\Jerry
%~dp1=C:\Documents and Settings\
%~nx1=Jerry

Last edited by kteague (09 May 2019 18:36)

Offline

#3 08 May 2019 23:27

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 1,026
Website

Re: %varname:~3,4%

I've granted you the rights to post links and edit posts now, new accounts don't get that automatically to keep the spammers at bay.

Offline

#4 09 May 2019 17:56

kteague
Member
Registered: 07 May 2019
Posts: 13

Re: %varname:~3,4%

Simon Sheppard wrote:

I've granted you the rights to post links and edit posts now, new accounts don't get that automatically to keep the spammers at bay.

Understandable.  Thank you, kind sir!

Offline

Board footer

Powered by FluxBB