Page 1 of 1
Leading Spaces Batch File
Posted: 23 May 2013 14:35
by CMOS
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.
Re: Leading Spaces Batch File
Posted: 23 May 2013 15:11
by Endoro
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"
)
Re: Leading Spaces Batch File
Posted: 23 May 2013 16:11
by Aacini
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
Re: Leading Spaces Batch File
Posted: 23 May 2013 20:10
by probyn
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
Re: Leading Spaces Batch File
Posted: 23 May 2013 20:24
by dbenham
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
Re: Leading Spaces Batch File
Posted: 23 May 2013 21:12
by Ocalabob
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.
Re: Leading Spaces Batch File
Posted: 23 May 2013 23:00
by Endoro
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?
Re: Leading Spaces Batch File
Posted: 24 May 2013 00:23
by foxidrive
Windows 8 has no space.
Code: Select all
some text
Press any key to continue . . .
Re: Leading Spaces Batch File
Posted: 29 May 2013 10:27
by CMOS
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?
Re: Leading Spaces Batch File
Posted: 29 May 2013 22:42
by dbenham
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