SETEXTRACT        Extract delimited substring from a string

Top  Previous  Next

Syntax:

SETEXTRACT

[ variable ]  =  [ string ] [ delim ] [ num ]

Arguments:

[ variable ]

Variable to assign; if the variable does not previously exist it is created.

 

[ string ]

Variable or string defining the value from which the substring is to be extracted.

 

[ delim ]

Variable or string defining the delimiter that separates sub-strings within [ string ].

 

[ num ]

Variable, string, or numeric constant defining the occurrence of delimited substring to extract.

Options:

none

 

 

 

This script command extracts the specified occurrence [ num ] of a delimited substring (excluding the delimiters themselves) from [ string ] and saves the result in [ variable ]. Occurrence numbering begins at 1. The [delim] argument may be one or more characters in length. Special characters like tab are allowed in the delimiter.

 

The beginning and end of [ string ] are seen as delimiters.

 

This example rearranges the %date internal variable to form a file name based on yesterday's date:

 

SET yesterday = %date ;; get today's date

DATESUB yesterday 1   ;; subtract 1 day

;; Date format is mm-dd-yy so "-" is delimiter

SETEXTRACT month = yesterday "-" 1

SETEXTRACT day = yesterday "-" 2

SETEXTRACT year = yesterday "-" 3

;; Build name string ex: 20110704.dat for July 4, 2011

SET MyFile = "20" + year + month + day + ".dat"

RCVFILE MyFile         ;; download file

 

Consider the following example which extracts the extension from a filename of unknown length:

 

;; file name could be:

;; file.ext

;; file.file1.ext

;; file.file1.file2.ext

SETSUBSTR depth = filename "."

SETNUM depth = depth + 1

SETEXTRACT extension = filename "." depth

;; extension now contains "ext"

 

This example writes the Script Log file to file named after the current date that is created in a sub-folder named according to the current script:

 

SETSUBSTR slash_count = %currentscript "\"

SETNUM segment = slash_count + 1

SETEXTRACT script_file = %currentscript "\" segment

SET fldr = %installdir + "\ProgramData\Logs\" + script_file

MAKEDIR fldr

SET LogName = fldr + "\" + %date + "script.log"

LOG LogName /append

 

Note: The %currentscript internal variable is empty except when a script is actually running so the previous example will not worked if typed directly into the main console window.

 

 

Related command(s):SET, SETRIGHT, SETLEFT, SETLEN, SETMID, SETSUBSTR

See also: READFILE, Build File Name with Current Date