#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,029
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

#5 22 Jul 2019 04:33

Try3
New Member
Registered: 20 Jul 2019
Posts: 1

Re: %varname:~3,4%

I have lost my link to the guide that enabled me to write the following list so I apologise to the extremely knowledgeable and helpful individual who did the hard work.  All I have done is put the whole thing together to act as both an explanation & as a runnable batch file demonstrator.

REM  I have mislaid the source of the excellent guide to manipulating variables that allowed me to compile this ready-reference list
REM  I have deliberately laid it out so it can be saved both for reading as a text file [.txt] and for running as a batch file [.bat]
REM  When saved as ManipulatingVariables-UsefulExamples.txt, I have added a shortcut to it in my menus because I end up referring to it for almost every batch file I write



REM The MyVar variable is used as the input in all the examples that follow
REM and the output variable for each example is NewVar
REM except for the 'Test for' examples - these just refer to MyVar 



REM ::::: The variable used as the example :::::
Set MyVar=abcdefghijklm
Echo %MyVar%
REM MyVar echo is abcdefghijklm
Pause



REM ::::: Extracting characters :::::

REM Extract the first character
REM Note - The first character is at location 0 [not location 1]
Set NewVar=%MyVar:~0,1%
Echo %NewVar%
REM NewVar echo is a
Pause

REM Extract the second character
Set NewVar=%MyVar:~1,1%
Echo %NewVar%
REM NewVar echo is b
Pause

REM Extract the second and third characters
Set NewVar=%MyVar:~1,2%
Echo %NewVar%
REM NewVar echo is bc
Pause

REM Extract the last character
Set NewVar=%MyVar:~-1%
Echo %NewVar%
REM NewVar echo is m
Pause

REM Extract the last two characters
Set NewVar=%MyVar:~-2%
Echo %NewVar%
REM NewVar echo is lm 
Pause

REM Extract the second though to second-to-last characters
Set NewVar=%MyVar:~1,-1%
Echo %NewVar%
REM NewVar echo is bcdefghijkl
Pause

REM Extract the second though to third-to-last characters
Set NewVar=%MyVar:~1,-2%
Echo %NewVar%
REM NewVar echo is bcdefghijk
Pause



REM ::::: Removing characters :::::

REM Remove the first character
Set NewVar=%MyVar:~1%
Echo %NewVar%
REM NewVar echo is bcdefghijklm
Pause

REM Remove the first and second characters
Set NewVar=%MyVar:~2%
Echo %NewVar%
REM NewVar echo is cdefghijklm
Pause

REM Remove the last character
Set NewVar=%MyVar:~0,-1%
Echo %NewVar%
REM NewVar echo is abcdefghijkl
Pause

REM Remove the last two characters
Set NewVar=%MyVar:~0,-2%
Echo %NewVar%
REM NewVar echo is abcdefghijk
Pause

REM Remove all occurences of a particular character
REM For this example, I have chosen to remove c
Set NewVar=%MyVar:c=%
Echo %NewVar%
REM NewVar echo is abdefghijklm
Pause

REM Remove all occurences of a particular character sequence
REM For this example, I have chosen to remove bc
Set NewVar=%MyVar:bc=%
Echo %NewVar%
REM NewVar echo is adefghijklm
Pause

REM Remove all characters up to and including the first occurence of a particular character
REM This can only be used from the start of a string not in mid position or from the end 
REM For this example, I have chosen to remove everything up to and including c
Set NewVar=%MyVar:*c=%
Echo %NewVar%
REM NewVar echo is defghijklm
Pause

REM Remove all characters up to and including the first occurence of a particular character sequence
REM This can only be used from the start of a string not in mid position or from the end 
REM For this example, I have chosen to remove everything up to and including cd
Set NewVar=%MyVar:*cd=%
Echo %NewVar%
REM NewVar echo is efghijklm
Pause



REM ::::: Testing for particular characters :::::

REM Test for the presence of a particular character in the string
REM Note - Only valid if the variable does not contain quotation marks so any that existed would have to be removed first [see above]
REM Notice that this example compares an extract from MyVar with the whole of MyVar [without creating a NewVar]
REM For this example, I have chosen to test for b
If Not "%MyVar:b=%"=="%MyVar%" Echo %MyVar% contains b
REM MyVar echo is abcdefghijklm contains b
Pause

REM Test for the presence of a particular character sequence in the string
REM only valid if the variable does not contain quotation marks so any that existed would have to be removed first [see above]
REM The characters are chosen using the Extract character[s] example syntax above
REM Notice that this example compares an extract from MyVar with the whole of MyVar [without creating a NewVar]
REM For this example, I have chosen to test for bcd
If Not "%MyVar:bcd=%"=="%MyVar%" Echo %MyVar% contains bcd
REM MyVar echo is abcdefghijklm contains bcd
Pause

REM Test for a particular character sequence at the start or another specific location within the variable
REM The characters are chosen using the Extract character[s] example syntax above
REM This example tests the two characters at the start of the variable
REM Notice that this example compares an extract from MyVar with the whole of MyVar [without creating a NewVar]
REM For this example, I have chosen to test for ab [at the start of the variable]
If "%MyVar:~0,2%"=="ab" Echo %MyVar% starts with ab
REM MyVar echo is abcdefghijklm starts with ab
Pause



REM ::::: Replacing characters :::::

REM Replace all occurences of a particular character - first of two examples of this
REM For this example, I have chosen to replace c with X
Set NewVar=%MyVar:c=X% 
Echo %NewVar%
REM NewVar echo is abXdefghijklm 
Pause

REM Replace all occurences of a particular character - second of two examples of this
REM For this example, I have chosen to replace c with XYZ
Set NewVar=%MyVar:c=XYZ% 
Echo %NewVar%
REM NewVar echo is abXYZdefghijklm 
Pause

REM Replace all occurences of a particular character sequence - first of two examples of this
REM For this example, I have chosen to replace bc with XY
Set NewVar=%MyVar:bc=XY%
Echo %NewVar%
REM NewVar echo is aXYdefghijklm
Pause

REM Replace all occurences of a particular character sequence - second of two examples of this
REM For this example, I have chosen to replace bc with X
Set NewVar=%MyVar:bc=X%
Echo %NewVar%
REM NewVar echo is aXdefghijklm
Pause

This has helped me a lot and I hope it helps you too,
Denis

Offline

Board footer

Powered by FluxBB