9

I would like create a file from a batch file. I can use the echo. > donald.txt, but it creates it with an initial blank line. What else I can use to a file starting from the first line?

4
  • Still a bit confused on what the problem is? Starting from the first line...can you clarify that? Commented Nov 24, 2010 at 14:42
  • To aid future searches, empty file is synonymous with blank file. Commented Dec 21, 2013 at 23:48
  • I don't think so. An empty file contains 0 bytes, but a blank file contains a certain number of spaces or lines with spaces. I think the OP should rename the question title and change "blank" by "empty". Commented Jan 17, 2014 at 0:29
  • Related: superuser.com/questions/10426/… Commented Jul 18, 2018 at 21:06

5 Answers 5

28

You just want to create a completely empty file? This will do it:

copy /y nul donald.txt

If you're looking to write something to first line, you want something like this:

echo Hello, World > donald.txt
Sign up to request clarification or add additional context in comments.

Comments

7

One of the options is

@echo off
rem Don't destroy an existing file
if exist testfile goto _nocreate
:: Create the zero-byte file
type nul>testfile
:_nocreate

Originally from http://www.netikka.net/tsneti/info/tscmd041.htm which contains some additional methods.

Comments

1

In old MS-DOS days I used this method to create an empty file:

rem > empty.txt

In new Windows this no longer works, but rem may be replaced by any command that show nothing. For example:

cd . > empty.txt

Comments

0

If you're using PowerShell:

function touch {set-content -Path ($args[0]) -Value ($null) } 
touch file-name

Source: http://blog.lab49.com/archives/249.

1 Comment

Urgh, that's an evil way of defining a touch command. Remember that touch is not only used for creating empty files, but also for updating the timestamp of an existing one. Better not use a name that has multiple uses already ;-). In keeping with PowerShell terminology, this should probably be called New-Item. But wait ... that already exists. New-Item foo.txt -Type File (or ni -t f foo.txt if you like it shorter). So, this is probably a non-solution to something that can be done easier with the tools already there.
0

That's because echo. inserts a blank line in your file.

Try using the same code, but without the period appended to the end of the echo statement:

echo "Test string" > donald.txt

3 Comments

If they want an empty file, then they certainly don't want the file to read "Test string" (even including the quotation marks).
@Joey: I interpreted "What else I can use to a file starting from the first line?" as meaning they wanted to write text into the file, but they wanted it to appear on the first line of the file, rather than the second with a blank line at the top.
Ah, ok. The question was indeed a little murky.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.