windows cmd command to create a new file Code Example

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

Without redirection, Luc Vu or Erik Konstantopoulospointout 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:UsersVonCprogtests>aaaa > empty_file
'aaaa' is not recognized as an internal or external command, operable program or batch file.

C:UsersVonCprogtests>dir

 Folder C:UsersVonCprogtests

27/11/2021  10:40    <REP>          .
27/11/2021  10:40    <REP>          ..
27/11/2021  10:40                 0 empty_file

In the same spirit, Samuel suggests in the comments:

the shortest one I use is basically the one by Nomad:

.>out.txt

It does give an error:

'.' is not recognized as an internal or external command

But this error is on stderr. And > only redirects stdout, where nothing have been produced.
Hence the creation of an empty file.
The error message can be disregarded here. Or, as in Rain‘s answer, redirected to NUL:

.>out.txt 2>NUL

(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:

 <nul (set/p z=) >filename

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

The “<nul” 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 <nul... trick) can be used to echo lines without any CR:

<nul (set/p z=hello) >out.txt
<nul (set/p z= world!) >>out.txt
dir out.txt

The dir command should indicate the file size as 11 bytes: “helloworld!“.

§

Learn how grepper helps you improve as a developer!

:/>  MD - создание папки через cmd

Run batch files on startup

If you want to execute a sequent of commands every time you sign in to your Windows 10 account, instead of using Task Scheduler, you can place the script in the “startup” folder to save the extra steps.

To run a script on startup on Windows 10, use these easy steps:

  1. Open File Explorer.
  2. Browse to the folder with the batch file.
  3. Right-click the batch file and select the Copy option.
  4. Use the Windows key R keyboard shortcut to open the Run command.
  5. Type the following command:

    shell:startup

    Run shell startup command

    Source: Windows Central

  6. Click the OK button.
  7. Click the Paste option from the “Home” tab in the Startup folder. (Or Click the Paste shortcut button to create a shortcut to the batch file.)

    Copy and Paste Batch File startup folder

    Source: Windows Central

  8. Sign out of your account.
  9. Sign back into the account.

After you complete the steps, every time you sign in to Windows 10, the batch file will execute and run the included commands.

We’re focusing this guide on Windows 10, but the ability to run batch files has been around for a long time, which means that you can refer to these instructions if you are still on an older version, including Windows 8.1 or Windows 7.

Оставьте комментарий

Adblock
detector