Create file from cmd windows

Create file from command line

We can create files from command line in two ways. The first way is to use fsutil command and the other way is to use echo command. If you want to write any specific data in the file then use echo command. If you are not bothered about the data in the file but just want to create a file of some specific size then you can use fsutil command.

To create file using echo

To create file using fsutil

Limitations

Fsutil can be used only by administrators. For non-admin users it throws up below error.

Thank you for the quick and dirty method since windows seems to lack the “touch” command.

How can we use these commands to ‘touch’ a file. Could anyone provide the exact command that can work as linux ‘touch’ for any type of file?

Windows does not lack “touch”. It is echo. As the post describes? type: echo “” Blah.cpp

dude, this isnt working on our school’s computers, can you tell another method that works on windows 8 computers please?

lmao it works at school PC

Make sure that you are typing in the code the right way. It happened to me when I typed in “echo This is a sample text file sample.txt”.

If you want to create a file with NO text in it do this:
at the CLI type “Echo.”: Your output should look like this:
C:\Temp>echo.

To write to a file:
C:\Temp> Echo. >test.txt

This will create a file with single space in the file.

How to Create and Delete a File or Folder with CMD [MiniTool News]

By Alisa | Follow | Last Updated December 25, 2019

Summary :

This tutorial introduces how to create a file or folder/directory with cmd.exe (Command Prompt) on Windows 10. MiniTool software also provides the professional data recovery software to help you recover any deleted/lost files from computer and various storage devices.

  • How do you create a file using Command Prompt?
  • Which command is used to create a file in DOS?

If you are wondering how to create a file or folder using Command Prompt, this tutorial provides a detailed guide.

How to Create a Folder with CMD

Step 1. Open Command Prompt

At the beginning, you can press Windows + R to open Run dialog, type cmd in Run, and press Enter to open Command Prompt in Windows 10.

Step 2. Go to the Folder Location

After you enter into Command Prompt, you are at C:\Users\YourName by default. If you want to create a folder in another directory, you can type cd folder path (replace the folder path with the actual folder location), and press Enter to go to the desired directory.

Tip: If the default folder isn’t your target folder and you want to create a file with cmd in anther folder, you should type the whole folder path and hit Enter.

Learn how to hide a folder or file in Windows 10 using CMD. Check how to hide a folder/file or show hidden files Windows 10 with attrib command line.

Step 3. Create a Folder with CMD

Then you can type mkdir folder name (replace folder name with the exact name of the folder you want to create), for instance, mkdir work. After you type the command line, you should press Enter key to run this command to create the folder.

How to Delete a Folder in Command Prompt

Step 1. If you want to delete a folder with CMD, you can follow the same instructions above to enter into Command Prompt and go the directory you want to delete a folder.

Step 2. Then you can type rmdir /s folder name (replace folder name with the exact folder name you’d like to delete), e.g. rmdri /s work. If the folder name has a space between words, you should place the folder name in quotations, e.g. rmdir /s “work 2020”. Finally hit Enter to execute the command, and type “Y” to confirm the action.

Читайте также:  Взломать wifi при помощи linux

How to Create a File with CMD

Step 1. Still, follow the same operation above to open Command Prompt in Windows 10 and go to the folder path in which you want to create a file.

Step 2. Type type nul > filename.fileextension in Command Prompt window and hit Enter, e.g. type nul > work.docx, this will create an empty word file named work.

If you want to create a file with some text in it, you can type echo enter your text here >filename.txt. Replace “enter your text here” with the exact text you want to contain in the file and replace the “filename.txt” with the desired file name and extension. For example, echo How to Create a File with CMD >work.txt.

If you want to create a file with a certain file size in CMD, you can type fsutil file createnew filename.docx 1000, replace filename and 1000 with your preferred file name and file byte size.

Learn how to open a file/folder in Command Prompt (cmd) on Windows 10. Step-by-step guide is included.

How to Delete a File in Command Prompt

Step 1. To delete a file with CMD, you can also open Command Prompt and go to the folder path in Command Prompt.

Step 2. Next you can type dir and press Enter to display all the files in the current folder. Find the file you want to delete.

Step 3. Then you can type del filename.fileextension and press Enter to delete the target file with CMD. You should replace the filename with the exact file name and file extension of the deleting file, e.g. del work.txt. If the file name has spaces, you should put the file name into quotations.

If you receive an error message saying that the file can’t be deleted, you can check: how to force delete a file that cannot be deleted.

Note: Deleting a file/folder using Command Prompt in Windows 10 will permanently delete the file and the deleted file will not go to the Recycle Bin, so please be careful with file deletion with CMD.

