space is added to the file if it is after the extension

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

space is added to the file if it is after the extension

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

See this:

Code: Select all

@echo off
set str=some text
echo %str%>tmp.tmp &REM space before the & sign!!!
result:

After the 'text' there is a space.

Code: Select all

some text 
Strange.

Saso

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

Re: space is added to the file if is after the extension

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

Hi Saso,

not quite strange, it's the expected behavior.

The rule is: If a redirection to a stream is found, store it in a buffer (one buffer for each stream), remove the redirection text from the command line.

What same mean? Some examples

Code: Select all

@echo off

del /s test.txt 2> NUL > NUL

@echo on

echo Test1>>test.txt&REM
echo Test2>>test.txt &REM
echo Test3>>     test.txt&REM
echo>>test.txt Test4&REM
echo Test5>>void.txt>>test.txt&REM
>* > ::$ 2>errorX.txt echo Test6>>void.txt>>NUL>>test.txt=2>Error.txt&REM

@echo off
for /F "delims=" %%a in ('findstr /N "^" test.txt') do (        
  echo %%a'
)
output wrote:C:\Users\jeb\x>..\redir.bat
C:\Users\jeb\x>echo Test1 1>>test.txt & REM
C:\Users\jeb\x>echo Test2 1>>test.txt & REM
C:\Users\jeb\x>echo Test3 1>>test.txt & REM
C:\Users\jeb\x>echo=Test4 1>>test.txt & REM
C:\Users\jeb\x>echo Test5 1>>test.txt & REM
C:\Users\jeb\x>echo Test6 1>>test.txt 2>Error.txt & REM
C:\Users\jeb\x>echo Test7 1>>test.txt 2>Error.txt & REM
1:Test1'
2:Test2 '
3:Test3'
4:Test4'
5:Test5'
6:Test6'
7:Test7'

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

Re: space is added to the file if it is after the extension

#3 Post by miskox » 11 Nov 2023 09:36

Thanks Jeb!

Saso

Post Reply