Computing.Net > Forums > Programming > Leading space whn writing var to tx

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Leading space whn writing var to tx

Reply to Message Icon

Name: keridbey
Date: January 10, 2008 at 06:25:25 Pacific
OS: WinXP
CPU/Ram: 1.83 Ghz / 1 Gb
Product: Dell
Comment:

I suspect this is a particularly elementary question I'm about to ask; please forgive me. I am merely trying to echo variables to text files, but when the variable is a single-digit number, it seems to mess up how it writes to the file, as in the example below:

set FIRST=5
set SECOND=6

echo %FIRST%>"C:\first.txt"
echo %SECOND%>"C:\second.txt"

The resulting text files end up blank! It looks like it's adding a leading space to the variable when it tries to write to the file, which then just seems to write a blank file. The text files must only contain the numbers (no quotes or extra characters), and the numbers won't always be single-digits. Any thoughts on how/why this is happening? Thanks!!



Sponsored Link
Ads by Google

Response Number 1
Name: Razor2.3
Date: January 10, 2008 at 06:49:15 Pacific
Reply:

Don't know why it's happening, but I do have a workaround:

echo.%FIRST%>"C:\first.txt"
echo.%SECOND%>"C:\second.txt"


0

Response Number 2
Name: User123456789
Date: January 10, 2008 at 07:05:06 Pacific
Reply:

I guess that is because this does not work either :


echo 2> test.txt


" 2>" is a system-interpreted string ...

Undo that by means of this:

echo.2> test.txt


2= error output, ex. using : 2>
1=normal output, but the number 1 is not actually written, ex. 1>

3, 4, ... are other reserved types ?!

Never had that problem ; never thought about it, but indeed, this will not work:

echo 2> test.txt

Hi there.


0

Response Number 3
Name: klint
Date: January 10, 2008 at 07:05:41 Pacific
Reply:

[message deleted because by the time I pressed the Post button, someone else had already posted what I was going to say]


0

Response Number 4
Name: keridbey
Date: January 10, 2008 at 07:05:52 Pacific
Reply:

Razor2.3 - Huzzah! Thanks a bunch; it works great!!!

User123456789 & klint - Yah, I hadn't ever run into this problem before, but that makes perfect sense. I'll remember to try to steer clear of this in the future. Thanks!


0

Response Number 5
Name: User123456789
Date: January 10, 2008 at 07:08:25 Pacific
Reply:

Hehe, this is funny


C:\>del test.txt

C:\>echo a 1> test.txt

C:\>type test.txt
a

C:\>

Hi there.


0

Related Posts

See More



Response Number 6
Name: IVO
Date: January 10, 2008 at 07:20:13 Pacific
Reply:

The problem is due to how NT cmd.exe processes the std out streams, i.e. 1> is stdout, 2> is stderr and more.

So if you want to echo a single digit you have to prefix it with the caret (^).

Referring to the original post the following solve the issue

If %first% lss 10 (
echo.^%first%> C:\first.txt
) else (
echo.%first%> C:\first.txt)


0

Response Number 7
Name: klint
Date: January 10, 2008 at 07:20:52 Pacific
Reply:

If you thought that was funny, how about

C:\>type x 2> y

C:\>type y
The system cannot find the file specified.

C:\>dir y
Directory of C:\

10/01/2008 15:17 44 y
1 File(s) 44 bytes
0 Dir(s) 5,974,595,906,560 bytes free

C:\>type y
The system cannot find the file specified.


0

Response Number 8
Name: User123456789
Date: January 10, 2008 at 07:25:02 Pacific
Reply:

LOL

That is amazing, and you fooled me for a couple of seconds there !!!

Hi there.


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Leading space whn writing var to tx

how to write character to a file? www.computing.net/answers/programming/how-to-write-character-to-a-file/4564.html

batch: copy one var to another www.computing.net/answers/programming/batch-copy-one-var-to-another/9107.html

writing data to file in java www.computing.net/answers/programming/writing-data-to-file-in-java/14129.html