How to Write Batch Files

Author: Monica Porter
Date Of Creation: 20 March 2021
Update Date: 1 July 2024
Anonim
How to Write a Simple Batch File in Windows
Video: How to Write a Simple Batch File in Windows

Content

In this article, wikiHow will show you how to write and save basic batch files on a Windows PC. A batch file contains a sequence of DOS commands (the Windows language) and is often written to automate common operations such as file transfers. You won't need any extravagant editor to create batch files: the standard Windows Notepad program is more than enough.

Steps

Part 1 of 2: Learn the basics of batch files

  1. , type Notepad, and then click the application icon Notepad Blue is at the top of the menu. advertisement
  • Notepad is often used to convert text files into batch files. However, you can almost write the text of a batch file anywhere.
  • Learn some basic batch commands. The batch file runs a series of DOS commands. Therefore, the commands you can use will be similar to the DOS commands. Here are some important commands:


    • ECHO - Display text on screen
    • @ECHO OFF - Hide commonly displayed text
    • START - Run a file with its default application
    • REM - Insert a comment line into the program
    • MKDIR / RMDIR - Create and remove folders
    • DEL - Delete the file (s)
    • COPY - Copy the file (s)
    • XCOPY - Allows file copying with additional options
    • FOR / IN / DO - This command allows you to specify a file
    • TITLE- Edit title of the window.
  • Write a directory creation program. One of the easiest ways to learn how to create a batch file is to focus on the basics first. For example, you can use a batch file to quickly create multiple directories:


  • Write the code to create a basic backup program. Batch files are great for running multiple commands, especially when it's configured to run multiple times. With the XCOPY command, you can create batch files to copy files from the selected folder into the backup folder and overwrite only files that have been updated from the previous copy:

    • These commands will copy the file from the "original" directory to the "backupfolder" folder (the backup folder). You can replace the path above with the path to the directory you want. Specifies that only updated files will be copied, specifies that all subdirectories in the directory listed will be copied and with the overwrite confirmation will be displayed for all files.
  • Write a more advanced backup program. While copying files from one folder to another is good enough, but what if at the same time, you still want to organize a little bit with those files? This is the time to use the FOR / IN / DO commands. You can use them to specify the location to be copied to, based on the file extensions:


  • Experiment with different batch commands. If you want more inspiration, you can refer to some sample batches.

  • Part 2 of 2: Save the batch file

    1. Complete textual content of batch file. Once you have imported and checked again, you can continue to save it as a batch file.
    2. Click File. This button is in the upper left corner of the Notepad window. A drop-down menu will appear.
    3. Press Save As ... (Save As) in the menu File just dropped. The Save As window will open.
    4. Enter a filename with the extension ".bat". In the "File name" text box, type the name you want to give your program and end with .bat.
      • For example, to create a program called "Backup" you would type Backup.bat.

    5. Click the drop-down "Save as type" box. This box is near the bottom of the Save As window. A new drop-down menu will appear.
    6. Click All Files (All files) are in the drop-down menu. Thanks to that, you can save the file in any format (in this case ".bat").

    7. Choose where to save the file. To do so, click the folder of your choice from among the folders located on the left side of the window (such as Desktop - save on screen).
    8. Press Save (Save). This option is in the lower right corner of the Save As window. The window will be closed.

    9. Close your Notepad file. This file will be saved as a batch file in the folder you selected.
    10. Edit batch file content. You can right-click on the batch file and select Edit (Edit) in the drop-down menu at any time. The batch file will open as a Notepad document and you can now edit as you like and save the file by pressing Ctrl+S.
      • You can immediately see the change the next time you run your batch file.
      advertisement

    Advice

    • You will have to use quotes to open a folder or file whose name contains spaces (for example start "C: Documents and Settings ").
    • A third-party editor, such as Notepad ++ can be used to edit a batch file. In general, however, for simple batch files, it is not necessary.
    • Some commands (such as ipconfig) require administrative privileges to run. If you are using an administrator account, you can right-click on the file and then select "Run as Administrator".

    Warning

    • Depending on the command used, a batch file can be potentially dangerous. Make sure that the codes you use will not perform any unwanted actions (such as deleting files or damaging your computer).