How to Recover a Mistakenly Deleted File/Folder

Using CMD to delete a file or folder will permanently delete the file. If you mistakenly delete a file/folder, you can use MiniTool Power Data Recovery – professional free data recovery software for Windows 10/8/7 – to easily recover any deleted/lost files and folder from computer, external hard drive, SSD, USB, SD card, and more in 3 simple steps.

Easy 3 steps to fast recover my files/data for free with best free file recovery software. 23 FAQs and solutions for how to recover my files and lost data are included.

  • Facebook
  • Twitter
  • Linkedin
  • Reddit

ABOUT THE AUTHOR

Position: Columnist

Alisa is a professional English editor with 4-year experience. She loves writing and focuses on sharing detailed solutions and thoughts for computer problems, data recovery & backup, digital gadgets, tech news, etc. Through her articles, users can always easily get related problems solved and find what they want. In spare time, she likes basketball, badminton, tennis, cycling, running, and singing. She is very funny and energetic in life, and always brings friends lots of laughs.

How To Create Files – From The Command Line (The Easy Way!) – Windows, CMD

Jump Right In:

Create A File Using CMD (Empty, Text, Batch).

Creating files with the current date.

Creating a dummy file with a specific size.

Create Files From Different Directories.

Create multiple files at once — Using CMD.

What We’ll Learn:

Welcome!
This guide is all about creating new files from the windows command line.

  • We’ll start by learning, how to navigate to the location in which we want to create our file or folder from the command prompt.
  • How to use the Echo command along with a redirector to create all sorts of files.
  • How to embed the current date into the name of your new file.
  • How to create dummy files with a specific size using FSutil.
  • And finally, we’ll learn how to use a for loop to create multiple files at once with a single command.

Lets get started!

Before we begin learning how to create files we must first learn how to change the location of our command line into to the location in which we want to create our new file.

For this example, let’s assume that we want to create our file in our desktop directory.


1)
Open a command prompt without administrator privileges

By default, the command prompt is located at a folder within your users directory that’s named after your computers username (C:\Users\MyPC)


2)
Use the “Dir” command to view every file and folder in the current directory.

The name of every file and folder as well some information about each of them will immediately appear.

Читайте также:  Герои 6 ошибка windows


3)
Navigate to the directory of your choice using the “CD” command.

“CD” stands for “Change Directory”. Remember to surround the name of the directory you want to navigate into within quotes.

Your current working directory will immediately change.

And that’s it! You should now have navigated into the directory of your choice.

In case you made a mistake you can navigate to the previous directory by typing Cd followed by two dots.

The double dots represent the parent directory.

Now that we are located in the correct directory lets create our file.

Create A File Using CMD (Empty, Text, Batch).

Lets start with learning how to create files from our command line. To do so we will need to use the “Echo” command along with a redirector.

What exactly is echo and a redirector I hear you ask?

This might start to sound complicated, but don’t worry its actually pretty simple.

  • Echo is primarily used to print variables or strings to the command line, similar to “print” or “Console.WriteLine” in other programming languages.
  • Redirectors are special symbols that can be used complete many functions such as combine two commands or redirect the output of one command into input for another.

Do you see where I am going with this?

We can use the echo command to print a string and redirect its output into a new file by using a redirector, specifically the greater than redirector (>).

Lets see what that would look like:

Text Files:

Lets start by creating a new text file.

To do so all we have to do is type the echo command followed by the text we want our new file to have, the grater than redirector, and the name of your new file within quotes along with the .txt extension.

You should see a new file in your current directory. Pretty simple right?

To view the contents of a file we just created directly from the command line we can use the “Type” command. Simply enter type followed by the name of the file we just created.

The contents of the file will immediately be shown in the command prompt.

Apart from creating a new file we can add or append text to an existing text file. To do so we need to use the greater than symbol twice. Here is what that would look like:

Our file will now contain the following text:

Empty Text Files

In many cases you might want to crate a completely empty text file. The syntax for this command is very similar to our previous command however this time we need to add a dot directly after the echo command.

An empty file will immediately appear in your current directory.

In case you are wondering, the dot tells the echo command to not output anything.

Batch Files

While creating text files is nice, in may case you will want to create batch files. Doing so is very simple, all we have to do is replace the extension of the name of our new file from .txt to .bat.

Here is what that would look like:

And a new batch file will immediately be created in your current directory.

Literally Any type of File:

In a similar way by using the extension of our choice we can create literally any type of file.

  • .docx Creates word documents.
  • .py Creates python scripts.
  • .sql Creates SQL databases.
  • And so on…

