Question

Using a batch file (.bat), I'm making a script that requires dynamic paths so that it can work on multiple computers. My problem is when I echo something to a file, it adds a line and an a return carriage.

Say I have a text file named foo.txt in the directory of the batch file, and its contents are completely empty.

In the batch file, I run:

echo test > foo.txt

The contents of foo.txt will be:

L1: foo

L2: 

There would be a space after foo in the first line and a second empty line. Now, this would be completely okay and I would entirely ignore it, but filename paths do not ignore it.

importing text from foo.txt like so:

set /p foo=< foo.txt

...and then:

set /p name=< C:\A.D.V.E.N.T.U.R.E.\test\%foo%\test2.txt

...would be interpreted as:

set /p name=< C:\A.D.V.E.N.T.U.R.E.\test\foo \test2.txt

Including an unwanted space. Is there anyway to make it so you can write text to a file without a space, or a command one could use to delete the carriage return and the space?

Was it helpful?

Solution 2

The data should be test, not foo

Clasically, try

>foo.txt echo test

but make sure that there are no trailing spaces after test.

(to APPEND to foo.txt use >> in place of >)

OTHER TIPS

You can also use parentheses to make sure unwanted space is not included in the output:

(echo test)      >foo.txt

above given answers works. But, the actual reason I found for it was the space before > So, instead of

echo test > foo.txt

it must be

echo test> foo.txt

NOTE. Don't put any space between test and >. This, results in a trailing space.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top