RSS

Getting FOR command to ignore spaces as delimiter in your input list

23 Feb

For those who uses the FOR command often enough, you will know that by default the command will take SPACE between characters as a delimiter. This will cause an issue if your input line actually includes SPACES.

For example, I have a list of user names call names.txt which I am using for a query using FOR:

  • Doe, John: HP Singapore
  • Wong, Kelvin: Palm
  • Li, Na: WTA

If I just use the following standard command, the output is not what I want:

FOR /f %a in (names.txt) do @echo %a

  • Doe,
  • Wong,
  • Li,

That’s because FOR command recognises SPACE by default and splits your input lines into a few tokens using SPACE as a delimiter. To trick FOR command to ignore SPACE, you can use a fake delimiter (ensure that the character you use does not appear in the input list) as below:

FOR /f “tokens=1 delims=~” %a in (names.txt) do @echo %a

  • Doe, John: HP Singapore
  • Wong, Kelvin: Palm
  • Li, Na: WTA
Advertisements
 
1 Comment

Posted by on February 23, 2011 in Scripts, Windows CMD

 

Tags:

One response to “Getting FOR command to ignore spaces as delimiter in your input list

  1. dave

    October 13, 2013 at 4:38 pm

    Thanks for that, helped me backup my wav files!

     

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: