Leading Spaces Batch File
Moderator: DosItHelp
Leading Spaces Batch File
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.
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
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
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:
Antonio
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
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
@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
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.
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.
--OUTPUT--
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
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
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
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.
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
I tested this with XP:
There is a leading space before "some text". Does this work with Vista and later too?
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
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
Yes, that is correctCMOS 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?
Dave Benham