Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Thursday, December 24, 2015

How to create an empty file at the command line in Windows?

How to create an empty file at the command line in Windows?


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.

Answer by Kris for How to create an empty file at the command line in Windows?


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.

Answer by DRapp for How to create an empty file at the command line in Windows?


copy con SomeFile.txt Enter

Ctrl-Z Enter

Answer by VonC for How to create an empty file at the command line in Windows?


Without redirection, Luc Vu or Erik Konstantopoulos point out to:

copy NUL EMptyFile.txt  copy /b NUL EmptyFile.txt  

"How to create empty text file from a batch file?" (2008) also points to:

type NUL > EmptyFile.txt  # also  echo. 2>EmptyFile.txt  copy nul file.txt > nul # also in qid's answer below  REM. > empty.file  fsutil file createnew file.cmd 0 # to create a file on a mapped drive  

Nomad mentions an original one:

C:\Users\VonC\prog\tests>aaaa > empty_file  'aaaa' is not recognized as an internal or external command, operable program or batch file.    C:\Users\VonC\prog\tests>dir     Folder C:\Users\VonC\prog\tests    27/11/2013  10:40              .  27/11/2013  10:40              ..  27/11/2013  10:40                 0 empty_file  

(Original answer, November 2009)

echo.>filename  

(echo "" would actually put "" in the file! And echo without the '.' would put "Command ECHO activated" in the file...)

Note: the resulting file is not empty but includes a return line sequence: 2 bytes.


This discussion points to a true batch solution for a real empty file:

 filename     dir filename   11/09/2009  19:45                 0 filename   1 file(s)                         0 bytes  

The "" pipes a nul response to the set/p command, which will cause the variable used to remain unchanged. As usual with set/p, the string to the right of the equal sign is displayed as a prompt with no CRLF.

Since here the "string to the right of the equal sign" is empty... the result is an empty file.


The difference with cd. > filename (which is mentioned in Patrick Cuff's answer and does also produce a 0-byte-length file) is that this "bit of redirection" (the trick) can be used to echo lines without any CR:

out.txt  >out.txt  dir out.txt  

The dir command should indicate the file size as 12 bytes: "hello world!".

Answer by Paul Nathan for How to create an empty file at the command line in Windows?


You can write your own touch.

//touch.cpp  #include   #include     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;  }  

Answer by nash for How to create an empty file at the command line in Windows?


Reading comments on my post, I have to admit I didn't read the question right.

On the Windows command-line, one way would be to use fsutil:

fsutil file createnew    

An example:

fsutil file createnew myEmptyFile.txt 0  

Below is for *nix command-line.

touch filename  

This command changes your modified date of a file or creates it if file is not found.

Answer by qid for How to create an empty file at the command line in Windows?


If you really want a totally empty file, without any output to stdout, you can cheat a little:

copy nul file.txt > nul  

Just redirect stdout to nul, and the output from copy disappears.

Answer by Patrick Cuff for How to create an empty file at the command line in Windows?


Here's another way:

cd. > filename  

Answer by Craig for How to create an empty file at the command line in Windows?


You could also use:

echo. 2>foo  

The debug output for echo. will almost definitely be empty.

Answer by Justin for How to create an empty file at the command line in Windows?


Try this:

type NUL > 1.txt  

this will definitely create an empty file.

Answer by Dowopfan for How to create an empty file at the command line in Windows?


First create your file so that it exists:

echo . > myfile.txt  

Then overwrite the created file with an empty version using the copy command:

copy /y nul myfile.txt  

Answer by nephi12 for How to create an empty file at the command line in Windows?


call>file.txt  

this is the cleanest way I know.

Answer by Nomad for How to create an empty file at the command line in Windows?


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.

Answer by 131 for How to create an empty file at the command line in Windows?


echo.|set /p=>file  

echo. suppress the "Command ECHO activated"

|set /p= prevent newline (and file is now 0 byte)

Answer by ninix for How to create an empty file at the command line in Windows?


Today I've discovered a new one :)

This will change the command line window title, but will also create a empty file.

title > file.txt  

Answer by foxidrive for How to create an empty file at the command line in Windows?


Yet another method that creates a zero byte file:

break > "file.txt"  

Answer by Lucky for How to create an empty file at the command line in Windows?


This worked for me,

echo > file.extension  

Here's another way I found today, got ideas from other answers but it worked

sometext > filename.extension  

Eg.

xyz > emptyfile.txt  //this would create an empty zero byte text file  abc > filename.mp4   //this would create an zero byte MP4 video media file  

This would show an error message in the command prompt that ,

xyz is not as an internal or external command, operable program or batch file.

But the weird thing I found was the file is being created in the directory even if the command is not a standard windows command.

Answer by iCrazybest for How to create an empty file at the command line in Windows?


Open file :

type file.txt  

New file :

Way 1 : type nul > file.txt  Way 2 : echo This is a sample text file > sample.txt  Way 3 : start notepad myfile.txt   

Edit content:

edit file.txt  

Copy

copy file1.txt file1Copy.txt  

Rename

rename file1.txt file1_rename.txt  

Delete file :

del file.txt  

Answer by Erik Konstantopoulos for How to create an empty file at the command line in Windows?


type nul>filename will create a new empty file.

Sorry I'm late.

UPDATE: Also copy nul filename works without redirecting (more obvious solution).

Answer by Erik Konstantopoulos for How to create an empty file at the command line in Windows?


Use:
copy nul filename
This will copy the file nul (empty file that exists in each directory) to the file filename.

Answer by Mohamed Hamed for How to create an empty file at the command line in Windows?


you can use

notepad test.js  

this command will create the file and open it for you in notepad

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.