How To

How To Create a File in Windows With Command Prompt

One of the simplest actions you undertake on your computer nearly every day is creating new files. If you use Windows, you most likely use the graphical user interface.

creating files on windows using command prompt

This method has drawbacks even though it may appear to be simpler in reality, especially if you have no prior CLI expertise. A GUI approach requires numerous stages, which makes it time-consuming and inefficient, in contrast to the CLI method, which offers a rapid and efficient way to generate files on your computer.

Here is a guide that will lead you through the various ways to utilise Command Prompt to create a file in Windows (whether it be Windows 11, Windows 10, Windows 8, or Windows 7).

How To Create a File in Windows With Command Prompt

CMD, often known as Command Prompt, is a robust Windows tool that makes it simple for you to carry out a variety of system tasks on your computer. It is pre-installed on the machine and enables you to execute commands for a wide range of tasks, including file management and job automation using batch files.

One such task is creating files, which you may accomplish more quickly and effectively using the CMD prompt than the File Explorer. You must understand how to navigate the Windows directory structure on it, though, in order to create files in the folder of your choice. Before we get started and show you how to create a file in Command Prompt.

Navigating Windows File System Using Command Prompt

Start by launching the Command Prompt before using CMD to traverse the Windows file system. To do this, simply hit Windows + X on your keyboard and choose Command Prompt from the menu.

To list all the files and directories (or folders) inside a folder, type the dir command next. When you have found the folder you wish to open, type the following command and hit Enter:

Folder name: cd

document CD

Run: to return to the parent directory.
cd ..

In order to go to the root directory:
cd \

While we’re about it, you should be aware that the Command Prompt allows you to create directories (or folders). To do this, navigate to the folder where you wish to create a new folder using the cd command, then use the syntax as follows:

directory name: mkdir

Run this command, for instance, to create the directory MyDocs.
MyDocs: mkdir

Use any of the following techniques to create files with CMD after you’re in the folder where you want to create them.

Creating a File Using echo Command

You may see messages you type in the CMD window by using the echo command. However, it also functions as a file creation command that makes a file out of the text you enter when used with the redirection operator (>).

Open the Command Prompt and type your command using the following syntax to create a file using the echo command:
echo filename.extension > your text here

For instance, if you wanted to create a text file called MyFile with the text This is sample text with the extension.txt, you would run:
The following text is an example: MyFile.txt

After creating the file, execute the following command to make that it was generated successfully:

the filename.extension command

Enter MyFile.txt

Creating a File Using copy con Command

The copy con command has a very different strategy than the echo command, which asks you for the content of the file you’re producing at the beginning.

It simply requires that you initially give your file a name. The new file is then opened in a text editor so that you can add text to it.

Use the syntax given below to create a file using copy con:
filename with extension copy con

MyFile.txt is copied.

You may now add the appropriate text to the file by going inside it in the Command Prompt window itself. Press Ctrl + Z to save the file and Ctrl + C to stop editing after you’ve finished.

Creating a File in Notepad Using Command Prompt

Despite the fact that both of the ways we’ve discussed so far are effective, they are not the best option if you want to insert lengthy text paragraphs into your text file. Use Notepad, the programme you typically use to create files, but through the Command Prompt as an alternative solution to this problem.

To do this, type your command in the CMD window using the syntax shown below, then press Enter.
filename.extension in a notepad

For instance, you would run the following command to generate a file named SampleDoc:
Text document SampleDoc.

The file will then be opened in Notepad by CMD. Since the file you input doesn’t already exist, you will be prompted to create a new one. To accept and create the file, click Yes.

Enter your text in the file as you normally would in Notepad, then press Ctrl + S to save it and Ctrl + W to dismiss it.

Creating Multiple Files Using Command Prompt

You can use the for loop as demonstrated in the following syntax when you need to create several files in a folder at once and populate them afterwards.

Type nul > “%a.txt” for /l%a in (1 1 10)
where (1 1 10) instructs the CMD to carry out the task starting at 1 and going up to 10 in steps of 1. Replace 10 in your command with 20 to create 20 files.

You would need to use the following syntax if you wanted to add a common name and a number at the beginning of each file:
Type nul > “filename%a.txt” for /l%a in (1 1 10)

where filename must be replaced with the name you want to give your files.

For instance, using the command below will produce 10 empty files with the name MyDoc-1.
Enter nul in (1 1 10) for /l%a and then click “MyDoc-%a.txt”

Additionally, you can modify the command to include the same action if you wish to generate many files with the same text. Use do echo your desired text in place of including do type nul to accomplish this.

For instance, to create 10 files with the names MyDoc-1.txt, MyDoc-2.txt, etc. containing the text This is sample text as text, you would run:
This is an example text: “MyDoc-%a.txt” for /l%a in (1 1 10) do echo
It will generate them in the directory you are now in.

Efficiently Creating Files on Windows With Command Prompt

It should be possible for you to create single-line text files, text files with numerous paragraphs, or even multiple files with custom text and file names using the techniques outlined in this article.

Therefore, you can select a way in accordance with your needs and quickly and effectively create files on Windows.

FAQs About CMD Create File

1. How do I create a text file in Windows command line?

In Windows, creating a text file is rather simple, and there are a few various approaches you can take. Use the echo command to generate a one-line text file if that’s all you need, whereas the copy con command or Notepad can be used to produce a text file containing many lines of text. Last but not least, you can employ the for loop to produce several text files.

2. Which command is used to create a file?

Microsoft provides a variety of CMD commands for generating files, including echo and copy con. However, you can also use other techniques, such as using Notepad to create a file or a for loop to create numerous files with the name and file extension you want.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button