How to create an empty file at the command line in

2019-01-12 13:14发布

How to create an empty file at the DOS/Windows command-line?

I tried:

copy nul > file.txt

but it always displays that a file was copied.

Is there any other method in the standard cmd?

It should be a method that does not require the touch command from Cygwin or any other nonstandard commands. The command needs to run from a script so keystrokes cannot be used.

30条回答
劫难
2楼-- · 2019-01-12 13:46

Try this :abc > myFile.txt First, it will create a file with name myFile.txt in present working directory (in command prompt). Then it will run the command abc which is not a valid command. In this way, you have gotten a new empty file with the name myFile.txt.

查看更多
干净又极端
3楼-- · 2019-01-12 13:46

On Windows I tried doing this

echo off > fff1.txt

and it created a file named fff1.txt with file size of 0kb

I didn't find any commands other than this that could create a empty file.

Note: You have to be in the directory you wish to create the file.

查看更多
男人必须洒脱
4楼-- · 2019-01-12 13:49

Yet another way:

copy nul 2>empty_file.txt
查看更多
戒情不戒烟
5楼-- · 2019-01-12 13:49

Just I have tried in windows

copy con file.txt

then Press Enter Key then Press Ctrl+Z Enter

And its worked for me.

For Ubuntu usually I am creating a file using VI command

vi file.txt

It will open the file then press ESC key then type :wp then press enter key. It will create a new file with empty data.

查看更多
戒情不戒烟
6楼-- · 2019-01-12 13:50
echo "" > filename

I believe this works on Windows/DOS, but my last hands-on experience with either is quite a while ago. I do know for a fact that it works on basically any POSIX compliant OS.

查看更多
Rolldiameter
7楼-- · 2019-01-12 13:50

You can write your own touch.

//touch.cpp
#include <fstream>
#include <iostream>

int main(int argc, char ** argv;)
{
  if(argc !=2)
  {
    std::cerr << "Must supply a filename as argument" << endl;
    return 1;
  }
  std::ofstream foo(argv[1]);
  foo.close();
  return 0;
}
查看更多
登录 后发表回答