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