# Solved: Batch Files. Read strings with spaces



## REALOldNick (Feb 19, 2008)

Using XP sp2. I will also need this run on Vista, and possibly Win98. A lot of different people will be using it.

I have a batch file, whose code I grabbed from the Web
I want it to read a folder location, and use that to call a programme at that location.

@echo off
for /f "tokens=1" %%a in (IViewLocation.txt) do set checkdate=%%a
echo %checkdate%
pause 

This works fine, except for any strings with spaces. I have tried double quotes around the string, but "How about if I try this" came out as "How .

Is there a line of code that will work, or what gives? My batch file writing never went this far.and now I am really rusty anyway.

Thanks for any help.


----------



## REALOldNick (Feb 19, 2008)

for /f "tokens=1 delims="" %%a in (IViewLocation.txt) do set checkdate=%%a


----------



## TheOutcaste (Aug 8, 2007)

REALOldNick said:


> for /f "tokens=1 delims=*"*" %%a in (IViewLocation.txt) do set checkdate=%%a


That will work unless you get a string with a double quote in it, as this sets the delimiter to *"*.
You should use one of these formats:
*for /f "tokens=*" %%a in (IViewLocation.txt) do set checkdate=%%a*
This says get all data items (*)
*for /f "tokens=1 delims=" %%a in (IViewLocation.txt) do set checkdate=%%a*
This says get the first data item, but don't use any delimiters, so it also gets everything.
Both still read one line at a time from the file, so CR/LF is always a delimiter
HTH

Jerry


----------



## REALOldNick (Feb 19, 2008)

Crikey! Thanks for that.

It would have worked for my app, because there is no need for double quotes in any of the workable uses for it (paths). But your code will be used anyway, because things may change, and it's better code.

Thanks again.

Nick


----------



## Squashman (Apr 4, 2003)

I have made this very same mistake and Jerry has corrected me on it. If you read the help file for a FOR loop you will see that it uses a space by default as the delimeter. So when you said tokens=1 and it saw the first space and gave you everything before the space. So as Jerry has stated above both of those for loops should work.

From the help..


> filenameset is one or more file names. Each file is opened, read
> and processed before going on to the next file in filenameset.
> Processing consists of reading in the file, breaking it up into
> individual lines of text and then parsing each line into zero or
> ...


----------



## REALOldNick (Feb 19, 2008)

HAH! I need to put "Solved" on all my posts! It gets fantastic replies! 

Thanks for all this info. I have dived back into batch work, as I said, rusty and with a reason to do it, which is the worst way to learn because it's desperate.

Nick


----------



## Squashman (Apr 4, 2003)

You should start learning Power Shell!


----------



## REALOldNick (Feb 19, 2008)

Sorry. Serious question. Is this advice on where I should be looking, or are you saying "Just you wait until...."?

Nick


----------



## Squashman (Apr 4, 2003)

It is something you should look into. The Power Shell is just what it sounds like. You can install it on XP and Vista. It is kind of a new cmd line for Windows.


----------



## REALOldNick (Feb 19, 2008)

Thanks mate!

I have to admit that, ferret that I am, I have already downloaded it.  

I am just in the process of getting the .net framework up to date.

More "interesting times" to come! 

Nick


----------



## ghostdog74 (Dec 7, 2005)

@OP, in my opinion, you shouldn't restrict yourself only to one OS platform. If you are interested, take a look at Python (or Perl).


----------



## REALOldNick (Feb 19, 2008)

I will...when I have the batch file version working! 

Nick


----------

