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?
-
Still a bit confused on what the problem is? Starting from the first line...can you clarify that?Aaron McIver– Aaron McIver2010-11-24 14:42:58 +00:00Commented Nov 24, 2010 at 14:42
-
To aid future searches, empty file is synonymous with blank file.DavidRR– DavidRR2013-12-21 23:48:36 +00:00Commented 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".Aacini– Aacini2014-01-17 00:29:30 +00:00Commented Jan 17, 2014 at 0:29
-
Related: superuser.com/questions/10426/…Lectrode– Lectrode2018-07-18 21:06:37 +00:00Commented Jul 18, 2018 at 21:06
Add a comment
|
5 Answers
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
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
Joey
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.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
Joey
If they want an empty file, then they certainly don't want the file to read "Test string" (even including the quotation marks).
Cody Gray
@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.
Joey
Ah, ok. The question was indeed a little murky.