recent
أخبار ساخنة

Create file using Batch script


How to create and make files in Batch?

To make and create batch file that create files, you have to do the following:

1- Open the notepad or any other text editor.

2- Turn the echo command off by typing @echo off

3- Use the command line echo.>FileName.ExtensionName

4- Save the command lines as .bat file.

5- Run the batch file.


After running the batch, the file FileName.ExtensionName will be created.

Example1: create the file MySong.mp3

The following code will create the mp3 file with name entitled MySongs as following:

@echo off
Echo.>MySongs.mp3

Create file name by users in batch

Now, we will move forward and do something more advanced. We will write batch commands that will allow users to type the file name and the extension name from the batch window. This feature will grant users to choose any name assigned to the created file.
The following command lines will serve that purpose:

@echo off
Title create file and extension named by users in batch window
set /p name= Enter the name of your created file:
if %name%== quit goto :b
set /p ext= Enter the Extension of the created file:
echo. >%name%.%ext%

After running the upper code, the batch window will disappear and the created file will be presented at the same location where the batch file existed.

Create many files in batch window

You may also write command line that create multiple files as much as you need without running the batch files many times. This can be done using the loops and the conditional sentence if. In this case we want to use two loops, the first one for repeating the file creation coupled with file name and its extension. The second loop for closing and exiting the batch file when the word “quit” detected in the first loop.  All you need to do is to run the batch file one time, and type the name of that file and its extension.
As an extra feature, you can add this command line which will force the batch window to close and exit when user type the word “quit” as the name of that file.
@echo off
Title create many files with dedicated names at once by users in batch window
:a
set /p name= Enter the name of your created file:
if %name%== quit goto :b
set /p ext= Enter the Extension of the created file:
echo. >%name%.%ext%
goto :a
:b
Exit

As shown in the upper code, we have defined the two variables: name & ext. Both of them are dedicated for naming the file and its extension. Once the user type “quit” the command will move to the exit section in b.

author-img
العلم للجميع

تعليقات

ليست هناك تعليقات
إرسال تعليق
    google-playkhamsatmostaqltradent