Creating files with the current date — Using CMD:

You can embed the current date right into the name of the file you are creating by taking advantage of the %Date% automatic variable. Here is what our command would look like:

This command will create a text file with the current date as its name in our current directory.

In my case the file will be named:

You might be wondering what /=- does.

By default, the date in the %Date% variable is stored in this format:

However, to use this as the name of a file we need to replace the backward slashes with any other symbol, so that the command line doesn’t misinterpret our date as a directory.

That is exactly what /=- does, you can replace the dash with any symbol of your choice.

Creating a dummy file with a specific size — Using CMD:

Using the windows command prompt you can create empty, dummy files with a specific size of your choice.

To do so we need to use the FSutil command instead of echo, which is what we have been using thus far.

FSutil stands for file system utility and as the name suggests it can be used to perform various operations on our file system.

But you don’t care about any of that, you just want to create a file.

To do so all you have to do is use the following syntax:

Confused? Lets break it down:

  • The File parameter instructs the FSutil command to create a file. Duh.
  • CreateNew Instruct it to create a new item.
  • Them we have the name of our new file, in this case: Dummy File.
  • And finally, the size we want our file to have in bytes.
Читайте также:  Показать температуру процессора windows 10

In this case a 10 MB file will be created.

Adjust the number of bytes to create a file with the size you want.

In case you want to convert gigabytes to bytes, use this online converter.

Create Files From Different Directories — From CMD:

In all the above examples our new files will be created in the current directory.

In case you want to create your new file in a directory other than your current one, you need to enter your target directory just before the name of your new file.

So here is what our echo command would look like:

This command will create an empty file in our documents directory.

To make it work for you replace with your computers username.

And here is what our FSutil command would look like:

This command will create a dummy file with a 10 MB size in our desktop directory.

Once again replace with your computers username.

Create multiple files at once — Using CMD:

In all the above examples we created only a single file. The beauty of the command line however is that you can complete complex, time consuming tasks in an instance, such as creating multiple files at once.

To do so, we need to use a for loop. For loops repeat an instruction a certain number of times.

Lets take a look at all the ways we can use a for loop to create multiple files:

Numbered Files (File1, File2, File3):

Lets start with creating numbered files or, files for which only a number changes between their names.

Without further a do, here is what our command would look like:

This command will create ten empty files in our current directory with the following names: File 1.txt, File 2.txt, File 3.txt, and so on…

This command might also look a little complicated, so let’s break it down:

  • The /L parameter instructs the for command to iterate through the numbers in our parentheses, instead of using them directly as the names of our files.
  • Our A parameter specifies a single letter replaceable parameter. You can replace A with any letter of your choice. This is similar to a variable, in the sense that its value changes.
  • After that we have our condition, the first number specifies the starting number, the second number is the step, and the third number is the ending number.
  • And finally, in parenthesis we have the command that will be executed every time the loop is executed.

Adjust the above command accordingly to suit your needs.

To create your files in a different directory, add the path of your choice, just before the name of your new files.

If you are unsure, here’s what that would look like:

Replace with your computers username.

Also, if you are planning on running any of these commands from a batch script you need to use two percentage signs instead of one, so that they are interpreted correctly.

Named Files (First, Second, Third):

Creating named files or, multiple files with different, specified names is quite similar.

Here is what that would look like:

The above command will create three files in your current directory with the following names: First.txt, Second.txt, Third.txt.

To make this command work for you, replace the names you want your files to have in the first parenthesis. And if necessary, replace, or adjust the second parenthesis with the command that will create your files the way you require.

The number of files that will be created depends entirely on how many file names you feed into the loop.

Once again to create your files in a different directory, add the target path just before the A parameter in the second parenthesis. Take a look at the above commands if you need a reference.

Finally, if you are planning on running this command from a batch script instead of the command line, you need to use two percentage signs instead of one so that your command is interpreted correctly.

Summary:

  • Navigate to the directory you want to create a file into using the “CD” and “Dir” commands.
  • Use the echo command along with the greater than redirector to create any type of file.
  • Take advantage of the %Date% automatic variable and embed the current date into the names of your files.
  • Use the FSutil command to crate a new file with a specific size.
  • Enter a path before the name of your file to create it in any directory.
  • Use a For loop to create multiple numbered or named files at once.

That’s It!

You now know how to create many types of files directly from the command prompt.

If you liked this short guide take a look at a few of our other posts related to the windows command line, or if you really liked it consider enrolling in our video course where you will learn the ins and outs of the Windows command Line.

Оцените статью