开发者

Creating a blank file from a batch file

开发者 https://www.devze.com 2023-01-27 06:33 出处:网络
I would like create a file from a batch file. I can use the echo. > donald.txt, but it creates it with an initial b开发者_StackOverflow中文版lank line. What else I can use to a file starting from t

I would like create a file from a batch file. I can use the echo. > donald.txt, but it creates it with an initial b开发者_StackOverflow中文版lank line. What else I can use to a file starting from the first line?


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


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.


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


If you're using PowerShell:

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

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


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
0

精彩评论

暂无评论...
验证码 换一张
取 消