Saturday, May 21, 2016

How to Write a Batch File

A batch file contains a series of DOS commands, and is commonly written to automate frequently performed tasks. Instead of typing the same commands over and over, you can simply double-click the batch file. Writing a batch file is easier than it looks; the difficult part is making sure that everything happens in the right order. Well-made batch files can save you a lot of time over the long run, especially if you deal in repetitive tasks.

EditSteps

EditWriting a Batch File

Note: In Linux, shell scripts are used instead of batch files. See this guide for details on writing shell scripts in Linux.
4288 1 1.jpg
  1. Open Notepad. Notepad allows you to create code as a text file and then save it when you're done as a batch file. You can open Notepad by clicking Start → Programs → Accessories → Notepad. You can also enter into the Run box.
  2. Learn some basic batch commands. Batch files run a series of DOS commands, so the commands that you can use are similar to DOS commands. Some of the more important ones include:
    4288 2.jpg
    • ECHO - Displays text on the screen
    • @ECHO OFF - Hides the text that is normally output
    • START - Run a file with it's default application
    • REM - Inserts a comment line in the program
    • MKDIR/RMDIR - Create and remove directories
    • DEL - Deletes a file or files
    • COPY - Copy a file or files
    • XCOPY - Allows you to copy files with extra options
    • FOR/IN/DO - This command lets you specify files.
    • TITLE - Edit the title of the window. [1]
  3. Write a program to create a directory. One of the easiest ways to learn how to create batch files is to focus on doing basic tasks first. For example, you can use a batch file to quickly create multiple directories:[2]
    4288 3 1.jpg

  4. Save the file. Once you have entered your code, you can save the file using the Batch filetype. Click File → Save As.
    4288 4 1.jpg
    • Click the "Save as type" dropdown menu.
      4288 4b1.jpg
    • Select "All files".
      4288 4b2.jpg
    • Enter a name for the program followed by or .
      4288 4b3.jpg
    • Make sure the encoding is ANSI and save the file
  5. Write the code to make a basic backup program. Batch files are great for running multiple commands, especially if you configure it to be able to run multiple times. With the XCOPY command, you can make a batch file that copies files from select folders to a backup folder, only overwriting files that have been updated since the last copy:
    4288 5 1.jpg

    • This copies over files from the folder "original" to the folder "backupfolder". You can replace these with the paths to the folders you want. specifies that only updated files will be copied, specifies that all subdirectories in the listed directory will be copied, and keeps the confirmation message appearing every time a file is overwritten.
  6. Save the file. Once you have entered your code, you can save the file using the Batch filetype. Click File → Save As.
    4288 6.jpg
    • Click the "Save as type" dropdown menu.
    • Select "All files".
    • Enter a name for the program followed by or .
  7. Write a more advanced backup program. While simply copying the files from one folder to another is nice, what if you want to do a little sorting at the same time? That's where the FOR/IN/DO command comes in. You can use that command to tell a file where to go depending on the extension:
    4288 7 1.jpg

  8. Save the file. Once you have entered your code, you can save the file using the Batch filetype. Click File → Save As.
    4288 8.jpg
    • Click the "Save as type" dropdown menu.
    • Select "All files".
    • Enter a name for the program followed by or .

EditSample Batch File

EditVideo

EditTips

  • You will have to use quotes if you want to open a directory or file with spaces in its name, like start "C:\Documents and Settings\".
  • Batch files can also have a .cmd extension beginning with Windows 2000. There is no difference in operation, but the .cmd files use 32-bit execution while the .bat files use 16-bit execution.
  • You can use third-party editors such as UltraEdit to edit your batch file, but for the most part, these are a waste of time when writing simple batch files.

EditWarnings

  • Depending on the commands you use, it can be dangerous. Be sure you know what you are doing, and that none of the code is dangerous (e.g. batch commands to delete files).

EditRelated wikiHows

EditSources and Citations

.



Cite error: <ref> tags exist, but no <references/> tag was found




No comments:

Post a Comment