Windows IT Pro is the leading independent community for IT professionals deploying Microsoft Windows server and client applications and technologies.
  
  
  Advanced Search 


August 01, 2001

Shell Scripting 101, Lesson 8

RSS
Subscribe to Windows IT Pro | See More Batch Files Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

When you run this For /f command, the command processor opens serverlist.txt and reads in each line, which the /f switch parses. The /f switch parses each line into individual pieces, or tokens. By default, the command processor uses spaces or tabs to determine the tokens—that is, it uses spaces or tabs as the delimiter. So, for example, if you have server names that contain spaces (e.g., fileserv alpha, fileserv beta), the For /f command just given you wouldn’t work because the %i variable would contain only a partial server name (i.e., fileserv).

Fortunately, the For /f command provides several parsing options, which the "options" parameter in the For /f command syntax represents. The tokens and delims options are the most commonly used parsing options. The tokens option lets you specify the tokens you want to capture. The delims option lets you use other delimiters besides the default spaces and tabs. A fun way to learn about these options is to try the following experiments.

Experimenting with the Tokens Option
Open the command-shell window. Run the command

Echo This experiment should help you understand the For command better > C:\testdata.txt

to create a file called testdata.txt on your C drive. Next, run the following commands:

For /f %i in (C:\testdata.txt) Do Echo %i
For /f "tokens=1" %i in (C:\testdata.txt) Do Echo %i
For /f "tokens=2" %i in (C:\testdata.txt) Do Echo %i
For /f "tokens=3" %i in (C:\testdata.txt) Do Echo %i

Figure 2 shows the results. As you can see, you can select any word in the text file by changing the number of the token. If you don’t use the tokens option, the first token is taken by default.

You can select all the tokens in a line by using an asterisk (*). Try the command

For /f "tokens=*" %i in (C:\testdata.txt) Do Echo %i 

If you want to select the first, third, and fifth tokens, you can use the command

For /f "tokens=1,2,3,4,5" %i in (C:\testdata.txt) Do Echo %i %k %m
   Previous  1  2  [3]  4  5  Next 


Top Viewed ArticlesView all articles
Command Prompt Tricks

One reader shares his tip for setting up the command prompt to reflect a remote path. ...

WinInfo Short Takes: Week of November 9, 2009

An often irreverent look at some of the week's other news, including some more Windows 7 sales momentum, some Sophos stupidity, Microsoft's cloud computing self-loathing, more whining from the browser makers, Zoho's "Fake Office," and much, much more ...

Understanding File-Size Limits on NTFS and FAT

A general confusion about files sizes on FAT seems to stem from FAT32's file-size limit of 4GB and partition-size limit of 2TB. ...


Related Articles Programmatically Change Permissions in Print Clusters

Shell Scripting 101, Lesson 10

Shell Scripting 101, Lesson 9

Shell Scripting 101, Lesson 7

Scripting Whitepapers From Development to Production: Streamlining SharePoint Deployment with DocAve Deployment Manager

Related Events Check out our list of Free Email Newsletters!

Scripting eBooks Keeping Your Business Safe from Attack: Encryption and Certificate Services

Best Practices for Managing Linux and UNIX Servers

Building an Effective Reporting System

Related Scripting Resources Introducing Left-Brain.com, the online IT bookstore
Looking for books, CDs, toolkits, eBooks? Prime your mind at Left-Brain.com

Discover Windows IT Pro eLearning Series!
Clear & detailed technical information and helpful how-to's, all in our trademark no-nonsense format


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro DevProConnections IT Job Hound
Left-Brain.com Technology Resource Directory asp.netPRO ITTV Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 © 2009 Penton Media, Inc. Terms of Use | Privacy Statement