Using SETX command to get tokens from a file
- MigrationUser
- Posts: 336
- Joined: 2021-Jul-12, 2:37 pm
- Contact:
Using SETX command to get tokens from a file
15 Nov 2013 05:28
Aacini
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:
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:
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 (for reasons I can not
fathom out).
This is the additional "setx /F help" text that I wrote in order to complete the original SETX help:
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:
SetXexample.bat:
Antonio
----------------------------
#2 15 Nov 2013 12:48
npocmaka
That's cool .I like command abusing:
----------------------------
#3 01 Aug 2014 20:05
Simon Sheppard
It's taken a while, but I have finally gotten around to rewriting the SetX page and have included some of Aacini suggestions above.
https://windevcluster.com/nt/setx.html
The one thing I'm not too keen on with the technique above is that it creates and leaves behind that dummy variable in the registry. I guess as long as you don't call the variable %systemroot% and it doesn't contain any sensitive information it's a small risk, but one to be aware of
Aacini
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%
Code: Select all
setx /F filename.txt dummyVar /A 0,0
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.
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
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
----------------------------
#2 15 Nov 2013 12:48
npocmaka
That's cool .I like command abusing:
it works if the , (comma ) is escaped...if you remove the quotes, an error is issued (for reasons I can not fathom out).
----------------------------
#3 01 Aug 2014 20:05
Simon Sheppard
It's taken a while, but I have finally gotten around to rewriting the SetX page and have included some of Aacini suggestions above.
https://windevcluster.com/nt/setx.html
The one thing I'm not too keen on with the technique above is that it creates and leaves behind that dummy variable in the registry. I guess as long as you don't call the variable %systemroot% and it doesn't contain any sensitive information it's a small risk, but one to be aware of
