Using SETX command to get tokens from a file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Using SETX command to get tokens from a file

#1 Post by Aacini » 14 Nov 2013 20:40

When I reviewed the SETX command help I discovered /A, /R, /D and /X switches unknown to me. After some tests I discovered that these switches may be used to extract individual tokens from a file in interesting ways, similar to a FINDSTR command placed into a FOR. Yes, I know that SETX is used to define persistent variables, but in this case we may use a dummy variable that can be deleted later or just ignored. Although SETX documentation specify that "created variables will be available in future DOS Windows sessions, not in current CMD.exe session", this limitation may be easily solved via a FOR /F command, because in this case the assigned value is displayed in the screen. This is the general method to get a token from SETX /F:

Code: Select all

for /F "tokens=3" %%a in ('"setx /F filename.txt dummyVar /A 0,4"') do set "var=%%a" & goto continue
:continue
echo The fifth token of first line in filename.txt is: %var:~0,-1%

Previous example works on my Windows 8 Spanish version computer, so perhaps you need to adjust the "tokens=3" and "%var:~0,-1%" parts in order to get the right result in your computer. In this case, execute this command with any file that have any data in the first line:

Code: Select all

setx /F filename.txt dummyVar /A 0,0

And then place the number of the token where the first word appear instead of the "3". In Spanish, the line shown is equivalent to this one: "Extracted value: theWord.". Note that when SETX command is placed inside the FOR it must be enclosed in quotes (inside the apostrophes); if you remove the quotes, an error is issued because the comma is deleted when the for is parsed. You may also escape the comma this way:

Code: Select all

for /F "tokens=3" %%a in ('setx /F filename.txt dummyVar /A 0^,4') do set "var=%%a" & goto continue


This is the additional "setx /F help" text that I wrote in order to complete the original SETX help:

Code: Select all

Syntax 3:
    SETX /F filename { var {/A l,t | /R ol,ot sToken} [/D delimiters] } | { /X }

    /F  filename          Specify the file to search.

    var                   A dummy variable name.

    /A  l,t               Get token T from line L (coordinates) in the file.
                          l,t coordinates are numbered starting from 0.

    /R  ol,ot  sToken     Search source token in the file, case insensitive;
                          then add ol,ot (with optional signs) to the found
                          coordinates and get the target token. If source or
                          target token not exists, set errorlevel = 1.

    /D  delimiters        Specify delimiters *besides* (not "instead of") the
                          standard delimiters: space, tab, CR and LF.
                          Maximum 11 characters, case sensitive.

    /X                    Show all tokens in the file with l,t coordinates.

I suggest you to run the /X option with a small file: "setx /F filename.txt /X" in order to review the "coordinates" that SETX /F command use, and then execute the first FOR example above directly from the command-line with different coordinates.

The /R switch perform a powerful search and lookup operation that may be used in several ways; it search the source token and then return another token placed OL lines above or below, and/or OT columns to right or left, from position of token found. The example below use this capability to achieve a translation of the names of numbers (from 1 to 10) between English, Spanish, French and German languages:

SetXexample.txt:

Code: Select all

English one two  three four   five  six   seven  eight nine  ten 
Spanish uno dos  tres  cuatro cinco seis  siete  ocho  nueve diez
French  un  deux trois quatre cinq  six   sept   huit  neuf  dix
German  ein zwei drei  vier   fünf  sechs sieben acht  neun  zehn


SetXexample.bat:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

rem SetXexample.bat: Example of SETX /F command to perform a table lookup in a file
rem Antonio Perez Ayala

rem Get the available languages from data file
set n=0
for /F %%a in (SetXexample.txt) do (
   set /A n+=1
   set language[!n!]=%%a
)

:nextLanguages
cls
echo Available languages:
echo/
for /L %%i in (1,1,%n%) do echo    %%i- !language[%%i]!
echo/
set "source="
set /P "source=Enter source language number: "
if not defined source cls & exit /B
set /P "target=Enter target language number: "
set /A lineOffset=target-source
echo/

:nextWord
echo/
set "sourceWord="
set /P "sourceWord=Enter !language[%source%]! word: "
if not defined sourceWord goto nextLanguages
set "targetWord="
for /F "tokens=3" %%a in ('"setx /F SetXexample.txt dummyVar /R %lineOffset%,0 %sourceWord%" 2^>NUL') do (
   set "targetWord=%%a"
   goto continue
)
:continue
if defined targetWord (
   echo !language[%target%]! translation is: %targetWord:~0,-1%
) else (
   echo I don't know that word...
)
goto nextWord


Antonio
Last edited by Aacini on 26 Feb 2014 12:15, edited 1 time in total.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Using SETX command to get tokens from a file

#2 Post by foxidrive » 15 Nov 2013 03:16

Thanks for posting Antonio. This is interesting.

I tried to break it and found that with /R the first instance of the word in sToken is always used. For instance in setXexample.txt there two mentions of 'six' and using

setx /F setxexample.txt v /r 3,-1 six

then it returns fünf


The other thing I tried was setting the delimiter to " and it ignored it.

/D delimiters

Post Reply