1、打开记事本。记事本允许您将代码创建为文本文件,然后在完成后将其保存为批处理文件。可以通过右键----打开记事本记事本通常用于将文本文件转换为批处理文件,但您几乎可以在任何地方编写批处理文件的文本。
2、学习一些基本的批处理命令。批处理文件运行一系列DOS命令,因此可以使用的命令与DOS命令类似。一些更重要的问题包括:ECHO - --Displays text on the screen@ECHO OFF - --Hides the text that is normally outputSTART --- Run a file with its default applicationREM --- Inserts a comment line in the programMKDIR/RMDIR - --Create and remove directoriesDEL - --Deletes a file or filesCOPY - --Copy a file or filesXCOPY --- Allows you to copy files with extra optionsFOR/IN/DO --- This command lets you specify files.TITLE- --Edit the title of the window
3、编写一个程序来创建一个目录。学习如何创建批处理文件的最简单方法之一是首先集中精力完成基本任务。例如,可以使用批处理文件快速创建多个目录MKDIR c:\example1 MKDIR c:\example2
4、编写一个基本备份程序的代码。批处理文件非常适合运行多个命令,特别是当您将其配置为能够运行多次时。使用xcopy命令,可以创建一个批处理文件,将选定文件夹中的文件复制到备份文件夹中,只覆盖自上次复制后已更新的文件:【@ECHO OFF XCOPY c:\original c:\backupfolder /m /e /y 】This copies over files from the folder "original" to the folder "backupfolder". You can replace these with the paths to the folders you want. /m specifies that only updated files will be copied, /e specifies that all subdirectories in the listed directory will be copied, and /y keeps the confirmation message appearing every time a file is overwritten.
5、编写一个更高级的备份程序。简单地将文件从一个文件夹复制到另一个文件夹是很好的,但是如果你想同时进行一些排序呢?这就是for/in/do命令的作用。根据扩展名的不同,可以使用该道药苍嗓命令告诉文件要去哪里:@ECHO OFF cd c:\sourceREM This is the location of the files that you want to sort FOR %%f IN (*.doc *.txt) DO XCOPY c:\source\"%%f" c:\text /m /yREM This moves any files with a .doc or REM .txt extension from c:\source to c:\textREM %%f is a variable FOR %%f IN (*.jpg *.png *.bmp) DO XCOPY C:\source\"%%f" c:\images /m /yREM This moves any files with a .jpg, .png, REM or .bmp extension from c:\source to c:\images
6、使用不同的批处理命令,可以看看下面的测验。如果要在批处理代码中添加仅复制更新文件的节,应在代码中添加什么?/mThat's right! Adding the /m function will ensure only updated files are copied. Using /m in a basic program will allow your batch file to efficiently back up your data. Read on for another quiz question./eNot quite! The /e function won't work to keep only updated files. Instead, use /e to specify that all your subdirectories in any listed directory should be copied. Try another answer...
7、保存批处理文件完成批处理文件文本的输入。完成并校对批处理文件后,可以继续将其保存为可执行文件。
8、单击“文件”。在记事本窗口的左上角。将出现一个下拉菜单。
9、单击另存为“文件”下拉菜单中。单击它将提示“另存为”窗口打开。
10、输入名称和“.bat”扩展名。在“文件名”文本框中,键入要命名的程序,然后键入.bat。例如,对于名为“1234”的程序,您可以在此处键入1234.bat。
11、单击“另存为类型”下拉框。您可以在“另存为”窗口的底部找到它。将出现一个下拉菜单。
12、单击所有文件。在下拉菜单中。这将允许您的文件保存为其扩展名(在本例中为“.bat”)。
13、点击另存为保存文件即可
14、编辑批处理文件的内容。任何时候,您都可以右键单击批处理文件,然后在生成的下拉菜单中单击“编辑”。这将以记事本文档的形式打开批处理文件;此时,您可以进行任何更改,然后按ctrl+s保存文件。