7

The closest I can come up with is:

echo 123 > out.txt

However this gives a trailing space and also a trailing newline.

4
  • 2
    Echo and cat are different beasts - cat reads a file, echo prints what you give it on the commandline. What are you trying to do?
    – Tom Newton
    Oct 11, 2012 at 20:14
  • @TomNewton Basically, I am creating some scripts in TeamCity to be run during a deployment process. I need to basically create a text-file from a script that is then later read in by another script during another process Oct 11, 2012 at 20:19
  • Then leave out the space before after the ">", and strip the trailing newline when you read the file. Oct 11, 2012 at 20:38
  • The original title referred to cat 123 > out.txt; I've just edited it to refer to echo. Oct 13, 2012 at 21:28

5 Answers 5

3

http://gnuwin32.sourceforge.net/packages.html

coreutils contains "echo". You can use the "-n" flag to supress the trailing newline

3

I've used this with success:

echo|set /p="123" > test.txt
4
  • 1
    +1 Neat, although the result still has the trailing space. Tweaked: >text.txt (echo|set /p="123") Oct 11, 2012 at 21:04
  • Ah, my mistake. The output has a trailing space when the double quotes are omitted (set /p=123). It's fine with the double quotes in place (set /p="123"). Oct 11, 2012 at 21:25
  • 1
    It's because you type a space in front of the >. Oct 13, 2012 at 21:34
  • It worked for me as-is.
    – Brian
    Oct 15, 2012 at 13:07
2

In PowerShell:

write-output 123 | out-file test.txt

There is no trailing space, but there is a newline.

If you want to do this in cmd, the answer you provided in your question is the "correct" way, but there are obvious formatting issues

0

I think you want this for text-based output:

type filename > output.txt
2
  • type only works with existing files it seems. In my case, I want to be able to specify the text inline. Oct 11, 2012 at 20:17
  • @CoolUserName: Then your use of "cat" in your question is misleading.
    – Brian
    Oct 11, 2012 at 20:56
0

Use copy con

C:\tmp>copy con mynewfile
This is my new file
I am typing stuff
^Z
        1 file(s) copied.

C:\tmp>type mynewfile
This is my new file
I am typing stuff

Press Control-Z to end input

'con' is a keyword signifying console.

copy src dest

copy con mynewfile

src - con - console

dest - mynewfile - destination

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .