ECHOing to a file without a trailing space

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
miskox
Posts: 630
Joined: 28 Jun 2010 03:46

ECHOing to a file without a trailing space

#1 Post by miskox » 10 Nov 2023 11:27

Hello all!

Code: Select all

@echo off
set field1=a
set field2=(a
set field3=2
set field4=34564119

echo %field1%;%field2%;%field3%>file1.tmp
echo %field1%;%field2%;%field3%>file2.tmp
echo %field1%;%field2%;%field3% >file3.tmp
echo (%field1%;%field2%;%field3%;%field4%)>file4.tmp
Results:

file1.tmp and file2.tmp are empty because "2" redirects it.

file3.tmp (size 9 bytes - trailing space after number 2)

Code: Select all

a;(a;2 
file4.tmp (size 19 bytes)

Code: Select all

(a;(a;2;34564119)
You see that

Code: Select all

>>file.tmp (echo...)
is no good because my data can contain round brackets ().

I don't need a space at the end of the line.

I need:

Code: Select all

a;(a;2
(numbers 2 and are just to show different behavior. There is only one number at the end of the file)

Please help.
Thanks.
Saso

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: ECHOing to a file without a trailing space

#2 Post by jeb » 10 Nov 2023 12:36

Code: Select all

(echo(%field1%;%field2%;%field3%;%field4%)>file4.tmp

OJBakker
Expert
Posts: 90
Joined: 12 Aug 2011 13:57

Re: ECHOing to a file without a trailing space

#3 Post by OJBakker » 10 Nov 2023 15:31

Place the redirect before the echo to avoid the trailing space in the echo command.

Code: Select all

>file0.tmp echo %field1%;%field2%;%field3%

Batcher
Posts: 74
Joined: 16 Apr 2009 10:36

Re: ECHOing to a file without a trailing space

#4 Post by Batcher » 11 Nov 2023 07:04

Code: Select all

>file4.txt echo %field1%;%field2%;%field3%
echo>file5.txt %field1%;%field2%;%field3%

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: ECHOing to a file without a trailing space

#5 Post by miskox » 11 Nov 2023 09:35

@jeb: your solution does not work. I have ( and ) in my data so I get

Code: Select all

34567891) was unexpected at this time.
@batcher: your solution:

Code: Select all

echo>file5.txt %field1%;%field2%;%field3%
works.

Thanks.
Saso

mataha
Posts: 35
Joined: 27 Apr 2023 12:34

Re: ECHOing to a file without a trailing space

#6 Post by mataha » 12 Nov 2023 17:15

Should be resilient enough:

Code: Select all

@setlocal DisableDelayedExpansion
@set field1=a
@set field2=(a
@set field3=2
@set field4=34564119
@setlocal EnableDelayedExpansion

@for /f delims^= %%s in ("(!field1!;!field2!;!field3!;!field4!)") do @(echo(%%s) >"file4.tmp"

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: ECHOing to a file without a trailing space

#7 Post by jeb » 13 Nov 2023 04:59

With DelayedExpansion it's safe with all character, but then you don't need the FOR /F

Code: Select all

@echo off
setlocal DisableDelayedExpansion
set field1=a
set field2=(a
set field3=2
set field4=34564119
setlocal EnableDelayedExpansion

(echo(!field1!;!field2!;!field3!;!field4!) >"file4.tmp"

Post Reply