1.

Solve : How to extract nth word from a list?

Answer»

I would like to extract the nthe word from a list.
As an example, if I have a list "one two three four" and would like to extract word 3, how would I do this in a batch?Hi 
Please be more explicit !
What's the delimiters of your list and where this is located on text file or from another command ??
The word is in a phrase with one line or with newline ?

The word is in a phrase with one line
Code: [Select]one two three four
Or with newline
Code: [Select]one
two
three
four

Can you provide us a real example and what code did you tried until now ?

Here is an example :
Code: [Select]echo off
Title Extarct word from list
set "Line=one two three four five"
set "Num=3"
SetLocal EnableDelayedExpansion
for /f "tokens=%Num%" %%i in ("%Line%") do set "word_%Num%=%%i"
echo The extracted word is = !word_%Num%!
pauseI think I over simplified my question.
Here is the script I have been working on with your addition now:

SETLOCAL EnableDelayedExpansion
set attribs=--a------
echo Attributes: %attribs%
FOR /L %%G IN (0,1,8) DO (
   set val=!attribs:~%%G,1!
   set num=%%G
   if not !val!==- (
   echo Attribute: Got here with !num!
   set line="DIRECTORY Read_Only Archived Hidden System_File Compressed_File Offline_File Temporary_File Reparse_point"
   for /f "tokens=%Num%" %%i in ("%Line%") do set "word_%Num%=%%i"
   echo The extracted word is = !word_%Num%!
        )
)

The "Got here with !num!" is correct as "Got here with 2".
Then the !word_%Num%! is a blank.
This of course is inside a loop and needs expansion but I am lost here since I had to use !num! first but if I use !num! with the token I receive an error.
How do I work around this?Just give a try for this modification
NB : I choose ; as delimters

Code: [Select]echo off
Title Extarct word from list
SetLocal EnableDelayedExpansion
set "attribs=--a------"
set "line=Directory;Read_Only;Archived;Hidden;System_File;Compressed_File;Offline_File;Temporary_File;Reparse_point"
echo Attributes: %attribs%

for /L %%G IN (0,1,8) DO (
set val=!attribs:~%%G,1!
if not "!val!"=="-" (
set "num=%%G"
)
)

echo Got here with %num%

for /f "tokens=%num% delims=;" %%i in ("%Line%") do (
set "word=%%i"
echo The extracted word is = !word!
)
Pause &  EXITThank you Hackoo.

Your code works but NEEDED to add:
set /A num+1
after set "num=%%G" to extract the correct word in the second for loop.

There is a problem though. I originally placed the 2nd loop inside the first to be able to display multiple words for files that exhibit multiple attributes (eg. Read only and System file [-r--s----]).
This code only displays the last attribute.I think I have it worked out now.
Rather than use a second loop after the first, call the second loop as a subroutine from inside the first loop as follows:

echo off
Title Extarct word from list
SetLocal EnableDelayedExpansion
set "attribs=-r--s----"
set "line=Directory;Read Only;Archived;Hidden;System File;Compressed File;Offline File;Temporary File;Reparse point"
echo Attributes: %attribs%

for /L %%G IN (0,1,8) DO (
   set val=!attribs:~%%G,1!
   if not "!val!"=="-" (
      set "num=%%G"
      set /A num+=1
      call :reit
   )
)
pause
exit

:reit
for /f "tokens=%num% delims=;" %%i in ("%Line%") do (
   set "word=%%i"
   echo The extracted word is = !word!
)
goto :eof
Pause &  EXIT

Just on the side, how do you insert code into the post as you have done Hackoo?
 Hi Frank 
I Just have one question :
How did you OBTAIN this format, i mean which command exactly can you get this ?
Code: [Select]-r--s---- Quote from: Frank on February 23, 2021, 04:20:53 PM

Just on the side, how do you insert code into the post as you have done Hackoo?
I'm glad that you have solved your issue !
Did you mean how to format code into [ code ][ /code ] ?
Just put your entire code between [ code ][ /code ] (Just trim the extra spaces between brackets) or select all your code and hit the button # for code !
And if you want to thanks someone , you have just a little link Thank member name  Hi Hackoo,
The format for the file attributes (-r--s----) I obtained while searching the internet here:

https://stackoverflow.com/questions/9252980/how-to-split-the-filename-from-a-full-path-in-batch

See the code extract:
Code: [Select]set "filename=C:\Folder1\Folder2\File.ext"
For %%A in ("%filename%") do (
    echo full path: %%~fA
    echo drive: %%~dA
    echo path: %%~pA
    echo file name only: %%~nA
    echo extension only: %%~xA
    echo expanded path with short names: %%~sA
    echo attributes: %%~AA
    echo date and time: %%~tA
    echo size: %%~zA
    echo drive + path: %%~dpA
    echo name.ext: %%~nxA
    echo full path + short name: %%~fsA)
Note the line 'echo attributes: %%~aA
I have now noted the link 'Thank member'.
Thank you for your patience.


Discussion

No Comment Found