Leading Spaces Batch File

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
CMOS
Posts: 8
Joined: 23 May 2013 14:21

Leading Spaces Batch File

#1 Post by CMOS » 23 May 2013 14:35

Hello,

I have a batch file that reads a list of names from one file and appends to them to another file. So say File1 has 4 lines in it :

Apples
Pears
Oranges
Grapes

I have a batch file that writes :

You like the following fruit

and then should list those four fruit on the same line :

You like the following fruit Apples,Pears,Oranges,Grapes

My problem is I can get everything on the same line but it constantly has a leading space. So the output ends up :

You like the following fruit Apples , Pears , Oranges , Grapes

I can't figure out where the leading spaces and trailing spaces are coming from.

I have the following :

@ECHO OFF
Cls
echo|set /p=You have the following fruit>> Output.txt
for /F %%a in (fruit.txt) do (
echo|set /p=%%a>> Output.txt
echo|set /p=,>> Output.txt
)

Any kind soul point me in the right direction?

Thank you.

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: Leading Spaces Batch File

#2 Post by Endoro » 23 May 2013 15:11

try this:

Code: Select all

@ECHO OFF
Cls
<nul set /p"=You have the following fruit ">>"Output.txt"
for /F %%a in (fruit.txt) do (
<nul set /p"=%%a">>"Output.txt"
<nul set /p"=,">>"Output.txt"
)


Aacini
Expert
Posts: 1888
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Leading Spaces Batch File

#3 Post by Aacini » 23 May 2013 16:11

The method you use show a comma at the end of the line, and it is somewhat complicated to avoid it. I suggest you to use this method instead:

Code: Select all

@ECHO OFF
setlocal EnableDelayedExpansion
Cls
set "line=You have the following fruit "
for /F %%a in (fruit.txt) do (
   set "line=!line!%%a,"
)
echo %line:~0,-1%>> Output.txt

Antonio

probyn
Posts: 7
Joined: 23 May 2013 20:01

Re: Leading Spaces Batch File

#4 Post by probyn » 23 May 2013 20:10

All that 'set /p' stuff is too complicated. Why not keep it simple?

@echo off
setlocal enabledelayedexpansion
for /f "tokens=*" %%a in (fruit.txt) do set list=!list!,%%a
echo/You have or like or whatever the following fruit[: ]%list:~1%>>output.txt


Phil Robyn
p r o b y n a t b e r k e l e y d o t e d u

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Leading Spaces Batch File

#5 Post by dbenham » 23 May 2013 20:24

The mysteriously appearing spaces are an artifact of how the line is parsed. Both sides of the pipe are processed in a new CMD.EXE thread, and the command(s) are parsed into a /C option for the CMD command. The special parser inserts a space before the >.

You could prevent the space by using echo|set /p "=text">>Output.txt. The space is still added to the command line, but it will not be printed because it is after the last quote.

Better yet, you can redirect input to NUL instead of using a pipe: <nul set /p=text>>Output.txt. No space is added in this case.

Yet another way to solve the problem is to enclose your whole output generating code in another level of parentheses and perform the redirection just once at the outer level. This is also a more efficient way to do it, since it takes some time to process redirection on every line.

Code: Select all

>Output.txt (
  ...
  echo|set/p=text
  ...
)

However, I would combine all three techniques in my code. Also, it is simpler to print the comma with %%a; there is no need for two commands. I would want a colon after the first statement, and a single space before each fruit. It is not so difficult to prevent an extra comma at the beginning or end.

Code: Select all

@echo off
setlocal
cls
set "first=true"
<nul >Output.txt (
  set /p "=You have the following fruit: "
  for /F %%a in (fruit.txt) do (
    if defined first (
      set /p "=%%a"
      set "first="
    ) else set /p "=, %%a"
  )
)
type output.txt
--OUTPUT--

Code: Select all

You have the following fruit: Apples, Pears, Oranges, Grapes

Or better yet, I would use Antonio's technique of building up a variable using delayed expansion, and printing it at the end, as it is simplerr. But I thought it would be instructive to demonstrate how to get your initial design to work :)


Dave Benham

Ocalabob
Posts: 79
Joined: 24 Dec 2010 12:16
Location: Micanopy Florida

Re: Leading Spaces Batch File

#6 Post by Ocalabob » 23 May 2013 21:12

OT. @ Phil Robyn
Nice work on your first post. Welcome to the forum.

OT. @Dave Benham
That was instructive for me. Thank you for the post!

Later.

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: Leading Spaces Batch File

#7 Post by Endoro » 23 May 2013 23:00

I tested this with XP:

Code: Select all

C:\TEST>type test.cmd
@echo off
echo|set /p=>con
echo(some text
C:\TEST>test
 some text

C:\TEST>


There is a leading space before "some text". Does this work with Vista and later too?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Leading Spaces Batch File

#8 Post by foxidrive » 24 May 2013 00:23

Windows 8 has no space.

Code: Select all

some text
Press any key to continue . . .

CMOS
Posts: 8
Joined: 23 May 2013 14:21

Re: Leading Spaces Batch File

#9 Post by CMOS » 29 May 2013 10:27

Endoro wrote:try this:

Code: Select all

@ECHO OFF
Cls
<nul set /p"=You have the following fruit ">>"Output.txt"
for /F %%a in (fruit.txt) do (
<nul set /p"=%%a">>"Output.txt"
<nul set /p"=,">>"Output.txt"
)



I just wanted to follow up on this and say thank you to everyone who responded. I ended up using the code above for my solution.

For my own knowledge, in the code that dbenham posted, does the code set a variable to TRUE, see it's defined and run the first block of code then the variable First becomes undefined so it will add the comma?

Is that correct?

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Leading Spaces Batch File

#10 Post by dbenham » 29 May 2013 22:42

CMOS wrote:
Endoro wrote:try this:
For my own knowledge, in the code that dbenham posted, does the code set a variable to TRUE, see it's defined and run the first block of code then the variable First becomes undefined so it will add the comma?

Is that correct?
Yes, that is correct


Dave Benham

Post Reply